| Code: |
| # obj : object being processed
# data : its mesh data # For the first object, the following is stored (first 2 instructions because of axis changes): first_obj_transform = Matrix.Rotation(radians(90), 4, 'X') first_obj_transform *= Matrix.Scale(-1, 4, Vector((0, 0, 1))) first_obj_matrix = obj.matrix_world.copy() Then, for each object (obj) and its mesh (data): obj_matrix = obj.matrix_world.copy() data.calc_tessface() # Compute and update the list of tessellated faces (faces of 3 or 4 vertices) ... # Take into account the axis changes: obj_matrix = obj_matrix * first_obj_transform # Pivot in world coordinates pivot = Vector([obj.matrix_world.translation[0], obj.matrix_world.translation[1], obj.matrix_world.translation[2]]) # Update pivot applying the axis transformation new_pivot = first_obj_transform * pivot obj_matrix.translation = new_pivot[0], new_pivot[1], new_pivot[2] # Get vertices, normals and UVs from the faces for face_num, face in enumerate(data.tessfaces): ... # Process triangle or quad for face_num, face in enumerate(data.tessfaces): f_v = face.vertices ... # Process each vertex for the found triangles # convert local object coordinates to local coordinates of first object local_vertex = data.vertices[f_v[vert_num]].co * obj_matrix * first_obj_matrix.inverted() local_vertex.x += first_obj_matrix.translation[0] - obj_matrix.translation[0] local_vertex.y += first_obj_matrix.translation[1] - obj_matrix.translation[1] local_vertex.z += first_obj_matrix.translation[2] - obj_matrix.translation[2] # Add the converted vertices in the export ... |
| CoDEmanX wrote: |
| you sure you want to convert vertex coordinates from local to 1st object's coordinate system??? |
| Code: |
| data = obj.to_mesh(bpy.context.scene, True, 'PREVIEW') # <<<<<
# For the first object, the following is stored (first 2 instructions because of axis change): first_obj_transform = Matrix.Rotation(radians(90), 4, 'X') first_obj_transform *= Matrix.Scale(-1, 4, Vector((0, 0, 1))) first_obj_matrix = obj.matrix_world.copy() # Pivot of first object in world coordinates pivot = Vector([first_obj_matrix.translation[0], first_obj_matrix.translation[1], first_obj_matrix.translation[2]]) # Update pivot applying the axis change new_pivot = first_obj_transform * pivot # Update matrix to apply axis rotation first_obj_matrix *= first_obj_transform # Update matrix to apply pivot new coordinates in new axis system first_obj_matrix.translation = new_pivot[0], new_pivot[1], new_pivot[2] # Update rotation matrix to apply pivot new coordinates first_obj_transform.translation = new_pivot[0], new_pivot[1], new_pivot[2] Then, for each object (obj) and its mesh (data created with to_mesh): obj_matrix = obj.matrix_world.copy() # Take into account the axis changes: obj_matrix = obj_matrix * first_obj_transform data.transform(obj_matrix) # <<<<< data.calc_tessface() # Compute and update the list of tessellated faces (faces of 3 or 4 vertices) ... # Get vertices, normals and UVs from the faces for face_num, face in enumerate(data.tessfaces): ... # Process triangle or quad for face_num, face in enumerate(data.tessfaces): f_v = face.vertices ... # Process each vertex for the found triangles # convert world object coordinates to local coordinates of first object local_vertex = data.vertices[f_v[vert_num]].co * first_obj_matrix.inverted() # Add the converted vertices in the export ... # to_mesh() created a new mesh so remove it bpy.data.meshes.remove(data) |