Export works but objects are not properly positioned

Compiling, libraries, modules, coding guidelines and porting

Moderators: jesterKing, stiv

Post Reply
dom107
Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

Export works but objects are not properly positioned

Post by dom107 » Mon Jun 04, 2012 11:03 am

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: Select all

# 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
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Post by CoDEmanX » Tue Jun 05, 2012 12:00 am

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...

dom107
Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

local coordinates system

Post by dom107 » Tue Jun 05, 2012 9:03 am

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.

dom107
Posts: 35
Joined: Wed Apr 18, 2012 10:04 am

and...

Post by dom107 » Tue Jun 05, 2012 10:42 am

data was created as:
data = obj.to_mesh(bpy.context.scene, True, 'PREVIEW')
... and what about using data.transform?

Code: Select all

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)       

Post Reply

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest