0
$\begingroup$

I'm trying to make a python script that will recursively rename the children in a hierarchy with a name derived after their parent, from a selected object.

Now I've gotten as far as I can select an object, run the script - and it will rename all the children accordingly.

The problem is that, if those children also has children of their own - I also want to rename those as well. But I cant find a way to rename the child of a child of an object.

I'm pretty new to python, but any solution or advice for what I should be looking at is much appreciated.

So heres what little I got so far:

    def execute(self, context):
    
    
    for child in bpy.context.object.children:
        
         
        #Rename child to "[ParentName]_[Type]"
        child.name = bpy.context.object.name + ("_") + child.type 

    return {'FINISHED'}
$\endgroup$
  • $\begingroup$ Share the script you got so far with this post please. $\endgroup$ – Leander Aug 24 at 10:56
  • $\begingroup$ oh sorry, I've added it $\endgroup$ – user104974 Aug 24 at 11:02
0
$\begingroup$

Extending a list of Children

You can make a list of children and extend the list with the children of each child. After renaming a children, it is removed from the list.

import bpy

def execute(context):
    ob = context.object
    
    if ob == None:
        return
    
    children = list(ob.children)
    index = 0
    while len(children):
        child = children.pop()
        child.name = ob.name + str(index).zfill(3)
        children.extend(child.children)
        index += 1
        
execute(bpy.context)

Recursively walking the Children

You can also create a function, which renames the children of an object (parameter) and calls itself for each of the children.

import bpy

def renameChildren(ob):
    for child in ob.children:
        child.name = ob.name + "_" + child.type
        renameChildren(child)

def execute(context):
    ob = context.object
    
    if ob == None:
        return
    
    renameChildren(ob)
        
execute(bpy.context)
| improve this answer | |
$\endgroup$
  • $\begingroup$ that's great! thank you very much! $\endgroup$ – user104974 Aug 24 at 12:11

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.