From fb9dc77cd0366ea6744fda226a3edb190952a645 Mon Sep 17 00:00:00 2001 From: Vilem Duha Date: Sat, 31 Jul 2021 09:43:00 +0200 Subject: BlenderKit: hide extra objects when appending collection All extra objects that are appended thanks to their link through geometry nodes, bone shapes and others are now hidden to a new subcollection. --- blenderkit/append_link.py | 23 +++++++++++++++++++++-- blenderkit/utils.py | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/blenderkit/append_link.py b/blenderkit/append_link.py index 5b54a81b..56d37d54 100644 --- a/blenderkit/append_link.py +++ b/blenderkit/append_link.py @@ -302,17 +302,26 @@ def append_objects(file_name, obnames=[], location=(0, 0, 0), link=False, **kwar bpy.ops.object.select_all(action='DESELECT') path = file_name + "\\Collection\\" - object_name = kwargs.get('name') + collection_name = kwargs.get('name') fc = utils.get_fake_context(bpy.context, area_type='VIEW_3D') - bpy.ops.wm.append(fc, filename=object_name, directory=path) + bpy.ops.wm.append(fc, filename=collection_name, directory=path) return_obs = [] + to_hidden_collection = [] + collection = None for ob in bpy.context.scene.objects: if ob.select_get(): return_obs.append(ob) if not ob.parent: main_object = ob ob.location = location + # check for object that should be hidden + if ob.users_collection[0].name == collection_name: + collection = ob.users_collection[0] + else: + to_hidden_collection.append(ob) + + if kwargs.get('rotation'): main_object.rotation_euler = kwargs['rotation'] @@ -321,6 +330,16 @@ def append_objects(file_name, obnames=[], location=(0, 0, 0), link=False, **kwar main_object.parent = bpy.data.objects[kwargs['parent']] main_object.matrix_world.translation = location + #move objects that should be hidden to a sub collection + if len(to_hidden_collection)>0 and collection is not None: + hidden_collection_name = collection_name+'_hidden' + h_col = bpy.data.collections.new(name = hidden_collection_name) + collection.children.link(h_col) + for ob in to_hidden_collection: + ob.users_collection[0].objects.unlink(ob) + h_col.objects.link(ob) + utils.exclude_collection(hidden_collection_name) + bpy.ops.object.select_all(action='DESELECT') utils.selection_set(sel) diff --git a/blenderkit/utils.py b/blenderkit/utils.py index 113dc130..afaf747e 100644 --- a/blenderkit/utils.py +++ b/blenderkit/utils.py @@ -152,6 +152,28 @@ def get_selected_replace_adepts(): return parents +def exclude_collection(name, state=True): + ''' + Set the exclude state of collection + Parameters + ---------- + name - name of collection + state - default True. + + Returns + ------- + None + ''' + vl = bpy.context.view_layer.layer_collection + cc = [vl] + found = False + while len(cc) > 0 and not found: + c = cc.pop() + if c.name == name: + c.exclude = state + found = True + cc.extend(c.children) + def get_search_props(): scene = bpy.context.scene wm = bpy.context.window_manager -- cgit v1.2.3