【问题标题】:Rotating a model in Blender Python exporter在 Blender Python 导出器中旋转模型
【发布时间】:2015-01-13 12:13:43
【问题描述】:

我正在编写模型导出器。我想在导出器脚本中将整个模型在 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 度

编辑:附上从 Blender 导出前后的屏幕截图:

【问题讨论】:

  • "如果模型包含多个网格,则网格位置不对" 能简单解释一下吗?

标签: python 3d rotation blender coordinate-systems


【解决方案1】:

终于找到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))   

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 2015-03-13
    • 2017-10-27
    • 2018-10-24
    • 2021-09-13
    • 2013-09-16
    • 2013-05-05
    • 1970-01-01
    • 2014-01-10
    相关资源
    最近更新 更多