B3.2 addon release: duplicate an object's full hierarchy including hidden-in-viewport children

Hi all :slight_smile:

while my render is running i’m playing around with the things i often need in blender.

Recently i dreamt of a real hierarchy handling in blender ( the existing one is just a joke ) and especially a hierarchy duplication.

here and there on the web, i found some small pieces of python and i finally built this:

import bpy

bl_info = {
    "name": "Hierarchy duplicate",
    "author": "",
    "version": (0, 1),
    "blender" : (3, 2, 0),
    "location": "View3D > Object",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "3D View"
}


class OBJECT_OT_DuplicateHierarchy(bpy.types.Operator):
    """Duplicate object hierarchy including hidden objects"""
    bl_idname = "object.hierarchy_dup"
    bl_label = "Duplicate objects hierar."
    bl_options = {'REGISTER', 'UNDO'}

    def CheckHiddenStatus(self,p):
        for c in p.children:
            c['memoHIV'] = False
            if(bpy.data.objects[c.name].hide_viewport == True):
                bpy.data.objects[c.name].hide_viewport = False
                c['memoHIV'] = True
            c.select_set(True)
            self.CheckHiddenStatus(c) # recurse and save...

    def RestoreHiddenStatus(self,p):
        for c in p.children:
            if(c['memoHIV'] == True):
                bpy.data.objects[c.name].hide_viewport = True
            self.RestoreHiddenStatus(c) # recurse and restore

    #---------------------------------------------------

    def execute(self, context):
        
        #get our selected parent object
        p1 = bpy.context.active_object

        #iterate through children and make selectable and select them                
        self.CheckHiddenStatus(p1)

        # duplicate the whole hierarchy
        bpy.ops.object.duplicate()

        p2Name = ""

        #iterate through selected objects to find duplicated parent object
        for obj in bpy.context.selected_objects:
            if obj.parent is None:
                p2Name = obj.name

        #iterate through original group and disable selection
        self.RestoreHiddenStatus(p1)

        #iterate through duplicated group and disable selection       
        p2 = bpy.data.objects[p2Name]
        self.RestoreHiddenStatus(p2)
        
        bpy.ops.transform.translate('INVOKE_DEFAULT')

        
        return {'FINISHED'}




def menu_entry(self, context):
    self.layout.operator(OBJECT_OT_DuplicateHierarchy.bl_idname)



addon_keymaps = []

def register():
    bpy.utils.register_class(OBJECT_OT_DuplicateHierarchy)
    bpy.types.VIEW3D_MT_object.append(menu_entry)    
    
    # Add the hotkey
    wm = bpy.context.window_manager
    kc = wm.keyconfigs.addon
    if kc:
        km = wm.keyconfigs.addon.keymaps.new(name='3D View', space_type='VIEW_3D')
        kmi = km.keymap_items.new(OBJECT_OT_DuplicateHierarchy.bl_idname, type='D', value='PRESS', ctrl=True)
        addon_keymaps.append((km, kmi))


def unregister():
    bpy.utils.unregister_class(OBJECT_OT_DuplicateHierarchy)
    
    # Remove the hotkey
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)
    addon_keymaps.clear()
    
    bpy.types.VIEW3D_MT_object.remove(menu_entry)


if __name__ == "__main__":
    register()
    
#===========================


Be sure i dont know at all what i do about the blender hooking part. The bl_info is kind of voodoo to me.
Also, this lil source should deserve beeing a small addon, adding a menu entry in the 3DView object menu but i got no idea on how to do this…
It only adds a shortcut CTRL+D

What it does is really simple:
it duplicates the whole hierarchy ( including hidden objects ) of a selected object.
Therefore no need

  • to activate your 53 hidden children
  • select the 230 children organized in 10 children objects, each parent of 23…
  • copy the thing after beeing happy of the time lost
  • loose long again to deactivate the 53 children that should be deactivated :rofl:

The lil piece of code recurses through the children tree and do all for you :slight_smile:

Am not sure this is interresting for anyone as i saw no question here, about hierarchy-and-hidden-children duplication. At least it will be usefull for me and my defective memory :stuck_out_tongue:

At last but not least, here’s a blend of the thing:
run the script, select ‘cube’ wich is parent of a hidden-in-viewport ‘cube.002’, hit CTRL+D and voilà :smiley:
dup_hierarchy.blend (2.4 MB)

Hope you like it !

Happy blending !

2 Likes

Finally i made a .py file that can be loaded from preferences->addons

:smiley:

The process is simple enough for me to understand it :rofl:

hierar_dup.py (2.9 KB)

happy blending !

2 Likes

I got a question though about ‘hierarchy-dup’.

When you SHIFT+D an object, the clone gets attached to the mouse pointer so that it is placed where the user wants.

I guess this is also possible to attach the ‘hierarchy-dup’ parent to the mouse, the same way but i got no idea on how to do this.

Could anyone please tell me ?

Thanks and happy blending ! :smiley:

EDIT: finally i got it :smiley:

bpy.ops.transform.translate(‘INVOKE_DEFAULT’)

here’s the later version:
hierarchy_duplicate.py (3.0 KB)

EDIT2: lol ! amm addon developper ? :rofl: WTF ? i almost don’t know python at all !!! :crazy_face:

1 Like