简体   繁体   English

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

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

I am importing a model that consists of multiple individual meshes.我正在导入一个包含多个单独网格的 model。 Right after import (where everything is selected), I want to rotate the imported selected objects based on a [X, Y, Z] angle parameter.导入后(选择所有内容),我想根据 [X, Y, Z] 角度参数旋转导入的选定对象。 Also I want to run the script as a blender "--background" shell process.我还想将脚本作为搅拌机“--background”shell 进程运行。

I tried doing something like this but it doesn't seem to work.我尝试做这样的事情,但它似乎不起作用。

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');

I get this error:我收到此错误:

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

I tried searching the internet for solutions but I couldn't understand exactly what is going wrong.我尝试在互联网上搜索解决方案,但我无法确切了解出了什么问题。 Also I think this error doesn't appear because I am running with "--background", but because I am running it as a terminal command.另外我认为这个错误不会出现,因为我正在使用“--background”运行,而是因为我将它作为终端命令运行。

Thanks in advance.提前致谢。 I am using Blender 2.9.我正在使用搅拌机 2.9。

Im running the same issue.我运行同样的问题。 I have some scripts that worked so fine in blender 2.83 as module using bpy.ops.transformm.rotate, now this is not working on the new bpy (blender as module) version 2.93.我有一些脚本在 blender 2.83 中作为模块使用 bpy.ops.transformm.rotate 运行良好,现在这不适用于新的 bpy(blender 作为模块)版本 2.93。

I realized that bpy.ops.transform.rotate.poll() return false using the module, from python script, while the function bpy.ops.transform.translate.poll() returns true.我意识到bpy.ops.transform.rotate.poll()使用模块从 python 脚本返回 false,而 function bpy.ops.transform.translate.poll()返回 true。

However when I run the same function in the scripting console of the blender 2.93 GUI, the function bpy.ops.transform.rotate.poll() returns true.但是,当我在搅拌机 2.93 GUI 的脚本控制台中运行相同的 function 时,function bpy.ops.transform.rotate.poll()返回 true。

So I think is a bug in the new version.所以我认为是新版本中的一个错误。

However I was able to fix this passing a VIEW_3D context as first argument in the operator:但是,我能够解决此问题,将 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'}

In your case:在你的情况下:

# ... 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