Remove unbaked object from viewlayer

This commit is contained in:
Spencer Killen 2022-12-25 13:04:00 -07:00
parent 071fbd8345
commit a83cf61256
Signed by: sjkillen
GPG Key ID: F307025B65C860BA
1 changed files with 19 additions and 4 deletions

View File

@ -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):