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

3 comments:

  1. Hi,

    It's not a big deal...
    I think No.3 source has bug, don't you?
    Line 6 is
    newtext = bpy.data.texts["dothistext"]
    V
    newtext = bpy.data.texts["SceneList"]

    Kenichi

    ReplyDelete

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