Skip to content

Commit

Permalink
Feature: make_container_publishable can convert container to current …
Browse files Browse the repository at this point in the history
…asset. (#54)

It keeps the subset name but changes the asset to the current one. This is useful to 'make local' OP containers and use them as instances for the current asset.
- Also fixes the way collections are nested or converted
- Added a missing docstring
  • Loading branch information
Tilix4 authored and kaamaurice committed Mar 13, 2023
1 parent 0d0b244 commit 6412d12
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
37 changes: 30 additions & 7 deletions openpype/hosts/blender/api/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,11 @@ class SCENE_OT_MakeContainerPublishable(bpy.types.Operator):
name="Container to make publishable"
)

convert_to_current_asset: bpy.props.BoolProperty(
name="Convert container to current OpenPype asset",
description="It changes the asset but keeps the subset name"
)

# NOTE cannot use AVALON_PROPERTY because of circular dependency
# and the refactor is very big, but must be done soon

Expand All @@ -968,6 +973,7 @@ def draw(self, context):
layout.prop_search(
self, "container_name", context.scene, "openpype_containers"
)
layout.prop(self, "convert_to_current_asset")

def execute(self, context):
if not self.container_name:
Expand Down Expand Up @@ -996,15 +1002,32 @@ def execute(self, context):
break

# Create instance
bpy.ops.scene.create_openpype_instance(
creator_name=creator_name,
asset_name=avalon_data["asset_name"],
subset_name=avalon_data["name"],
datablock_name="",
gather_into_collection=isinstance(
create_args = {
"creator_name": creator_name,
"asset_name": legacy_io.Session["AVALON_ASSET"]
if self.convert_to_current_asset
else avalon_data["asset_name"],
"subset_name": avalon_data["name"],
"gather_into_collection": isinstance(
container.outliner_entity, bpy.types.Collection
),
)
}
if container.outliner_entity:
# For collection, allow renaming
if isinstance(container.outliner_entity, bpy.types.Collection):
# TODO may not be a good design because this value is from source workfile...
container.outliner_entity.is_openpype_instance = False

create_args.update(
{
"datapath": BL_TYPE_DATAPATH.get(
type(container.outliner_entity)
),
"datablock_name": container.outliner_entity.name,
}
)

bpy.ops.scene.create_openpype_instance(**create_args)

# Add datablocks to instance
new_instance = openpype_instances[-1]
Expand Down
18 changes: 15 additions & 3 deletions openpype/hosts/blender/api/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,23 @@ class Creator(LegacyCreator):
def _filter_outliner_datablocks(
datablocks: List,
) -> Tuple[Set[bpy.types.Collection], Set[bpy.types.Object]]:
"""Filter datablocks to return in two sets collections and objects.
Args:
datablocks (List): Datablocks to filter.
Returns:
Tuple[Set[bpy.types.Collection], Set[bpy.types.Object]]:
(collections, objects)
"""
collections = set()
objects = set()
for block in datablocks:
# Collections
if type(block) is bpy.types.Collection:
if isinstance(block, bpy.types.Collection):
collections.add(block)
# Objects
elif type(block) is bpy.types.Object:
elif isinstance(block, bpy.types.Object):
objects.add(block)
return collections, objects

Expand Down Expand Up @@ -474,7 +483,6 @@ def _process_outliner(
) and not collections_as_list[0].is_openpype_instance:
container_collection = collections_as_list[0]
container_collection.name = collection_name # Rename
collections.clear() # Remove it from collections to link
else:
container_collection = bpy.data.collections.new(
collection_name
Expand All @@ -484,6 +492,10 @@ def _process_outliner(
)
container_collection.is_openpype_instance = True

# Remove container collection from collections to link
if container_collection in collections:
collections.remove(container_collection)

# Set color tag
if self.color_tag and hasattr(container_collection, "color_tag"):
container_collection.color_tag = self.color_tag
Expand Down

0 comments on commit 6412d12

Please sign in to comment.