Getting mesh's transformation

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

rodrigoloc
Posts: 2
Joined: Sun Dec 16, 2012 12:08 am

Getting mesh's transformation

Postby rodrigoloc » Sun Dec 16, 2012 12:29 am

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'}

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Sun Dec 16, 2012 1:57 am

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

stiv
Posts: 3646
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Postby stiv » Sun Dec 16, 2012 5:46 am

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.

rodrigoloc
Posts: 2
Joined: Sun Dec 16, 2012 12:08 am

Postby rodrigoloc » Mon Dec 17, 2012 1:17 am

Thanks you two. It works perfectly.

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

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Mon Dec 17, 2012 6:26 am

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


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests