简体   繁体   中英

Rotating a model in Blender Python exporter

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:

  1. (done manually) Select all meshes you want to export

  2. Press 'r' to activate rotation

  3. Press 'x' and rotate 90 degrees

Edit: Attached a screenshot before and after exporting from Blender: 之前和之后

Finally found an answer :

def get_override(area_type, region_type):
    for area in bpy.context.screen.areas: 
        if area.type == area_type:             
            for region in area.regions:                 
                if region.type == region_type:                    
                    override = {'area': area, 'region': region} 
                    return override
    #error message if the area or region wasn't found
    raise RuntimeError("Wasn't able to find", region_type," in area ", area_type,
                    "\n Make sure it's open while executing script.")


#we need to override the context of our operator    
override = get_override( 'VIEW_3D', 'WINDOW' )
#rotate about the X-axis by 45 degrees
bpy.ops.transform.rotate(override, value=6.283/8, axis=(1,0,0))   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM