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...

May 14, 2011

Blender 2.6 Python Grouping Objects


Do this experiment with nothing selected. A scene with 3 objects : Cube1, Cube2, Cube3
>>> bpy.ops.group.create(name="myGroup")
{'FINISHED'} # will not appear in outliner yet.

>>> bpy.context.scene.objects['Cube1'].select = True
{'FINISHED'}

>>> bpy.ops.object.group_link(group="myGroup")
{'FINISHED'}

The result of the above code should be that the object becomes part of "myGroup" Now un-group it again, and un-select everything. If you want all of them added you could do:
import bpy

# # or if you want to select all mesh objects.
# objects_to_add = [obj.name for obj in bpy.data.objects if obj.type == 'MESH']

# I will populate a list in advance, the square brackets are optional here.
objects_to_add = ["Cube1", "Cube2", "Cube3"]
for name in objects_to_add:
    bpy.context.scene.objects[name].select = True
    bpy.ops.object.group_link(group="myGroup")    
    bpy.context.scene.objects[name].select = False
if myGroup didn't exist yet you would do:
import bpy

# not visible in outliner until objects are linked.
bpy.ops.group.create(name="myGroup") 

objects_to_add = "Cube1", "Cube2", "Cube3"
for name in objects_to_add:
    bpy.context.scene.objects[name].select = True
    bpy.ops.object.group_link(group="myGroup")    
    bpy.context.scene.objects[name].select = False
This version is probably the cleanest I can come up with for now. It contains a little bit more code that checks if the group exists yet.
import bpy

# extra checking, if myGroup doesn't already exist, create it.
# if you know your group doesn't exist, this is not needed.
# if you are unsure at runtime, then this makes sure you don't 
# create a myGroup.001 etc..
if not 'myGroup' in bpy.data.groups:
    bpy.ops.group.create(name="myGroup") 

# if you want to select all mesh objects.
objects_to_add = [obj.name for obj in bpy.data.objects if obj.type == 'MESH']

# this would populate a list in advance, the square brackets are optional here.
# objects_to_add = ["Cube1", "Cube2", "Cube3"]

scn = bpy.context.scene
for name in objects_to_add:
    scn.objects.active = scn.objects[name]
    bpy.ops.object.group_link(group="myGroup")    
  

Without bpy.ops

This example shows a way to insert items into a group without the use of bpy.ops and should be faster than the previous methods when applied to large object lists. You can verify in the outliner goups mode that they have indeed been added. Blender might not outline these objects with the grouped colour you would normally expect, this is a bug and should be easy to fix (if it hasn't already)