24
$\begingroup$

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?

$\endgroup$

2 Answers 2

43
$\begingroup$

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:

2.8+

bpy.context.view_layer.objects.active = some_obj

2.7x

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

Or

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

Or

obj.rotation_euler = (x_value, y_value, z_value)

Or

obj.rotation_euler.rotate_axis('X', math.radians(45))
$\endgroup$
10
  • 5
    $\begingroup$ Why should one avoid bpy.ops ? $\endgroup$
    – McLawrence
    Commented Aug 28, 2017 at 5:43
  • 7
    $\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$
    – Jake Dube
    Commented Aug 28, 2017 at 23:32
  • 2
    $\begingroup$ bpy_prop_collection: attribute "active" not found $\endgroup$
    – Phil
    Commented May 11, 2022 at 16:21
  • $\begingroup$ @Phil See answer below. This was written in 2017 for an older version of Blender's API $\endgroup$
    – Jake Dube
    Commented May 11, 2022 at 16:32
  • $\begingroup$ I disagree that it's better not to use bpy.ops. By default, code should be optimized for coding speed vs. execution speed. Only if performance becomes noticeably bad should one look into low-level optimization. $\endgroup$
    – sw1337
    Commented Jun 19, 2022 at 23:47
77
$\begingroup$

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

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

You must log in to answer this question.

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