2

我正在导入一个包含多个单独网格的模型。导入后(所有内容都被选中),我想根据 [X, Y, Z] 角度参数旋转导入的选定对象。我还想将脚本作为搅拌机“--background”外壳进程运行。

我尝试做这样的事情,但它似乎不起作用。

bpy.ops.transform.rotate(value=math.radians(param.x), orient_axis='X'); bpy.ops.transform.rotate(value=math.radians(param.y), orient_axis='Y'); bpy.ops.transform.rotate(value=math.radians(param.z), orient_axis='Z');

我收到此错误:

RuntimeError: Operator bpy.ops.transform.rotate.poll() 失败,上下文不正确

我尝试在互联网上搜索解决方案,但我无法确切了解出了什么问题。另外我认为这个错误不会出现,因为我正在使用“--background”运行,而是因为我将它作为终端命令运行。

提前致谢!我正在使用搅拌机 2.9。

4

1 回答 1

6

我运行同样的问题。我有一些脚本在 blender 2.83 中作为模块使用 bpy.ops.transformm.rotate 运行良好,现在这不适用于新的 bpy(blender 作为模块)版本 2.93。

我意识到bpy.ops.transform.rotate.poll()使用模块从python脚本返回false,而函数bpy.ops.transform.translate.poll()返回true。

但是,当我在 blender 2.93 GUI 的脚本控制台中运行相同的函数时,该函数bpy.ops.transform.rotate.poll()返回 true。

所以我认为是新版本中的一个错误。

但是,我能够解决此问题,将 VIEW_3D 上下文作为运算符中的第一个参数传递:

>>> ov=bpy.context.copy()
>>> ov['area']=[a for a in bpy.context.screen.areas if a.type=="VIEW_3D"][0]
>>> bpy.ops.transform.rotate(ov)
{'FINISHED'}

在你的情况下:

# ... already selected objects, ov is for override, I'm lazy.
>>> ov=bpy.context.copy()
>>> ov['area']=[a for a in bpy.context.screen.areas if a.type=="VIEW_3D"][0]
>>> bpy.ops.transform.rotate(ov, value=math.radians(param.x), orient_axis='X')
{'FINISHED'}
于 2021-05-26T01:14:37.420 回答