I'm writing a model exporter. I want to rotate my whole model 90 degrees in X-axis in the exporter script. The problem is, if the model contains more than one mesh, the mesh positions are wrong. Here's a relevant section of my exporter:
objects = [Object for Object in context.selected_objects if Object.type in ("MESH")]
for obj in objects:
...
mat_x90 = mathutils.Matrix.Rotation(-math.pi/2, 4, 'X')
obj.data.transform(mat_x90)
object[ OBJ.MAT ] = obj.matrix_world.copy()
object[ OBJ.LOC ] = object[ OBJ.MAT ].to_translation()
object[ OBJ.ROT ] = object[ OBJ.MAT ].to_quaternion()
object[ OBJ.SCA ] = object[ OBJ.MAT ].to_scale()
object[ OBJ.UVL ] = None
...
for face in obj.data.tessfaces:
for vertex_id in (0, 1, 2):
vertex_pnt = self.get_vertex_pnt(object, obj.data, face, vertex_id)
mesh.vertices.append( vertex_pnt )
...
def get_vertex_pnt( self, obj_prop, mesh, face, face_vi ):
# position
co = obj_prop[ OBJ.LOC ] +
mathutils.Vector(obj_prop[OBJ.ROT] * mathutils.Vector([
mesh.vertices[face.vertices[face_vi]].co[0] * obj_prop[OBJ.SCA][0], \
mesh.vertices[face.vertices[face_vi]].co[1] * obj_prop[OBJ.SCA][1], \
mesh.vertices[face.vertices[face_vi]].co[2] * obj_prop[OBJ.SCA][2] \
]))
If I don't apply the 90 degrees rotation in the beginning of the loop, mesh positions are ok, but the model is 90 degrees rotated in X-axis which I don't want.
Basically, I want to to this operation in a script:
(done manually) Select all meshes you want to export
Press 'r' to activate rotation
Press 'x' and rotate 90 degrees
Edit: Attached a screenshot before and after exporting from Blender:
Finally found an answer: