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)

Useful for self when documenting what parts have been optimized manually. someone else might find a use for it.



import bpy

# creates a new text file, named "SceneList"
# writes Groups and contained Objects to it.
        
bpy.ops.text.new()
newtext = bpy.data.texts[-1] #  -1 is last added text file
newtext.name = "SceneList"

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

for item in bpy.data.groups:
    newtext.write(item.name + "
")
    for object in item.objects:
        newtext.write("--" + object.name + "
")

this version makes the textfile inside blender.

Hey, I’m Patrick’s twin and I can’t get this code to work. I’m not used to the Blender Environment. When I run the first code that uses print I don’t know where it prints to. As for the 2nd code it only prints “====================” in the code window and hides the code. I have the default cube object in the environment. I’m trying to print off all it’s vertices. If you can figure out what’s wrong I would greatly appreciate it. Also, it you know how to write what vertex is connected to other vertices that would be greatly appreciated as well. Thanks. :slight_smile:

hi,
thanks for the useful snippets,
I wonder if this would be any use adding to the .blend/scene information script?
:slight_smile:

@Meta-Androcto, there’s a slight problem with my approach to selecting what text-object to write to, here’s why.

bpy.ops.text.new()

creates a new text-object with default name “Text”, if it encounters an existing text-object with that name it does “Text.001”, etc. And that’s cool, it’s desired behaviour really. The problem of using [-1] in

bpy.data.texts[-1]

is that it seems to select from an ordered list, which means if the .blend already contains text-objects with names like “z_tranforms”, then [-1] will refer to “z_transforms” and not the newly created/instantiated text-object “Text or Text.001”.

A more elegant solution would be to overload bpy.ops.text.new() to also accept a name, that way it doesn’t break old scripts. So ideally i would write

bpy.ops.text.new("MyFile")

Thanks for your help, I got the code about working. :slight_smile: I’ve got it displaying all the vertices and I’m able to grab vertices from faces. Do you know how to display all the vertices that are connected/joined with a vertex?

Hi mikeytdan, that’s a good start then, happy to help.

Do you know how to display all the vertices that are connected/joined with a vertex?

Forgive me if this explanation presents things below your level of understanding; if it does, then i write this for the benefit of potential newcomers.

By now perhaps you realize that all vertices of an object have to be enumerated. Enumeration (in this sense) is a fancy way of saying they need an address. Let’s say you have a cube-mesh made from 8 vertices, no matter what programming language you use, you’ll see sometimes similar to:


vertlist[0] = Vector((x0,y0,z0))
vertlist[1] = Vector((x1,y2,z3))
...etc..
vertlist[7] = Vector((x7,y7,z7))

this is saying: “I have a collection of xyz coordinates, which I want to reference later on, so i should give it a number.” (Kind of like a postman knows where to deliver a letter if (and only if) you have the correct postal address on the front).

As soon as you talk about connecting vertices you are either making Edges or Faces, but definitely Edges. (because you can’t have faces without edges, but you can have edges without faces - like in a wire-frame/cage). Edges are described by collecting two vertices. A cube can be described as


Edges = [   
                [0,1],[1,2],[2,3],[3,0],
                [4,5],[5,6],[6,7],[7,4],
                [0,4],[1,5],[2,6],[3,7]
            ]

Your task would be to count how many times the vertex is referenced, in the above example: 1 is referenced in edges [0,1] , [1,2] …and lateron in [1,5]. This becomes a python exercise.


numbered_vertex = 1   # where 1 is an arbitrary choice of vertex
for i in Edges:
    if numbered_vertex in i:
        print(i)

If you are even remotely interested in these ‘behind the scenes’ concepts in computer graphics read the openGL redbook, which you will find at any respectable google search engine near you. Especially take a stroll down the first 5 chapters.

Thanks again for your help. I’ve been messing around with python trying to get it to display a list of the edges but I’m having no luck. I’ve tried:

import bpy

for item in bpy.data.objects:
    
    if item.type == 'MESH':
        print(item.data.edges);#<-- doesn't display them
        print(item.data.edges[1]);#<-- doesn't display them
        for e in item.data.edges:
            print(e);#<-- doesn't display them

Once I can have it print out the list of edges I will be able to do what I want with it.

Scratch what I said above, I got it working. :slight_smile: Here is the code:

import bpy

bpy.ops.text.new()
newtext = bpy.data.texts[-1]
newtext.name = "EdgesList"

for item in bpy.data.objects:
    
    newtext.write(item.name + "
")
    if item.type == 'MESH':
        newtext.write('var/edges_%s = "' % (item.name))
        for e in item.data.edges:
            newtext.write( '[%i, %i]' % (e.vertices[0] + 1, e.vertices[1] + 1));
        newtext.write('"
');

python is not a place for semicolons.


import bpy

for item in bpy.data.objects:
    if item.type == 'MESH':
        for e in item.data.edge_keys:
            print(e)

mikeytdan, just be careful with the


newtext = bpy.data.texts[-1]
newtext.name = "EdgesList"

i explained earlier in the thread what the pitfalls are. But if you only have one textfile you’ll probably not experience any problem.