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

zac*_*per 3 python scripting blender bpy

我正在导入一个由多个单独的网格组成的模型。导入后(选择所有内容),我想根据 [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:运算符 bpy.ops.transform.rotate.poll() 失败,上下文不正确

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

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

osk*_*nas 9

我正在运行同样的问题。我有一些脚本在 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'}
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

# ... 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'}
Run Code Online (Sandbox Code Playgroud)