在Blender Python导出器中旋转模型

时间:2014-11-16 14:47:27

标签: python 3d rotation blender coordinate-systems

我正在写一个模型导出器。我想在导出器脚本中将整个模型在X轴上旋转90度。问题是,如果模型包含多个网格,则网格位置是错误的。这是我的出口商的相关部分:

    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] \
                                                                                    ]))

如果我不在循环开始时应用90度旋转,则网格位置正常,但模型在X轴上旋转90度,这是我不想要的。

基本上,我想在脚本中执行此操作:

  1. (手动完成)选择要导出的所有网格

  2. 按' r'激活轮换

  3. 按' x'并旋转90度

  4. 编辑:在从Blender导出之前和之后附上屏幕截图: before and after

1 个答案:

答案 0 :(得分:0)

最后找到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))