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)

7 comments:

  1. I tried this one on Blender, looks like this command bpy.ops.object.select_name is no longer existed?

    ReplyDelete
  2. That's right, .select_name was removed. in 2.6. I will update the post to be 2.6 compatible. Thanks for the heads up Jimmy!

    ReplyDelete
    Replies
    1. Thanks Dealga, will check the update. Maybe just add the update at the bottom so people can see the changes.

      This is truly one-stop Blender Blog for Python :) I am truly blown away. I am still beginner in Python scripting, but will study your blog. Hopefully one day I can understand all of them.

      The variety of scripts here are brilliant. Thanks for sharing all this knowledge.

      Delete
    2. While blogspot is pretty good, things like this don't seem to be possible after overwriting an original post. Oh well, lesson learned.

      I might consider dedicated hosting solutions. But it's just for fun, so I don't know :)

      Delete
  3. I cannot find a way to group objects without first selecting them. Is this your experience as well? It seems rather messy to have to clear the selection, select an object, and then add the object to a group when it would be much cleaner to just add the name to a group procedurally. Unfortunately, I cannot find a way to do this in Blender.

    Sometimes I deal with creating thousands of objects and it seems to slow down a bit with all the selecting/deselecting, but maybe it's just my imagination...

    ReplyDelete
    Replies
    1. what sticks out immediately is that my code here uses a bpy.ops call, a large consecutive run of calls to bpy.ops will be slow. I'll update this code tomorrow to avoid using bpy.ops -- which i think is possible. Thanks for bringing this to my attention.

      Delete
    2. I've added a short example of how to group objects without using bpy.ops to the end of the post, let me know if that solves the speed issue.

      Delete

Please use Blender.StackExchange.com for python scripting questions unrelated to this post.