1
$\begingroup$

This simple script rotates a monkey:

import bpy


def rotate_monkey(angle):

    # Deselect all.
    bpy.ops.object.select_all(action='DESELECT')
    
    # Select bpy.data.objects["Suzanne"].
    bpy.data.objects["Suzanne"].select_set(state=True)
    bpy.context.view_layer.objects.active = bpy.data.objects["Suzanne"]
    
    # Rotate bpy.data.objects["Suzanne"].
    bpy.ops.transform.rotate(value=angle, orient_axis='Z', orient_type='GLOBAL')
     
    # Deselect all.
    bpy.ops.object.select_all(action='DESELECT')


rotate_monkey(1)

It works just fine.

But when I try to run it with the handler I get an error:

import bpy


def rotate_monkey(angle):

    # Deselect all.
    bpy.ops.object.select_all(action='DESELECT')
    
    # Select bpy.data.objects["Suzanne"].
    bpy.data.objects["Suzanne"].select_set(state=True)
    bpy.context.view_layer.objects.active = bpy.data.objects["Suzanne"]
    
    # Rotate bpy.data.objects["Suzanne"].
    bpy.ops.transform.rotate(value=angle, orient_axis='Z', orient_type='GLOBAL')
     
    # Deselect all.
    bpy.ops.object.select_all(action='DESELECT')


bpy.context.scene.frame_current = 0
bpy.context.scene.frame_end = 5


def move(self, context):
    frame_numb = bpy.context.scene.frame_current
    rotate_monkey(frame_numb)

bpy.app.handlers.frame_change_pre.clear()    
bpy.app.handlers.frame_change_pre.append(move)


Traceback (most recent call last):
  File "C:\Users\XX\Documents\20220706_rotation.blend\animate.py", line 29, in move
  File "C:\Users\XX\Documents\20220706_rotation.blend\animate.py", line 14, in rotate_monkey
  File "C:\Program Files\Blender Foundation\Blender 3.0\3.0\scripts\modules\bpy\ops.py", line 132, in __call__
    ret = _op_call(self.idname_py(), None, kw)
RuntimeError: Operator bpy.ops.transform.rotate.poll() failed, context is incorrect

How to fix it?

$\endgroup$
2
  • 1
    $\begingroup$ For operators you need to ensure that the correct context is used (e.g. the area has to be a 3D View). In your case this can be easily fixed by not relying on operators and just rotating the object directly. blender.stackexchange.com/questions/167830/… $\endgroup$ Commented Jul 6, 2022 at 12:33
  • $\begingroup$ @RobertGützkow Thanks for the link. Found there what I need. $\endgroup$
    – Petr
    Commented Jul 6, 2022 at 17:14

1 Answer 1

1
$\begingroup$

Avoid using OPS in the scripts. To use OPS you should define the area where the operator is executed by replacing the context and other context values, that's not defined properly in API description.

You can read more about it here:
https://docs.blender.org/api/3.2/bpy.types.Context.html#bpy.types.Context.temp_override

And also in your script you select the object, so that you lose current selection.

But in this case, it is much easier without OPS:

import bpy

def rotate_monkey(angle):

    monkey = bpy.data.objects["Suzanne"]
    
    # Rotate bpy.data.objects["Suzanne"].
    monkey.rotation_euler.z = angle
     
bpy.context.scene.frame_current = 0
bpy.context.scene.frame_end = 5

def move(self, context):
    frame_numb = bpy.context.scene.frame_current
    rotate_monkey(frame_numb)

bpy.app.handlers.frame_change_pre.clear()    
bpy.app.handlers.frame_change_pre.append(move)
$\endgroup$
1
  • $\begingroup$ Got it. Thank you $\endgroup$
    – Petr
    Commented Jul 6, 2022 at 17:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .