Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_frame_exploded_mesh(obj, frame):
- # Set Blender to the specific frame
- bpy.context.scene.frame_set(frame)
- # Duplicate the object for this frame
- obj_copy = obj.copy()
- obj_copy.data = obj.data.copy()
- # link is needed for modifier operations
- bpy.context.collection.objects.link(obj_copy)
- #DECIMATE----------------------------------------------
- decimate_mod = obj_copy.modifiers.new(name="Decimate", type='DECIMATE')
- bpy.context.view_layer.objects.active = obj_copy
- #apply both modifiers to get mesh and decimate it
- bpy.ops.object.modifier_apply(modifier="MeshSequenceCache")
- print("pre decimation : ", len(obj_copy.data.polygons))
- max_faces = 8000
- original_faces = len(obj_copy.data.polygons)
- decimate_mod.ratio = (max_faces / original_faces) * 0.96
- bpy.ops.object.modifier_apply(modifier=decimate_mod.name)
- print("post decimation : ", len(obj_copy.data.polygons))
- mesh_eval = bpy.data.meshes.new_from_object(obj_copy)
- # create new mesh instance from that and explode it and record vertex data
- bm = bmesh.new()
- bm.from_mesh(mesh_eval)
- # Triangulate all faces
- bmesh.ops.triangulate(bm, faces=bm.faces)
- # Split all the edges so no two faces share vertices
- bmesh.ops.split_edges(bm, edges=bm.edges[:])
- # Update the mesh with the exploded geometry
- bm.to_mesh(mesh_eval)
- # Collect triangle data from that frame
- faces = {}
- for face_index, face in enumerate(bm.faces):
- triangle = {}
- for vert_index,vert in enumerate(face.verts):
- co = vert.co.copy()
- normal = vert.normal.copy() # Get normal of the vertex
- # Store vertex coordinates and normal
- triangle[f'vertex_{vert_index}'] = {
- 'position': [co.x, co.y, co.z],
- 'normal': [normal.x, normal.y, normal.z]
- }
- faces[f'face_{face_index}'] = triangle
- # Clean up
- bm.free()
- bpy.data.meshes.remove(mesh_eval)
- bpy.context.collection.objects.unlink(obj_copy)
- #revert back to original object reference
- bpy.context.view_layer.objects.active = active_obj
- #print("processed frame: ", frame, "with ", len(faces), "faces")
- return faces
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement