From a83cf612561b5817adbbd4419235de2af6bf5d04 Mon Sep 17 00:00:00 2001 From: Spencer Killen Date: Sun, 25 Dec 2022 13:04:00 -0700 Subject: [PATCH] Remove unbaked object from viewlayer --- mesh_baking/tool.py | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/mesh_baking/tool.py b/mesh_baking/tool.py index 2507221..90fae58 100644 --- a/mesh_baking/tool.py +++ b/mesh_baking/tool.py @@ -1,5 +1,5 @@ import bpy -from bpy.types import Context, Object, Scene, Collection +from bpy.types import Context, Object, Scene, Collection, ViewLayer, LayerCollection def get_first_collection_sharing_scene(scene: Scene, obj: Object): def collection_in_scene(collection: Collection, tree: Collection): @@ -8,6 +8,14 @@ def get_first_collection_sharing_scene(scene: Scene, obj: Object): return any(collection_in_scene(collection, child) for child in tree.children) return next(candidate for candidate in obj.users_collection if collection_in_scene(candidate, scene.collection)) +def collection_to_layer_collection(view_layer: ViewLayer, collection: Collection) -> LayerCollection: + wl = [view_layer.layer_collection] + while len(wl): + lc = wl.pop() + if lc.collection == collection: + return lc + wl.extend(lc.children) + def is_pc2_enabled(): return hasattr(bpy.ops, "export_shape") and hasattr(bpy.ops.export_shape, "pc2") @@ -31,7 +39,7 @@ def export_pc2(scene: Scene, obj: Object): bpy.ops.object.modifier_apply(modifier=modifier.name) bpy.context.view_layer.update() -def create_proxy(scene: Scene, obj: Object): +def create_proxy(view_layer: ViewLayer, scene: Scene, obj: Object): copy = obj.copy() copy.name = f"{obj.name}Bake" copy.data = copy.data.copy() @@ -41,7 +49,14 @@ def create_proxy(scene: Scene, obj: Object): export_pc2(scene, copy) mod = copy.modifiers.new("Mesh Bake", "MESH_CACHE") mod.cache_format = "PC2" - mod.filepath = export_pc2_filepath(obj) + mod.filepath = export_pc2_filepath(copy) + + c.objects.unlink(obj) + cc = bpy.data.collections.new(f"{obj.name}-unbaked") + cc.objects.link(obj) + c.children.link(cc) + lcc = collection_to_layer_collection(view_layer, cc) + lcc.exclude = True class BakeMeshOperator(bpy.types.Operator): bl_idname = "object.bakemesh" @@ -57,7 +72,7 @@ class BakeMeshOperator(bpy.types.Operator): return {'FINISHED'} scene = context.scene obj = context.active_object - create_proxy(scene, obj) + create_proxy(context.view_layer, scene, obj) return {'FINISHED'} def BakeMeshOperator_menu_func(self, context: Context):