From 1aecfef38a5ab0220f9e0856984ff13961e12fdd Mon Sep 17 00:00:00 2001 From: Ondrej Samohel Date: Thu, 3 Mar 2022 17:02:51 +0100 Subject: [PATCH] add loaded containers to published instance --- .../hosts/maya/plugins/load/load_reference.py | 21 +++++++++ .../plugins/publish/extract_maya_scene_raw.py | 45 ++++++++++++++++++- .../defaults/project_settings/maya.json | 6 +++ .../schemas/schema_maya_publish.json | 24 ++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) diff --git a/openpype/hosts/maya/plugins/load/load_reference.py b/openpype/hosts/maya/plugins/load/load_reference.py index 0565b0b95c..859e1d339a 100644 --- a/openpype/hosts/maya/plugins/load/load_reference.py +++ b/openpype/hosts/maya/plugins/load/load_reference.py @@ -1,9 +1,11 @@ import os from maya import cmds from avalon import api +from avalon.pipeline import AVALON_CONTAINER_ID from openpype.api import get_project_settings from openpype.lib import get_creator_by_name import openpype.hosts.maya.api.plugin +from openpype.hosts.maya.api.pipeline import AVALON_CONTAINERS from openpype.hosts.maya.api.lib import maintained_selection @@ -125,6 +127,11 @@ class ReferenceLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): return new_nodes + def load(self, context, name=None, namespace=None, options=None): + super(ReferenceLoader, self).load(context, name, namespace, options) + # clean containers if present to AVALON_CONTAINERS + self._organize_containers(self[:]) + def switch(self, container, representation): self.update(container, representation) @@ -158,3 +165,17 @@ class ReferenceLoader(openpype.hosts.maya.api.plugin.ReferenceLoader): options={"useSelection": True}, data={"dependencies": dependency} ) + + @staticmethod + def _organize_containers(nodes): + # type: (list) -> None + for node in nodes: + id_attr = "{}.id".format(node) + if not cmds.attributeQuery("id", node=node, exists=True): + print("-" * 80) + print("skipping {}".format(node)) + continue + if cmds.getAttr(id_attr) == AVALON_CONTAINER_ID: + print("=" * 80) + print("moving {}".format(node)) + cmds.sets(node, forceElement=AVALON_CONTAINERS) diff --git a/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py b/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py index 9c432cbc67..591789917e 100644 --- a/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py +++ b/openpype/hosts/maya/plugins/publish/extract_maya_scene_raw.py @@ -6,6 +6,7 @@ from maya import cmds import openpype.api from openpype.hosts.maya.api.lib import maintained_selection +from avalon.pipeline import AVALON_CONTAINER_ID class ExtractMayaSceneRaw(openpype.api.Extractor): @@ -57,10 +58,22 @@ class ExtractMayaSceneRaw(openpype.api.Extractor): else: members = instance[:] + loaded_containers = None + if {f.lower() for f in self.add_for_families}.intersection( + {f.lower() for f in instance.data.get("families")}, + {instance.data.get("family").lower()}, + ): + loaded_containers = self._add_loaded_containers(members) + + selection = members + if loaded_containers: + self.log.info(loaded_containers) + selection += loaded_containers + # Perform extraction self.log.info("Performing extraction ...") with maintained_selection(): - cmds.select(members, noExpand=True) + cmds.select(selection, noExpand=True) cmds.file(path, force=True, typ="mayaAscii" if self.scene_type == "ma" else "mayaBinary", # noqa: E501 @@ -83,3 +96,33 @@ class ExtractMayaSceneRaw(openpype.api.Extractor): instance.data["representations"].append(representation) self.log.info("Extracted instance '%s' to: %s" % (instance.name, path)) + + @staticmethod + def _add_loaded_containers(members): + # type: (list) -> list + refs_to_include = [ + cmds.referenceQuery(ref, referenceNode=True) + for ref in members + if cmds.referenceQuery(ref, isNodeReferenced=True) + ] + + refs_to_include = set(refs_to_include) + + obj_sets = cmds.ls("*.id", long=True, type="objectSet", recursive=True, + objectsOnly=True) + + loaded_containers = [] + for obj_set in obj_sets: + + if not cmds.attributeQuery("id", node=obj_set, exists=True): + continue + + id_attr = "{}.id".format(obj_set) + if cmds.getAttr(id_attr) != AVALON_CONTAINER_ID: + continue + + set_content = set(cmds.sets(obj_set, query=True)) + if set_content.intersection(refs_to_include): + loaded_containers.append(obj_set) + + return loaded_containers diff --git a/openpype/settings/defaults/project_settings/maya.json b/openpype/settings/defaults/project_settings/maya.json index c25f416562..74ecf502d1 100644 --- a/openpype/settings/defaults/project_settings/maya.json +++ b/openpype/settings/defaults/project_settings/maya.json @@ -502,6 +502,12 @@ } } }, + "ExtractMayaSceneRaw": { + "enabled": true, + "add_for_families": [ + "layout" + ] + }, "ExtractCameraAlembic": { "enabled": true, "optional": true, diff --git a/openpype/settings/entities/schemas/projects_schema/schemas/schema_maya_publish.json b/openpype/settings/entities/schemas/projects_schema/schemas/schema_maya_publish.json index bf7b8a22e7..5c17e3db2c 100644 --- a/openpype/settings/entities/schemas/projects_schema/schemas/schema_maya_publish.json +++ b/openpype/settings/entities/schemas/projects_schema/schemas/schema_maya_publish.json @@ -547,6 +547,30 @@ "type": "schema", "name": "schema_maya_capture" }, + { + "type": "dict", + "collapsible": true, + "key": "ExtractMayaSceneRaw", + "label": "Maya Scene (Raw)", + "checkbox_key": "enabled", + "children": [ + { + "type": "boolean", + "key": "enabled", + "label": "Enabled" + }, + { + "type": "label", + "label": "Add loaded instances to those published families:" + }, + { + "key": "add_for_families", + "label": "Families", + "type": "list", + "object_type": "text" + } + ] + }, { "type": "dict", "collapsible": true,