77num_nodes_to_name = {3 : 'triangle' , 4 : 'quad' }
88
99
10- # import bpy
11- def readMZD (filepath ):
10+ def readMZD_to_meshio (filepath ):
1211 out_numVertices = None
1312 out_numPolygons = None
1413 out_vertPositions = None
@@ -129,6 +128,156 @@ def readMZD(filepath):
129128 out_vertAttribute = np .frombuffer (byte , dtype = np .float32 )
130129 point_data ['uvw_map' ] = out_vertAttribute .reshape ((out_numVerticeAttributes , 3 ))
131130
131+ # For the rest of attributes, because meshio doest not support attributes on nodes, (equivalent to face cornder in blender)
132+ # So the attributes data will be skipped
133+ elif chunkID == 0xDA7A0011 : # node normals.
134+ file .seek (size , 1 )
135+ pass
136+ elif chunkID == 0xDA7A0013 : # node colors.
137+ file .seek (size , 1 )
138+ pass
139+ elif chunkID == 0xDA7A0014 : # node UVWs.
140+ file .seek (size , 1 )
141+ pass
142+ else :
143+ file .seek (size , 1 )
144+ pass
145+ return meshio .Mesh (out_vertPositions .reshape ((out_numVertices , 3 )), cells , point_data )
146+
147+ def readMZD_to_bpymesh (filepath , mesh ):
148+ shade_scheme = False
149+ if mesh .polygons :
150+ shade_scheme = mesh .polygons [0 ].use_smooth
151+
152+ if len (mesh .vertices )> 0 :
153+ mesh .clear_geometry ()
154+
155+ out_numVertices = None
156+ out_numPolygons = None
157+ out_vertPositions = None
158+ out_numNodes = None # number of loops
159+ out_polyVIndicesNum = None # faces_loop_total
160+ out_polyVIndices = None #loops_vert_idx
161+
162+ with open (filepath , 'rb' ) as file :
163+ byte = file .read (24 )
164+ if byte != head :
165+ return - 4
166+ while 1 :
167+ # check if it reach the end
168+ byte = file .read (24 )
169+ if byte == end :
170+ break
171+ else :
172+ # if not reach the end, rewind the pointer back 24 bytes
173+ file .seek (- 24 , 1 )
174+
175+ byte = file .read (4 )
176+ chunkID = int .from_bytes (byte , byteorder = 'little' )
177+
178+ byte = file .read (24 )
179+ name = byte
180+
181+ byte = file .read (4 )
182+ size = int .from_bytes (byte , byteorder = 'little' )
183+
184+ if chunkID == 0x0ABC0001 : # vertices and polygons.
185+
186+ byte = file .read (4 )
187+ out_numVertices = int .from_bytes (byte , byteorder = 'little' )
188+ if out_numVertices < 0 :
189+ return - 127
190+ if out_numVertices == 0 :
191+ break
192+
193+ byte = file .read (12 * out_numVertices )
194+ out_vertPositions = np .frombuffer (byte , dtype = np .float32 )
195+
196+ byte = file .read (4 )
197+ out_numPolygons = int .from_bytes (byte , byteorder = 'little' )
198+
199+ byte = file .read (out_numPolygons )
200+ out_polyVIndicesNum = np .frombuffer (byte , dtype = np .uint8 )
201+ out_numNodes = out_polyVIndicesNum .sum (dtype = np .int32 )
202+
203+ byte = file .read (4 )
204+ numBytesPerPolyVInd = int .from_bytes (byte , byteorder = 'little' )
205+
206+ if numBytesPerPolyVInd == 4 :
207+ # int
208+ byte = file .read (out_numNodes * numBytesPerPolyVInd )
209+ out_polyVIndices = np .frombuffer (byte , dtype = np .int32 )
210+ elif numBytesPerPolyVInd == 2 :
211+ # unsigned short
212+ byte = file .read (out_numNodes * numBytesPerPolyVInd )
213+ # WARNING: not sure if it's correct
214+ # uncovered branch from test data
215+ out_polyVIndices = np .frombuffer (byte , dtype = np .uint16 )
216+ else :
217+ return - 127
218+
219+ mesh .vertices .add (out_numVertices )
220+ mesh .vertices .foreach_set ('co' ,out_vertPositions )
221+
222+ mesh .loops .add (out_numNodes )
223+ mesh .loops .foreach_set ('vertex_index' ,out_polyVIndices )
224+
225+ mesh .polygons .add (out_numPolygons )
226+ mesh .polygons .foreach_set ('loop_total' ,out_polyVIndicesNum )
227+ faces_loop_start = None
228+
229+ if out_polyVIndicesNum .size > 0 :
230+ faces_loop_start = np .cumsum (out_polyVIndicesNum )
231+ faces_loop_start = np .roll (faces_loop_start , 1 )
232+ faces_loop_start [0 ] = 0
233+ mesh .polygons .foreach_set ('loop_start' ,faces_loop_start )
234+ mesh .polygons .foreach_set ("use_smooth" , [shade_scheme ] * out_numPolygons )
235+ mesh .update ()
236+ mesh .validate ()
237+
238+ elif chunkID == 0xDA7A0001 : # vertex normals.
239+ byte = file .read (4 )
240+ out_numVerticeAttributes = int .from_bytes (byte , byteorder = 'little' )
241+ if out_numVerticeAttributes != out_numVertices :
242+ return - 127
243+
244+ byte = file .read (out_numVerticeAttributes * 6 )
245+ out_vertAttribute = np .frombuffer (byte , dtype = np .uint16 )
246+ out_vertAttribute = table [out_vertAttribute ]
247+ # point_data['normal'] = out_vertAttribute.reshape((out_numVerticeAttributes, 3))
248+
249+ elif chunkID == 0xDA7A0002 : # vertex motions
250+ byte = file .read (4 )
251+ out_numVerticeAttributes = int .from_bytes (byte , byteorder = 'little' )
252+ if out_numVerticeAttributes != out_numVertices :
253+ return - 127
254+
255+ byte = file .read (out_numVerticeAttributes * 6 )
256+ out_vertAttribute = np .frombuffer (byte , dtype = np .uint16 )
257+ out_vertAttribute = table [out_vertAttribute ]
258+ # point_data['velocity'] = out_vertAttribute.reshape((out_numVerticeAttributes, 3))
259+
260+ elif chunkID == 0xDA7A0003 : # vertex colors
261+ byte = file .read (4 )
262+ out_numVerticeAttributes = int .from_bytes (byte , byteorder = 'little' )
263+ if out_numVerticeAttributes != out_numVertices :
264+ return - 127
265+
266+ byte = file .read (out_numVerticeAttributes * 8 )
267+ out_vertAttribute = np .frombuffer (byte , dtype = np .uint16 )
268+ out_vertAttribute = table [out_vertAttribute ]
269+ # point_data['color'] = out_vertAttribute.reshape((out_numVerticeAttributes, 3))
270+
271+ elif chunkID == 0xDA7A0004 : # vertex UVWs.
272+ byte = file .read (4 )
273+ out_numVerticeAttributes = int .from_bytes (byte , byteorder = 'little' )
274+ if out_numVerticeAttributes != out_numVertices :
275+ return - 127
276+
277+ byte = file .read (out_numVerticeAttributes * 12 )
278+ out_vertAttribute = np .frombuffer (byte , dtype = np .float32 )
279+ # point_data['uvw_map'] = out_vertAttribute.reshape((out_numVerticeAttributes, 3))
280+
132281 elif chunkID == 0xDA7A0011 : # node normals.
133282 file .seek (size , 1 )
134283 print (6 )
@@ -144,8 +293,4 @@ def readMZD(filepath):
144293 else :
145294 # print(name)
146295 file .seek (size , 1 )
147- pass
148- return meshio .Mesh (out_vertPositions .reshape ((out_numVertices , 3 )), cells , point_data )
149-
150- def constructMZD (filepath ):
151- pass
296+ pass
0 commit comments