繁体   English   中英

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

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

我正在导入一个包含多个单独网格的 model。 导入后(选择所有内容),我想根据 [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() 失败,上下文不正确

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

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

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

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

但是,当我在搅拌机 2.93 GUI 的脚本控制台中运行相同的 function 时,function 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'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM