Featured post

new redirect for blender.org bpy docs.

http://www.blender.org/api/blender_python_api_current/ As of 10/11 november 2015 we can now link to the current api docs and not be worr...

Showing posts with label Selecting Objects. Show all posts
Showing posts with label Selecting Objects. Show all posts

July 07, 2011

How to set active_object via python

a simple one liner!
"""using an object reference"""
>>> bpy.context.scene.objects.active = bpy.data.objects["Cube"]

# if checked to see which is now active
>>> bpy.context.scene.objects.active

# this may not update visibly as you might expect in the viewport, 
# but you will see the origin of active_object, and the name bottom left.
bpy.data.objects['Cube']

active != selected, you could have 10 objects selected, of which only 1 can ever be active at a time. Or none selected, but 1 active. To get a better understanding of this not so intuitive situation: unselect everything, if you have two objects in the scene, the last one interacted with will be 'active' (as in, shows the origin, and name displayed bottom left)

Setting the state of an object to 'select = True' , is not the same thing

Example usage of active object

as an example, below is code that adds Empties while an object is in Edit Mode, the script must set the active object back to the former object, after an empty is added. Try it with suzanne in Edit Mode, then select one vertex and run the script.

July 06, 2011

Hide_select, marking multiple objects to be unselectable


# with numerous objects selected, if you want to prevent their selection 'en masse'
for i in bpy.context.selected_objects:
i.hide_select = True

you might be interested in .hide and .hide_render

Selecting ungrouped objects


# a little verbose? :)
import bpy

scene_groups = bpy.data.groups

# make sure before this, that nothing is selected.
names_of_grouped_objects = []

for i in bpy.context.selectable_objects:
for group in bpy.data.groups:
for item in group.objects:
if item.name == i.name:
print("i.name", i.name, " is grouped in ", group.name)
names_of_grouped_objects.append(i.name)

# ungrouped objects
print("ungrouped objects are these")
for i in bpy.context.selectable_objects:
if i.name not in names_of_grouped_objects:
print(">>>",i.name)
bpy.data.objects[i.name].select = True