Extract mesh vertices and faces from objects. Works in both object mode and edit mode.
()
| 77 | return object_data |
| 78 | |
| 79 | def extract_object_model_data(): |
| 80 | """ |
| 81 | Extract mesh vertices and faces from objects. |
| 82 | Works in both object mode and edit mode. |
| 83 | """ |
| 84 | object_data = {} |
| 85 | objects = get_all_objects() |
| 86 | scene = bpy.context.scene |
| 87 | |
| 88 | for obj in objects: |
| 89 | scene.frame_set(scene.frame_start) |
| 90 | |
| 91 | # Only process mesh objects |
| 92 | if obj.type != 'MESH': |
| 93 | continue |
| 94 | |
| 95 | # Select the object and make it active |
| 96 | bpy.context.view_layer.objects.active = obj |
| 97 | obj.select_set(True) |
| 98 | |
| 99 | # If in object mode, access mesh data directly |
| 100 | mesh = obj.data |
| 101 | |
| 102 | # Extract vertices (in local coordinates) |
| 103 | vertices = np.array([v.co[:] for v in mesh.vertices], dtype=np.float32) |
| 104 | |
| 105 | # Extract faces (vertex indices) |
| 106 | faces = [] |
| 107 | for p in mesh.polygons: |
| 108 | for iters in range(len(p.vertices) - 2): |
| 109 | faces.append([p.vertices[0], p.vertices[iters + 1], p.vertices[iters + 2]]) |
| 110 | faces = np.array(faces, dtype=np.int64) |
| 111 | |
| 112 | object_data[obj.name] = { |
| 113 | 'mesh': (vertices, faces), |
| 114 | } |
| 115 | |
| 116 | return object_data |
| 117 | |
| 118 | fbx_path = os.path.join(fbx_folder_path, f'{folder}.fbx') |
| 119 | assert os.path.exists(fbx_path), f"FBX file not found: {fbx_path}" |
no test coverage detected