0
$\begingroup$

I'm trying to select and rotate an object in a frame_change_pre-handler. Here is the function:

def frame_handler(self, context, dummy):
    self.max_frame = 200
    slow = 45
    axis = "X"
    rotVal = -slow
    scn = context   #->instead of bpy.context.scene, which I tried before     
    frm = scn.frame_current
    print(f"current  frame: {scn.frame_current}") #-> this works correct
    
    cup = bpy.data.objects["cup"] 
    print(f"cup name = {cup.name}") # -> this works fine!

    try:

      if ((scn.frame_current > 1) &  (scn.frame_current % 5) == 0):
          print("starting cup  rotation")

          bpy.ops.object.select_all(action='DESELECT')

          cup.select_set(True)
          bpy.context.view_layer.objects.active = cup

          bpy.ops.transform.rotate(value=math.radians(rotVal), orient_axis=axis)
         
         
          bpy.ops.anim.keyframe_insert_byx_name(type="Rotation")
          print("cup  rotated")
        
          bpy.context.view_layer.update()
    except Exception as e:
        print (f"error {e}")
        

The line with bpy.ops.transform.rotate gives the error:

error Operator bpy.ops.transform.rotate.poll() failed, context is incorrect

My blender version is 3.1.

Is there maybe a simpler way to rotate an object in blender 3 (like cup.rotate(x,y,z) or so)?

Thank you!

I made a blend-file with some minor code changes:

Instead of: bpy.ops.transform.rotate(value=math.radians(rotVal), orient_axis=axis)

I tried this: cup.rotation_euler.rotate_axis('X',math.radians(rotVal ))

but the error still stays the same.

edit: maybe I could ask even more precise: what would be the correct context and how can I set it to the correct context, so that blender executes the rotation as desired?

$\endgroup$
2
  • $\begingroup$ You should modify the object matrix directly instead of using bpy.ops $\endgroup$
    – X Y
    Sep 22, 2022 at 4:05
  • $\begingroup$ Thank you. What is the python statement for that? $\endgroup$
    – Mike75
    Sep 22, 2022 at 6:53

1 Answer 1

0
$\begingroup$

I was wrong in one point: the error was not the same with the modified statements, because I just put the statements into a wrong function.

So this statements

 cup = bpy.data.objects["cup"] 
 cup.rotation_euler.rotate_axis('X',math.radians(rotVal ))    

already execute the rotation as desired and do not lead into a context problem.

It would be worth to find out anyway, why bpy.ops.transform.rotate leads into an error, but this is another question.

I made an update of the blender-file:

which works fine now.

$\endgroup$

You must log in to answer this question.

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