Previous Thread  Next Thread

chat icon Getting mesh's transformation

rodrigoloc

Posted: Sun Dec 16, 2012 12:29 am
Joined: 16 Dec 2012
Posts: 2
Hello.
I'm trying to do an exporter to my C/C++ geometry format. It works almost perfectly but I've got problems with the transformation.
How can I get the mesh's transformation?
When I write at the python console 'bpy.data.meshes[0].vertices[0].co' the output is (-1.0 ; -1.0 ; -1.0) (it's an example), but when I rotate the mesh at the 3D View, it continues being the same output at the console.

Here's the code (I'm not very good at python):

filename = bpy.path.ensure_ext(self.filepath, self.filename_ext)
file = open(filename, 'wb')
d = bpy.data
mc = len(d.meshes)
array('i', [mc]).tofile(file)
for i in range(mc):
mesh = d.meshes[i]
vc = len(mesh.vertices)
array('i', [vc]).tofile(file)
for j in range(vc):
vertex = mesh.vertices[j]
for co in vertex.co:
array('f', [co]).tofile(file)
for normal in vertex.normal:
array('f', [normal]).tofile(file)
pc = len(mesh.polygons)
array('i', [pc]).tofile(file)
for j in range(pc):
polygon = mesh.polygons[j]
pvc = len(polygon.vertices)
array('i', [pvc]).tofile(file)
for k in range(pvc):
array('i', [polygon.vertices[k]]).tofile(file)
file.close()
return {'FINISHED'}
Reply with quote


CoDEmanX

Posted: Sun Dec 16, 2012 1:57 am
Joined: 05 Apr 2009
Posts: 691
you need to multiply the object's world matrix with the mesh coordinate to get an absolute coord:

ob.matrix_world * me.vertices[#].co

but it's more efficient to use to_mesh(), which lets you apply modifiers and shape keys, take that new mesh and do Mesh.transform(ob.matrix_world). If you then access the verts, they will be already in global space.
_________________
I'm sitting, waiting, wishing, building Blender in superstition...
Reply with quote


stiv

Posted: Sun Dec 16, 2012 5:46 am
Joined: 05 Aug 2003
Posts: 3489
One thing that noobs find confusing is that blender objects have two parts - Object and ObjData:

* The Object part holds the spatial information for the object - the location, rotation, scale . This is the world transformation matrix.

* The ObjData part holds the geometry - in the case of a Mesh, this is the verts, edges and faces that make up the object.

Each object has its own Object part, but a single ObjData can be shared among many Objects (linked duplicates)


The other helpful bit is the Code tag here on the forums which keeps phpBB from eating your Python formatting.
Reply with quote


rodrigoloc

Posted: Mon Dec 17, 2012 1:17 am
Joined: 16 Dec 2012
Posts: 2
Thanks you two. It works perfectly.

Here's the code:
mesh = d.meshes[i]
mesh.transform(d.objects[mesh.name].matrix_world)
Reply with quote


CoDEmanX

Posted: Mon Dec 17, 2012 6:26 am
Joined: 05 Apr 2009
Posts: 691
note that this will change the original mesh!

if you don't want that, use a copy of the mesh and remove it afterwards
_________________
I'm sitting, waiting, wishing, building Blender in superstition...
Reply with quote


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