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 groups. Show all posts
Showing posts with label groups. Show all posts

July 06, 2011

Creating a Group with no objects

bpy.data.groups.new("alom")
won't appear in the outliner until it has objects associated with it.

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

May 14, 2011

Blender 2.5 Python Printing Groups and Objects and using Text Editor

# create a named text object 
>>> bpy.data.texts.new(name="dothistext")
bpy.data.texts["dothistext"]
py script to print names of groups in scene and their objects
import bpy

# print a divider
print("="*20)

for item in bpy.data.groups:
    print(item.name)
    for object in item.objects:
        print("--", object.name)
print() statements print to the debug console, if you don't know where that is. read this thread.
( Blender-2.57-User-preferences-and-the-console )

Useful for self when documenting what parts have been optimized manually. someone else might find a use for it. This script makes a textfile inside blender, which will be visible in the 'Text Editor' view.
import bpy
# creates a new text file, named "SceneList"
# writes Groups and contained Objects to it.

bpy.data.texts.new(name="SceneList")
newtext = bpy.data.texts["SceneList"] 
# or in one line
# newtext = bpy.data.texts.new(name="SceneList")

newtext.write("="*20 + "\n") # makes a divider

# your scene must contain groups. (groups are a collection of objects)
for item in bpy.data.groups:
    newtext.write(item.name + "\n")
    for object in item.objects:
        newtext.write("--" + object.name + "\n")
An example of reading a file from disk and writing it to a textobject:
# will create a text file called MyTextName (it will appear in the dropdown)
bpy.data.texts.new(name="MyTextName") 

# give give you a reference to that object, once created.
textobject = bpy.data.texts["MyTextName"] 

with open("yourtexttoread.txt", r) as readfile:
    for line in readfile:
        textobject.write(line)

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)