Changeset View
Changeset View
Standalone View
Standalone View
io_shape_mdd/export_mdd.py
| Context not available. | |||||
| file.close() | file.close() | ||||
| def check_vertcount(mesh, vertcount): | |||||
| """ | |||||
| check and make sure the vertcount is consistent throughout the frame range | |||||
| """ | |||||
| if len(mesh.vertices) != vertcount: | |||||
| raise Exception('Error, number of verts has changed during animation, cannot export') | |||||
| def save(operator, context, filepath="", frame_start=1, frame_end=300, fps=25.0): | def save(operator, context, filepath="", frame_start=1, frame_end=300, fps=25.0): | ||||
| """ | # Initialize scene for export | ||||
| Blender.Window.WaitCursor(1) | |||||
| mesh_orig = Mesh.New() | |||||
| mesh_orig.getFromObject(obj.name) | |||||
| """ | |||||
| scene = context.scene | scene = context.scene | ||||
| obj = context.object | |||||
| if bpy.ops.object.mode_set.poll(): | |||||
| bpy.ops.object.mode_set(mode='OBJECT') | |||||
| orig_frame = scene.frame_current | orig_frame = scene.frame_current | ||||
| scene.frame_set(frame_start) | scene.frame_set(frame_start) | ||||
| me = obj.to_mesh(scene, True, 'PREVIEW') | |||||
| #Flip y and z | |||||
| ''' | |||||
| mat_flip = mathutils.Matrix(((1.0, 0.0, 0.0, 0.0), | |||||
| (0.0, 0.0, 1.0, 0.0), | |||||
| (0.0, 1.0, 0.0, 0.0), | |||||
| (0.0, 0.0, 0.0, 1.0), | |||||
| )) | |||||
| ''' | |||||
| mat_flip = mathutils.Matrix() | |||||
| numverts = len(me.vertices) | if bpy.ops.object.mode_set.poll(): | ||||
| bpy.ops.object.mode_set(mode='OBJECT') | |||||
| numframes = frame_end - frame_start + 1 | frame_count = frame_end - frame_start + 1 | ||||
| f = open(filepath, 'wb') # no Errors yet:Safe to create file | f = open(filepath, 'wb') # no Errors yet:Safe to create file | ||||
| # Count the number of vertices in the first frame | |||||
| first_frame_vertcount = 0 | |||||
| for object in context.selected_objects: | |||||
| if 'MESH' == object.type: | |||||
| mesh = object.to_mesh(scene, True, 'PREVIEW') | |||||
| first_frame_vertcount = first_frame_vertcount + len(mesh.vertices) | |||||
| # Write the header | # Write the header | ||||
| f.write(pack(">2i", numframes, numverts)) | f.write(pack(">2i", frame_count, first_frame_vertcount)) | ||||
| # Write the frame times (should we use the time IPO??) | # Write the frame times (should we use the time IPO??) | ||||
| f.write(pack(">%df" % (numframes), *[frame / fps for frame in range(numframes)])) # seconds | f.write(pack(">%df" % (frame_count), *[frame / fps for frame in range(frame_count)])) # seconds | ||||
| for frame in range(frame_start, frame_end + 1): # in order to start at desired frame | # Loop through all animation frames; track number of vertices | ||||
| for frame in range(frame_start, frame_end + 1): | |||||
| # Set animation frame | |||||
| scene.frame_set(frame) | scene.frame_set(frame) | ||||
| me = obj.to_mesh(scene, True, 'PREVIEW') | |||||
| check_vertcount(me, numverts) | |||||
| me.transform(mat_flip * obj.matrix_world) | |||||
| # Write the vertex data | # Loop through all objects | ||||
| f.write(pack(">%df" % (numverts * 3), *[axis for v in me.vertices for axis in v.co])) | frame_vertcount = 0 | ||||
| for object in context.selected_objects: | |||||
| if 'MESH' == object.type: | |||||
| # Convert object to mesh in world space | |||||
| mesh = object.to_mesh(scene, True, 'PREVIEW') | |||||
| mesh.transform(object.matrix_world) | |||||
| # Write the vertex data | |||||
| mesh_vertcount = len(mesh.vertices) | |||||
| f.write(pack(">%df" % (mesh_vertcount * 3), *[axis for v in mesh.vertices for axis in v.co])) | |||||
| # Track the total number of vertices | |||||
| frame_vertcount = frame_vertcount + mesh_vertcount | |||||
| # Check if our vertex count has changed | |||||
| if first_frame_vertcount != frame_vertcount: | |||||
| f.close() | |||||
| raise Exception('Error, number of verts has changed during animation, cannot export') | |||||
| # Clean up | |||||
| f.close() | f.close() | ||||
| print('MDD Exported: %r frames:%d\n' % (filepath, numframes - 1)) | # Print summary | ||||
| print('MDD Exported: %r frames:%d\n' % (filepath, frame_count)) | |||||
| scene.frame_set(orig_frame) | scene.frame_set(orig_frame) | ||||
| # Indicate that we're done | |||||
| return {'FINISHED'} | return {'FINISHED'} | ||||
| Context not available. | |||||