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
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'}
Getting mesh's transformation
Moderators: jesterKing, stiv
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.
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...
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.
* 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.
-
- Posts: 0
- Joined: Sun Dec 16, 2012 12:08 am