2
$\begingroup$

Today I'm able to select all parameters that I need. But select a object it's pretty simple, using name it's ok.

But I use a very complex hierarchy in blender. As you can see just below:

enter image description here

Thereby I start with a simpler example and try to created a script just to print each object with their name and save the hierarchy, but some difficulties appears to me.

First I start to use type and be sure to conserve the hierarchy.

I decide to use an Empty as a "structure node".

So if I come across a Mesh I just print its name and if it's "EMPTY" I print its name. After printing all the mesh's names I check to see if another Empty is in the same block. I then go to the next one and do the same things. I do this for the entire group, it's a recursive function I think.

This is my Outliner:

enter image description here

But I have two difficulties:

First, I try this script to print the object, but didn't work well. I'm a newbi sorry if there is mistake. Could you help me to understand why it doesn't work?

enter image description here

Secondly, I tried something simpler, just to print by type, starting from the first one to the second one, but it seems to print by alphabetical order. I don't know what happened.

enter image description here

I've tried so many things in a week and now I'm here for your help. Could you take the time to help?

I am at your disposal for further information.

EDIT 25/07/2019

Hello evvery one,

So i try myself to find a solution, and it's pretty hard for me .

I created two function to created a list with all original parents

def explo_hie(id_scene): 
    global memoire_1
    global generation_1
    memoire_1=[]
    generation_1=[]
    for o in bpy.data.scenes[id_scene].objects:
        memoire_1.append(o.name) 
    ##
    k = len(memoire_1)
    ##
    for i in range(0,k) :
        if bpy.data.objects[memoire_1[i]].parent == None :
            generation_1.append(memoire_1[i])
            print("Add")
        else :
            print("Do nothing because has parent")
    ##
    print("First genration or origal parents",generation_1)
    ##
    w = len(generation_1)
    ##   
    for a in range (0,w):
        memoire_1.remove(generation_1[a])       
    ##
    print(memoire_1)

explo_hie("Scene")

And it's work pretty weel i'm pround of me, but i think there is some upgrades to do.

But my probleme is over the looping, because as you can see i created a code to check types of objects :

################       
#Step One 
################

# We need to knwo the len of the generation_1
#This list contains the first object of our generation
k = len(generation_1) 

#I scan all object of this list
#For each index i check the type           
for i in range(0,k) :
    type = bpy.data.objects[generation_1[i]].type
    if type == 'MESH': 
        print("Func grab mesh",type)
    elif type == 'EMPTY':
        print("It's a structural node",type)
    else :
        print("User you don't respect rules",type))

But as you can see it's note a reccursive fonction so i tried to do this :

################       
#Step Two 
################

# We need to knwo the len of the generation_1
#This list contains the first object of our generation
k = len(generation_1) 

#I scan all object of this list
#For each index i check the type           
for i in range(0,k) :
    type = bpy.data.objects[generation_1[i]].type
    if type == 'MESH': 
        print("Func grab mesh")
    elif type == 'EMPTY':
        print("It's a structural node",type)
        #But if it's a structural noed i need to check if it has children 
        nbr_enfants = bpy.data.objects[generation_1[i]].children
        if len(nbr_enfants) == 0 :
            print("Pas d'enfants fonction get info")
        else :
            print(nbr_enfants)
            a=len(nbr_enfants)
            for o in range(0,a) :
                e = nbr_enfants[o]
                print()
                type = bpy.data.objects[e].type
                if type == 'MESH': 
                    print("Fonction grab mesh")
                elif type == 'EMPTY':
                    print("reccursive func")
                else :
                    print("User you don't respect rules",type)         
    else :
        print("User you don't respect rules",type))

But i'm not able to created a the reccursive function ... and to be honnest i don't really understand how to do it. I try some simple exemple for algebric operation and i achiv it but for blender i don't know how to do it. Could you help me ?

$\endgroup$
  • $\begingroup$ scene.objects is all objects in scene. Use this if you are interested in all objects in a scene. ob.children is a list of all immediate child objects. $\endgroup$ – batFINGER Jul 17 at 16:28
  • $\begingroup$ Hi. Please explain it "didn't work well" in greater detail. What didn't go well? What happened? Use the edit link below your question to add more details. $\endgroup$ – Ray Mairlot Jul 17 at 16:47
  • $\begingroup$ Edit 25/07: I add infos about my work $\endgroup$ – Marcus Jul 25 at 9:55
1
$\begingroup$

Printing the hierarchy

Note sure if this totally answers your question, but it shows how to traverse a hierarchy recursively. Code similar from

How do I copy children of an object using copy() method, without messing up their positions?

and Show and Hide child objects along with Parent

Test script to recursively run thru the hierarchy. Change the levels keyword argument to go deeper than 10.

import bpy
scene = bpy.context.scene

def print_heir(ob, levels=10):
    def recurse(ob, parent, depth):
        if depth > levels: 
            return
        print("  " * depth, ob.name)

        for child in ob.children:
            recurse(child, ob,  depth + 1)
    recurse(ob, ob.parent, 0)

root_obs = (o for o in scene.objects if not o.parent)

for o in root_obs:
    print_heir(o)

enter image description here

 Lamp
 Camera
   Cube.003
     Cube
       Cube.001
       Cube.005
 Cube.002
 Cube.004
$\endgroup$
  • $\begingroup$ Thx a lot, you help me so much and give me more informations to learn and trying to improve my knowledges :-) $\endgroup$ – Marcus Jul 25 at 13:18
  • 1
    $\begingroup$ Cheers. IMO the Blender API is a great way to learn python. And btw well put together question with clear info on what you want to achieve, and what you have tried so far. Well worth and deserving of another upvote or two ... .keep plugging away. $\endgroup$ – batFINGER Jul 27 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.