-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.py
149 lines (121 loc) · 2.98 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#Sam Kellman-Wanzer
#GLTF file converter
#Takes a type of file supported by PyMesh and converts to GLTF
import numpy as np
import pymesh
import base64
print("enter the file name")
filestring = str(input())
mesh = pymesh.load_mesh(filestring);
vertex_data = mesh.vertices
face_data = mesh.faces
VERTICES = vertex_data.ravel().astype(np.float32)
MAX_X = -np.inf
MAX_Y = -np.inf
MAX_Z = -np.inf
MIN_X = np.inf
MIN_Y = np.inf
MIN_Z = np.inf
for v in vertex_data:
MAX_X = max(MAX_X, v[0])
MAX_Y = max(MAX_Y, v[1])
MAX_Z = max(MAX_Z, v[2])
MIN_X = min(MIN_X, v[0])
MIN_Y = min(MIN_Y, v[1])
MIN_Z = min(MIN_Z, v[2])
INDICES = face_data.ravel().astype(np.ushort)
MAX = -np.inf
MIN = np.inf
for i in face_data:
MAX = max(MAX, i[0])
MAX = max(MAX, i[1])
MAX = max(MAX, i[2])
MIN = min(MIN, i[0])
MIN = min(MIN, i[1])
MIN = min(MIN, i[2])
VERTICES = np.array(VERTICES, dtype=np.float32)
INDICES = np.array(INDICES, dtype = np.ushort)
HOWMANY_V = len(VERTICES) / 3
HOWMANY_I = len(INDICES)
HOWMANYBYTES_V = VERTICES.nbytes
HOWMANYBYTES_I = INDICES.nbytes
B64_VERTICES = base64.b64encode(VERTICES)
B64_INDICES = base64.b64encode(INDICES)
gltf = {
"asset": {
"version": "2.0",
"generator": "Pymesh gltf converter"
},
"accessors": [
{
"bufferView": 0,
"byteOffset": 0,
"componentType": 5126,
"count": HOWMANY_V,
"type": "VEC3",
"max": [MAX_X, MAX_Y, MAX_Z],
"min": [MIN_X, MIN_Y, MIN_Z]
},
{
"bufferView": 1,
"byteOffset": 0,
"componentType": 5123,
"count": HOWMANY_I,
"type": "SCALAR",
"max": [MAX],
"min": [MIN]
}
],
"bufferViews": [
{
"buffer": 0,
"byteOffset": 0,
"byteLength": HOWMANYBYTES_V,
"target": 34962
},
{
"buffer": 1,
"byteOffset": 0,
"byteLength": HOWMANYBYTES_I,
"target": 34963
}
],
"buffers": [
{
"uri": "data:application/octet-stream;base64,"+str(B64_VERTICES, 'utf-8'),
"byteLength": HOWMANYBYTES_V
},
{
"uri": "data:application/octet-stream;base64,"+str(B64_INDICES, 'utf-8'),
"byteLength": HOWMANYBYTES_I
}
],
"meshes": [
{
"primitives": [{
"mode": 4,
"attributes": {
"POSITION": 0
},
"indices": 1
}]
}
],
"nodes": [
{
"mesh": 0
}
],
"scenes": [
{
"nodes": [
0
]
}
],
"scene": 0
}
print ( str(gltf).replace("'", '"') ) # we need double quotes instead of single quotes
#save file
with open('cube.gltf', 'w') as f:
f.write(str(gltf).replace("'", '"'))