如果在后台执行脚本,如何在 Blender Python 上使用旋转运算符?

How to use the rotate operator on Blender Python if you execute the script on the background?

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

我试过这样做,但似乎不起作用。

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() failed, context is incorrect

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

提前致谢! 我正在使用 Blender 2.9。

我运行遇到同样的问题。我有一些脚本在使用 bpy.ops.transformm.rotate 的 blender 2.83 as module 中工作得很好,现在这不适用于新的 bpy(blender as module)版本 2.93.

我意识到 bpy.ops.transform.rotate.poll() return 错误使用模块,来自 python 脚本,而函数 bpy.ops.transform.translate.poll() return 是正确的。

然而,当我 运行 在 blender 2.93 GUI 的脚本控制台中执行相同的函数时,函数 bpy.ops.transform.rotate.poll() return 为真。

所以我认为是新版本的一个bug。

但是我能够解决这个传递 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'}