I want something along the following lines:

import bpy
object = bpy.data.objects['Cube']
bpy.context.scene.objects.active = object
bpy.ops.transform.rotate(value=0.638031, axis=(-0.818828, 0.361665, -0.445779), constraint_axis=(False, False, False), constraint_orientation='GLOBAL', mirror=False, proportional='DISABLED', proportional_edit_falloff='SMOOTH', proportional_size=1)

But I still have to select the object manually. How can I make the object active using python?


Answer to your question

With python, it is better to not use bpy.ops at all, but to just manipulate it's rotation directly. To answer your question of how to make an object active in python:

bpy.context.scene.objects.active = some_obj

Better ways to rotate objects

After getting an object, e.g.,

obj = bpy.context.object

It would be much better to manipulate transforms directly though:

obj.rotation_euler.x += x_offset


obj.rotation_euler.rotate(Euler((x_offset, y_offset, z_offset)))


obj.rotation_euler = (x_value, y_value, z_value)


obj.rotation_euler.rotate_axis('X', math.radians(45))
  • 3
    $\begingroup$ Why should one avoid bpy.ops ? $\endgroup$
    – McLawrence
    Aug 28 '17 at 5:43
  • 5
    $\begingroup$ @McLawrence Mostly because of speed. When you call an operator, Blender will do a scene update before continuing. This can become very expensive especially if you are calling it in a repetitive manner, e.g., in a loop. Check out this post by one of the Blender developers on the site for more info. $\endgroup$
    – JakeD
    Aug 28 '17 at 23:32

Just a little update if you are searching for this for 2.80 and up:

The active object has moved from the scene to the new view layers system, as you can have multiple active objects across multiple view layers.

bpy.context.view_layer.objects.active = ob

As mentioned by mentalist in the comments below, you can also set the active objects to "None". (Because "None" is predefined in Python.)

bpy.context.view_layer.objects.active = None

Also see: Blender 2.8 API, python, set active object

  • 2
    $\begingroup$ Holy crap thank you so much. Was literally research this. $\endgroup$
    – Inkplay_
    Mar 25 '19 at 0:36
  • 2
    $\begingroup$ Offsite linking is not hated upon here, just that you summarize the important parts directly in your answer. $\endgroup$
    – JakeD
    Aug 29 '19 at 0:44
  • 1
    $\begingroup$ Also... bpy.context.view_layer.objects.active = None if wanting to ensure no objects are active. $\endgroup$
    – Mentalist
    Nov 14 '20 at 10:07
  • $\begingroup$ You are an awesome person. Thank you so much. $\endgroup$ Jul 21 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.