3

I am importing a model that consists of multiple individual meshes. Right after import (where everything is selected), I want to rotate the imported selected objects based on a [X, Y, Z] angle parameter. Also I want to run the script as a blender "--background" shell process.

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

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.

Thanks in advance! I am using Blender 2.9.

1 Answer 1

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.

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.

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.

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:

>>> 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'}
Sign up to request clarification or add additional context in comments.

5 Comments

Man you are great! Helped me a lot, thank you! Have a nice day!
Ran into the exact same issue. For the record the script was working with Blender 2.90 and stopped working in Blender 2.93 (and also 2.92 - didn't try 2.91).
I can confirm in 2.91 the original worked. Mine also stopped in 2.93 ( didn't try 2.92). The fix is awesome! I wonder how you figured it out!
Also, when you said it was a bug, is this what blender.org intended, or is something require a fix from them, and is it already fixed in 3.x? I haven't found a bug report on their site. I haven't tried anything in Blender 3.x - which I will attempt that on my current python workflow after the summer.
In case you got the deprecation warning: area = [a for a in bpy.context.screen.areas if a.type=="VIEW_3D"][0] then put your code inside with bpy.context.temp_override(area=area):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.