0
$\begingroup$

I want to get the names of all models in the blender in order

I use blender console for the first time

 objs = bpy.context.scene.objects
 for obj in objs :
  print(obj.name)
..... it is omitted
pasted__piriformis
pasted__puborectalis_part_of_levator_ani
pasted__prerectal_fibers_part_of_levator_ani
pasted__pubococcygeus_part_of_levator_ani
pasted__anal_sphincter_external
pasted__anal_sphincter_internal
pasted__iliococcygeus_part_of_evatorani
pasted__Deep_transverse_perineal
pasted__superficial_transverse_perineal
pasted__central_tendon_of_urogenital_diaphragm
pasted__anococcygeal_ligament

....it is omitted

>>> print(bpy.context.scene.objects)
<bpy_collection[844], SceneObjects>

It was successful but console line number limit is low. The console size itself is small, so only a fraction of the 844 is displayed on the console, and the displayed ones are quite small.

$\endgroup$

3 Answers 3

1
$\begingroup$

Okay it looks like there's two parts to this question, first is getting the information you're looking for (the blender objects sorted by hierarchy) and second is exporting that somewhere else (Excel).


So for the first part:

Since the objects are stored in a python dictionary, they aren't in any order at all. So you'll need to write a small script to search through the objects and put them in order.

Something like the following rough code will organize them putting the parent object first, followed by its children. Based on code found here.

def hierarchy(ob):
    objs = []
    def recurse(ob):
        objs.append(ob)
        for child in ob.children:
            recurse(child)
    recurse(ob)
    return objs

objs = []
for ob in (o for o in bpy.context.scene.objects if not o.parent):
    objs.extend(hierarchy(ob))

Though this doesn't account for the arrangement of collections.


For the second, if all you need is to view it in the console, you could print the list like so:

for ob in objs:
    print(ob.name)

If you aren't seeing enough of the output from your command, one option is to increase the console scrollback limit. This affects how far you can scroll up in the console using the mouse wheel. Go to Edit > Settings, then System > Memory & Limits > Console Scrollback Lines, and increase it to as much as you need.

Settings > System > Memory & Limits > Console Scrollback Lines

But it sounds like you're hoping it export this into a spreadsheet, in which case your best bet might be to directly export it to a csv file that Excel could then open.

For that, something like this should work:

import csv

with open('output.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    for ob in objs:
        writer.writerow([ob.name])

This will create a csv file in the current directory with each name on a new row, though it can be tweaked to create whatever spreadsheet you need.

$\endgroup$
1
$\begingroup$

because it's a dictionary

keys = bpy_collection[844]
values = SceneObjects

so you can easily get keys by this command line:

 print(bpy.context.scene.objects.keys())

python script:

import bpy
objs = bpy.context.scene.objects.keys()
for key in objs:
    print(key)

enter image description here

$\endgroup$
2
  • $\begingroup$ thak you very much but it is not suitable for putting in Excel ``` print(obj.name)```` don't print " ' " , " , " it automatically line breaks $\endgroup$ Aug 21, 2020 at 5:53
  • 3
    $\begingroup$ @yourHousebreaker You would get better answers if you put that in the question.. edit your question to include the output format you want. $\endgroup$
    – Robin Betts
    Aug 21, 2020 at 6:39
0
$\begingroup$
import bpy
import sys 

file = open("C:\WW.txt",'w')
sys.stdout=file

objs= bpy.context.scene.objects
for obj in objs:
    print(obj.name)
    
sys.stdout = sys.__stdout__ 
file.close()

but it wasn't in the order in hierarchy

$\endgroup$
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .