Previous Thread  Next Thread

chat icon Export works but objects are not properly positioned

dom107

Posted: Mon Jun 04, 2012 10:03 am
Joined: 18 Apr 2012
Posts: 35
Hello,

I have a set of Blender objects that I want to export as a single object.
I process each object, adding vertices, triangles for the exported object.
Dealing with materials for the processed objects is not mentionned here.

Is the following processing principle correct? (I'm asking, as I don't quite get what I expect: The export works.
I have all the expected objects but objects are not properly positioned in the exported world).

Thanks in advance for advices and help.

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
   ...
Reply with quote


CoDEmanX

Posted: Mon Jun 04, 2012 11:00 pm
Joined: 05 Apr 2009
Posts: 894
you sure you want to convert vertex coordinates from local to 1st object's coordinate system???
_________________
I'm sitting, waiting, wishing, building Blender in superstition...
Reply with quote


dom107

Posted: Tue Jun 05, 2012 8:03 am
Joined: 18 Apr 2012
Posts: 35
CoDEmanX wrote:
you sure you want to convert vertex coordinates from local to 1st object's coordinate system???


Yes, in the export format, the object format is in local coordinates + world matrix.
Reply with quote


dom107

Posted: Tue Jun 05, 2012 9:42 am
Joined: 18 Apr 2012
Posts: 35
data was created as:
data = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
... and what about using data.transform?


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)       
Reply with quote


 
Jump to:  
Powered by phpBB © 2001, 2005 phpBB Group