From c260996522872a8bf56f74b52a598f61f9ba3b0e Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Fri, 19 Apr 2024 20:07:02 +0200 Subject: [PATCH 1/5] Implement `Reorder` helper for Maya --- client/ayon_core/hosts/maya/api/lib.py | 252 +++++++++++++++++++++++++ 1 file changed, 252 insertions(+) diff --git a/client/ayon_core/hosts/maya/api/lib.py b/client/ayon_core/hosts/maya/api/lib.py index 321bcbc0b5..e30070c796 100644 --- a/client/ayon_core/hosts/maya/api/lib.py +++ b/client/ayon_core/hosts/maya/api/lib.py @@ -6,6 +6,7 @@ from pprint import pformat import sys import uuid import re +import operator import json import logging @@ -4403,3 +4404,254 @@ def create_rig_animation_instance( variant=namespace, pre_create_data={"use_selection": True} ) + + +class Reorder(object): + """Helper functions for reordering in Maya outliner""" + + @staticmethod + def group_by_parent(nodes): + """Groups the given input list of nodes by parent. + + This is a convenience function for the Reorder functionality. + This function assumes the nodes are in the `long/fullPath` format. + """ + nodes = cmds.ls(nodes, long=True) + nodes_by_parent = defaultdict(list) + for node in nodes: + parent = node.rsplit("|", 1)[0] + nodes_by_parent[parent].append(node) + return nodes_by_parent + + @staticmethod + def get_children_with_index(parent): + """Get children under parent with their indices""" + def node_to_index(nodes): + return {node: index for index, node in enumerate(nodes)} + + if not parent: + return node_to_index(cmds.ls(assemblies=True, long=True)) + else: + return node_to_index( + cmds.listRelatives(parent, + children=True, + fullPath=True) or [] + ) + + @staticmethod + def get_index(node): + node = cmds.ls(node, long=True)[0] # enforce long names + parent = node.rsplit("|", 1)[0] + if not parent: + return cmds.ls(assemblies=True, long=True).index(node) + else: + return cmds.listRelatives(parent, + children=True, + fullPath=True).index(node) + + @staticmethod + def get_indices(nodes): + """Returns a dictionary with node, index pairs. + + This is preferred over get_index method for larger number of nodes, + because it is more optimal in performance. + + eg: + { + '|side': 3, + '|top': 1, + '|pSphere1': 4, + '|persp': 0, + '|front': 2 + } + + Returns: + dict: index by node + """ + nodes = cmds.ls(nodes, long=True) # enforce long names + node_indices = dict() + cached_children = dict() + for node in nodes: + parent = node.rsplit("|", 1)[0] + if parent not in cached_children: + cached_children[parent] = Reorder.get_children_with_index(parent) # noqa: E501 + + node_indices[node] = cached_children[parent][node] + return node_indices + + @staticmethod + def set_index(node, index): + if not node: + return + cmds.reorder(node, front=True) + cmds.reorder(node, r=index) + + @staticmethod + def set_indices(node_indices): + """Set node order by node to index dict. + + Args: + node_indices (dict): Node name to index dictionary + + """ + if not isinstance(node_indices, dict): + raise TypeError( + "Reorder.set_indices() requires a dictionary with " + "(node, index) pairs as input. " + "`{0}` is an invalid input type.".format( + type(node_indices).__name__) + ) + + if not node_indices: + return + + # force nodes to the back to not influence each other during reorder + cmds.reorder(node_indices.keys(), back=True) + + for node, index in sorted(node_indices.items(), + key=operator.itemgetter(1)): + Reorder.set_index(node, index) + + @staticmethod + def sort(nodes, key=lambda x: x.rsplit("|", 1)[-1], reverse=False): + """Sorts the node in scene by the key function. + + Default sorting key is alphabetically by using the object's short name. + """ + nodes = cmds.ls(nodes, long=True) # ensure long paths + if not nodes: + return + + # Group by parent to sort nodes per parent + parents = Reorder.group_by_parent(nodes) + + for child_nodes in parents.values(): + + node_indices = Reorder.get_indices(child_nodes) + indices = sorted(node_indices.values()) + + new_indices = { + node: indices[i] for i, node in + enumerate(sorted(child_nodes, key=key, reverse=reverse)) + } + Reorder.set_indices(new_indices) + + @staticmethod + def reverse(nodes): + nodes = cmds.ls(nodes, long=True) # ensure long paths + if not nodes: + return + + # Group by parent to sort nodes per parent + parents = Reorder.group_by_parent(nodes) + + for child_nodes in parents.values(): + + node_indices = Reorder.get_indices(child_nodes) + indices = sorted(node_indices.values(), reverse=False) + + iterable = enumerate(sorted(node_indices.items(), + key=operator.itemgetter(1), + reverse=True)) + new_indices = { + node: indices[i] for i, (node, _old_index) in iterable + } + Reorder.set_indices(new_indices) + + @staticmethod + def align_bottom(nodes): + """Reorder to the lowest (most back) of node in `nodes`.""" + nodes = cmds.ls(nodes, long=True) # ensure long paths + if not nodes: + return + + # Group by parent to sort nodes per parent + parents = Reorder.group_by_parent(nodes) + for child_nodes in parents.values(): + + # Reorder.set_index forces to front and then moves all objects + # together (so they will be stacked together). Then it applies the + # index as relative offset, we can use that here to our advantage. + # And it is a lot faster than Reorder.set_indices in that scenario. + index_per_node = Reorder.get_indices(child_nodes) + back_index = max(index_per_node.values()) + new_front_index = back_index - len(child_nodes) + 1 + Reorder.set_index(child_nodes, new_front_index) + + @staticmethod + def align_top(nodes): + """Reorder to the highest (most front) of node in `nodes`.""" + nodes = cmds.ls(nodes, long=True) # ensure long paths + if not nodes: + return + + # Group by parent to sort nodes per parent + parents = Reorder.group_by_parent(nodes) + for childNodes in parents.values(): + + # Reorder.set_index forces to front and then moves all objects + # together (so they will be stacked together). Then it applies the + # index as relative offset, we can use that here to our advantage. + # And it is a lot faster than Reorder.set_indices in that scenario. + index_per_node = Reorder.get_indices(childNodes) + front_index = min(index_per_node.values()) + Reorder.set_index(childNodes, front_index) + + @staticmethod + def move(nodes, relative, wrap=True): + """ Reorder by the given relative amount. """ + # TODO: Implement the disabling of wrapping around when at bottom. + if not nodes: + return + cmds.reorder(nodes, r=relative) + + @staticmethod + def to_bottom(nodes): + """Reorder to all the way to the bottom.""" + if not nodes: + return + cmds.reorder(nodes, back=True) + + @staticmethod + def to_top(nodes): + """Reorder to all the way to the top.""" + if not nodes: + return + cmds.reorder(nodes, front=True) + + @staticmethod + def order_to(nodes): + """Reorder the nodes to the order of the input list. + + Tip: + If you pass this your current selection list it will reorder + the nodes to the order of your selection. + + """ + nodes = cmds.ls(nodes, long=True) # ensure long paths + if not nodes: + return + + # Make a dictionary of the input order so we can optimize the look-up + # of the index in the order of the input `nodes`. + selected_order = {node: i for i, node in enumerate(nodes)} + + # Group by parent since we want to sort nodes under its current parent + parents = Reorder.group_by_parent(nodes) + for child_nodes in parents.values(): + + # Get the current indices + node_indices = Reorder.get_indices(child_nodes) + + # We get the original indices so we can position to those same + # positions, albeit with the new ordering of the nodes. + orig_indices = sorted(node_indices.values()) + + # Order the nodes by current selection (input list) and then apply + # the list of indices from `nodeIndices` in low-to-high order. + new_indices = dict( + zip(sorted(node_indices.keys(), + key=lambda x: selected_order[x]), + orig_indices) + ) + Reorder.set_indices(new_indices) From 3db28b74f18441d4db8fd60e3681b34041d7de37 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Fri, 19 Apr 2024 20:10:58 +0200 Subject: [PATCH 2/5] Refactor Maya load placeholder, fixes: - Implement fixes for outliner order of loaded placeholders (always place after placeholder) - Allow storing more complex data, e.g. EnumDef with multiselection=True - Change in behavior: This may fix a bug where previously placeholders may sometimes load only one subset when it should have loaded more. It could thus influence the loading behavior on existing templates - Implements a PlaceholderPlugin base class to be used by others potential plug-ins --- .../maya/api/workfile_template_builder.py | 159 +++++++++++- .../maya/plugins/template/load_placeholder.py | 234 ++++-------------- 2 files changed, 208 insertions(+), 185 deletions(-) diff --git a/client/ayon_core/hosts/maya/api/workfile_template_builder.py b/client/ayon_core/hosts/maya/api/workfile_template_builder.py index cfd416b708..d518d3933c 100644 --- a/client/ayon_core/hosts/maya/api/workfile_template_builder.py +++ b/client/ayon_core/hosts/maya/api/workfile_template_builder.py @@ -1,3 +1,5 @@ +import json + from maya import cmds from ayon_core.pipeline import ( @@ -8,13 +10,15 @@ from ayon_core.pipeline import ( ) from ayon_core.pipeline.workfile.workfile_template_builder import ( TemplateAlreadyImported, - AbstractTemplateBuilder + AbstractTemplateBuilder, + PlaceholderPlugin, + PlaceholderItem, ) from ayon_core.tools.workfile_template_build import ( WorkfileBuildPlaceholderDialog, ) -from .lib import get_main_window +from .lib import read, imprint, get_main_window PLACEHOLDER_SET = "PLACEHOLDERS_SET" @@ -86,6 +90,157 @@ class MayaTemplateBuilder(AbstractTemplateBuilder): return True +class MayaPlaceholderPlugin(PlaceholderPlugin): + """Base Placeholder Plugin for Maya with one unified cache. + + Creates a locator as placeholder node, which during populate provide + all of its attributes defined on the locator's transform in + `placeholder.data` and where `placeholder.scene_identifier` is the + full path to the node. + + Inherited classes must still implement `populate_placeholder` + + """ + + use_selection_as_parent = True + item_class = PlaceholderItem + + def _create_placeholder_name(self, placeholder_data): + return self.identifier.replace(".", "_") + + def _collect_scene_placeholders(self): + nodes_by_identifier = self.builder.get_shared_populate_data( + "placeholder_nodes" + ) + if nodes_by_identifier is None: + # Cache placeholder data to shared data + nodes = cmds.ls("*.plugin_identifier", long=True, objectsOnly=True) + + nodes_by_identifier = {} + for node in nodes: + identifier = cmds.getAttr("{}.plugin_identifier".format(node)) + nodes_by_identifier.setdefault(identifier, []).append(node) + + # Set the cache + self.builder.set_shared_populate_data( + "placeholder_nodes", nodes_by_identifier + ) + + return nodes_by_identifier + + def create_placeholder(self, placeholder_data): + + parent = None + if self.use_selection_as_parent: + selection = cmds.ls(selection=True) + if len(selection) > 1: + raise ValueError( + "More than one node is selected. " + "Please select only one to define the parent." + ) + parent = selection[0] if selection else None + + placeholder_data["plugin_identifier"] = self.identifier + placeholder_name = self._create_placeholder_name(placeholder_data) + + placeholder = cmds.spaceLocator(name=placeholder_name)[0] + if parent: + placeholder = cmds.parent(placeholder, selection[0])[0] + + self.imprint(placeholder, placeholder_data) + + def update_placeholder(self, placeholder_item, placeholder_data): + node_name = placeholder_item.scene_identifier + + changed_values = {} + for key, value in placeholder_data.items(): + if value != placeholder_item.data.get(key): + changed_values[key] = value + + # Delete attributes to ensure we imprint new data with correct type + for key in changed_values.keys(): + placeholder_item.data[key] = value + if cmds.attributeQuery(key, node=node_name, exists=True): + attribute = "{}.{}".format(node_name, key) + cmds.deleteAttr(attribute) + + self.imprint(node_name, changed_values) + + def collect_placeholders(self): + placeholders = [] + nodes_by_identifier = self._collect_scene_placeholders() + for node in nodes_by_identifier.get(self.identifier, []): + # TODO do data validations and maybe upgrades if they are invalid + placeholder_data = self.read(node) + placeholders.append( + self.item_class(scene_identifier=node, + data=placeholder_data, + plugin=self) + ) + + return placeholders + + def post_placeholder_process(self, placeholder, failed): + """Cleanup placeholder after load of its corresponding representations. + + Hide placeholder, add them to placeholder set. + Used only by PlaceholderCreateMixin and PlaceholderLoadMixin + + Args: + placeholder (PlaceholderItem): Item which was just used to load + representation. + failed (bool): Loading of representation failed. + """ + # Hide placeholder and add them to placeholder set + node = placeholder.scene_identifier + + cmds.sets(node, addElement=PLACEHOLDER_SET) + cmds.hide(node) + cmds.setAttr("{}.hiddenInOutliner".format(node), True) + + def delete_placeholder(self, placeholder): + """Remove placeholder if building was successful + + Used only by PlaceholderCreateMixin and PlaceholderLoadMixin. + """ + node = placeholder.scene_identifier + + # To avoid that deleting a placeholder node will have Maya delete + # any objectSets the node was a member of we will first remove it + # from any sets it was a member of. This way the `PLACEHOLDERS_SET` + # will survive long enough + sets = cmds.listSets(o=node) or [] + for object_set in sets: + cmds.sets(node, remove=object_set) + + cmds.delete(node) + + def imprint(self, node, data): + """Imprint call for placeholder node""" + + # Complicated data that can't be represented as flat maya attributes + # we write to json strings, e.g. multiselection EnumDef + for key, value in data.items(): + if isinstance(value, (list, tuple, dict)): + data[key] = "JSON::{}".format(json.dumps(value)) + + imprint(node, data) + + def read(self, node): + """Read call for placeholder node""" + + data = read(node) + + # Complicated data that can't be represented as flat maya attributes + # we read from json strings, e.g. multiselection EnumDef + for key, value in data.items(): + if isinstance(value, str) and value.startswith("JSON::"): + value = value[len("JSON::"):] # strip of JSON:: prefix + data[key] = json.loads(value) + + return data + + def build_workfile_template(*args): builder = MayaTemplateBuilder(registered_host()) builder.build_template() diff --git a/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py b/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py index 5bfaae6500..2de4594f47 100644 --- a/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py +++ b/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py @@ -1,87 +1,48 @@ -import json - from maya import cmds from ayon_core.pipeline.workfile.workfile_template_builder import ( - PlaceholderPlugin, - LoadPlaceholderItem, PlaceholderLoadMixin, + LoadPlaceholderItem ) from ayon_core.hosts.maya.api.lib import ( - read, - imprint, - get_reference_node + get_container_transforms, + get_node_parent, + Reorder +) +from ayon_core.hosts.maya.api.workfile_template_builder import ( + MayaPlaceholderPlugin, ) -from ayon_core.hosts.maya.api.workfile_template_builder import PLACEHOLDER_SET -class MayaPlaceholderLoadPlugin(PlaceholderPlugin, PlaceholderLoadMixin): +class MayaPlaceholderLoadPlugin(MayaPlaceholderPlugin, PlaceholderLoadMixin): identifier = "maya.load" label = "Maya load" - def _collect_scene_placeholders(self): - # Cache placeholder data to shared data - placeholder_nodes = self.builder.get_shared_populate_data( - "placeholder_nodes" - ) - if placeholder_nodes is None: - attributes = cmds.ls("*.plugin_identifier", long=True) - placeholder_nodes = {} - for attribute in attributes: - node_name = attribute.rpartition(".")[0] - placeholder_nodes[node_name] = ( - self._parse_placeholder_node_data(node_name) - ) - - self.builder.set_shared_populate_data( - "placeholder_nodes", placeholder_nodes - ) - return placeholder_nodes - - def _parse_placeholder_node_data(self, node_name): - placeholder_data = read(node_name) - parent_name = ( - cmds.getAttr(node_name + ".parent", asString=True) - or node_name.rpartition("|")[0] - or "" - ) - if parent_name: - siblings = cmds.listRelatives(parent_name, children=True) - else: - siblings = cmds.ls(assemblies=True) - node_shortname = node_name.rpartition("|")[2] - current_index = cmds.getAttr(node_name + ".index", asString=True) - if current_index < 0: - current_index = siblings.index(node_shortname) - - placeholder_data.update({ - "parent": parent_name, - "index": current_index - }) - return placeholder_data + item_class = LoadPlaceholderItem def _create_placeholder_name(self, placeholder_data): - placeholder_name_parts = placeholder_data["builder_type"].split("_") - pos = 1 + # Split builder type: context_assets, linked_assets, all_assets + prefix, suffix = placeholder_data["builder_type"].split("_", 1) + parts = [prefix] + + # add family if any placeholder_product_type = placeholder_data.get("product_type") if placeholder_product_type is None: placeholder_product_type = placeholder_data.get("family") if placeholder_product_type: - placeholder_name_parts.insert(pos, placeholder_product_type) - pos += 1 + parts.append(placeholder_product_type) # add loader arguments if any loader_args = placeholder_data["loader_args"] if loader_args: - loader_args = json.loads(loader_args.replace('\'', '\"')) - values = [v for v in loader_args.values()] - for value in values: - placeholder_name_parts.insert(pos, value) - pos += 1 + loader_args = eval(loader_args) + for value in loader_args.values(): + parts.append(str(value)) - placeholder_name = "_".join(placeholder_name_parts) + parts.append(suffix) + placeholder_name = "_".join(parts) return placeholder_name.capitalize() @@ -104,68 +65,6 @@ class MayaPlaceholderLoadPlugin(PlaceholderPlugin, PlaceholderLoadMixin): ) return loaded_representation_ids - def create_placeholder(self, placeholder_data): - selection = cmds.ls(selection=True) - if len(selection) > 1: - raise ValueError("More then one item are selected") - - parent = selection[0] if selection else None - - placeholder_data["plugin_identifier"] = self.identifier - - placeholder_name = self._create_placeholder_name(placeholder_data) - - placeholder = cmds.spaceLocator(name=placeholder_name)[0] - if parent: - placeholder = cmds.parent(placeholder, selection[0])[0] - - imprint(placeholder, placeholder_data) - - # Add helper attributes to keep placeholder info - cmds.addAttr( - placeholder, - longName="parent", - hidden=True, - dataType="string" - ) - cmds.addAttr( - placeholder, - longName="index", - hidden=True, - attributeType="short", - defaultValue=-1 - ) - - cmds.setAttr(placeholder + ".parent", "", type="string") - - def update_placeholder(self, placeholder_item, placeholder_data): - node_name = placeholder_item.scene_identifier - new_values = {} - for key, value in placeholder_data.items(): - placeholder_value = placeholder_item.data.get(key) - if value != placeholder_value: - new_values[key] = value - placeholder_item.data[key] = value - - for key in new_values.keys(): - cmds.deleteAttr(node_name + "." + key) - - imprint(node_name, new_values) - - def collect_placeholders(self): - output = [] - scene_placeholders = self._collect_scene_placeholders() - for node_name, placeholder_data in scene_placeholders.items(): - if placeholder_data.get("plugin_identifier") != self.identifier: - continue - - # TODO do data validations and maybe upgrades if they are invalid - output.append( - LoadPlaceholderItem(node_name, placeholder_data, self) - ) - - return output - def populate_placeholder(self, placeholder): self.populate_load_placeholder(placeholder) @@ -176,25 +75,6 @@ class MayaPlaceholderLoadPlugin(PlaceholderPlugin, PlaceholderLoadMixin): def get_placeholder_options(self, options=None): return self.get_load_plugin_options(options) - def post_placeholder_process(self, placeholder, failed): - """Cleanup placeholder after load of its corresponding representations. - - Args: - placeholder (PlaceholderItem): Item which was just used to load - representation. - failed (bool): Loading of representation failed. - """ - # Hide placeholder and add them to placeholder set - node = placeholder.scene_identifier - - cmds.sets(node, addElement=PLACEHOLDER_SET) - cmds.hide(node) - cmds.setAttr(node + ".hiddenInOutliner", True) - - def delete_placeholder(self, placeholder): - """Remove placeholder if building was successful""" - cmds.delete(placeholder.scene_identifier) - def load_succeed(self, placeholder, container): self._parent_in_hierarchy(placeholder, container) @@ -210,55 +90,43 @@ class MayaPlaceholderLoadPlugin(PlaceholderPlugin, PlaceholderLoadMixin): if not container: return - roots = cmds.sets(container, q=True) or [] - ref_node = None - try: - ref_node = get_reference_node(roots) - except AssertionError as e: - self.log.info(e.args[0]) + # TODO: This currently returns only a single root but a loaded scene + # could technically load more than a single root + container_root = get_container_transforms(container, root=True) - nodes_to_parent = [] - for root in roots: - if ref_node: - ref_root = cmds.referenceQuery(root, nodes=True)[0] - ref_root = ( - cmds.listRelatives(ref_root, parent=True, path=True) or - [ref_root] - ) - nodes_to_parent.extend(ref_root) - continue - if root.endswith("_RN"): - # Backwards compatibility for hardcoded reference names. - refRoot = cmds.referenceQuery(root, n=True)[0] - refRoot = cmds.listRelatives(refRoot, parent=True) or [refRoot] - nodes_to_parent.extend(refRoot) - elif root not in cmds.listSets(allSets=True): - nodes_to_parent.append(root) + # Bugfix: The get_container_transforms does not recognize the load + # reference group currently + # TODO: Remove this when it does + parent = get_node_parent(container_root) + if parent: + container_root = parent + roots = [container_root] - elif not cmds.sets(root, q=True): - return + # Add the loaded roots to the holding sets if they exist + holding_sets = cmds.listSets(object=placeholder.scene_identifier) or [] + for holding_set in holding_sets: + cmds.sets(roots, forceElement=holding_set) - # Move loaded nodes to correct index in outliner hierarchy + # Parent the roots to the place of the placeholder locator and match + # its matrix placeholder_form = cmds.xform( placeholder.scene_identifier, - q=True, + query=True, matrix=True, worldSpace=True ) - scene_parent = cmds.listRelatives( - placeholder.scene_identifier, parent=True, fullPath=True - ) - for node in set(nodes_to_parent): - cmds.reorder(node, front=True) - cmds.reorder(node, relative=placeholder.data["index"]) - cmds.xform(node, matrix=placeholder_form, ws=True) - if scene_parent: - cmds.parent(node, scene_parent) - else: - cmds.parent(node, world=True) + scene_parent = get_node_parent(placeholder.scene_identifier) + for node in set(roots): + cmds.xform(node, matrix=placeholder_form, worldSpace=True) - holding_sets = cmds.listSets(object=placeholder.scene_identifier) - if not holding_sets: - return - for holding_set in holding_sets: - cmds.sets(roots, forceElement=holding_set) + if scene_parent != get_node_parent(node): + if scene_parent: + node = cmds.parent(node, scene_parent)[0] + else: + node = cmds.parent(node, world=True)[0] + + # Move loaded nodes in index order next to their placeholder node + cmds.reorder(node, back=True) + index = Reorder.get_index(placeholder.scene_identifier) + cmds.reorder(node, front=True) + cmds.reorder(node, relative=index + 1) From 93dda6110add3ceb9d1956eec87e1fdbaf6cb9f8 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Fri, 19 Apr 2024 20:15:56 +0200 Subject: [PATCH 3/5] Remove bloated `Reorder` class in favor of the only used function --- client/ayon_core/hosts/maya/api/lib.py | 261 +----------------- .../maya/plugins/template/load_placeholder.py | 4 +- 2 files changed, 17 insertions(+), 248 deletions(-) diff --git a/client/ayon_core/hosts/maya/api/lib.py b/client/ayon_core/hosts/maya/api/lib.py index e30070c796..2e77fe6c64 100644 --- a/client/ayon_core/hosts/maya/api/lib.py +++ b/client/ayon_core/hosts/maya/api/lib.py @@ -4406,252 +4406,21 @@ def create_rig_animation_instance( ) -class Reorder(object): - """Helper functions for reordering in Maya outliner""" +def get_node_index_under_parent(node: str) -> int: + """Return the index of a DAG node under its parent. - @staticmethod - def group_by_parent(nodes): - """Groups the given input list of nodes by parent. + Arguments: + node (str): A DAG Node path. - This is a convenience function for the Reorder functionality. - This function assumes the nodes are in the `long/fullPath` format. - """ - nodes = cmds.ls(nodes, long=True) - nodes_by_parent = defaultdict(list) - for node in nodes: - parent = node.rsplit("|", 1)[0] - nodes_by_parent[parent].append(node) - return nodes_by_parent + Returns: + int: The DAG node's index under its parents or world - @staticmethod - def get_children_with_index(parent): - """Get children under parent with their indices""" - def node_to_index(nodes): - return {node: index for index, node in enumerate(nodes)} - - if not parent: - return node_to_index(cmds.ls(assemblies=True, long=True)) - else: - return node_to_index( - cmds.listRelatives(parent, - children=True, - fullPath=True) or [] - ) - - @staticmethod - def get_index(node): - node = cmds.ls(node, long=True)[0] # enforce long names - parent = node.rsplit("|", 1)[0] - if not parent: - return cmds.ls(assemblies=True, long=True).index(node) - else: - return cmds.listRelatives(parent, - children=True, - fullPath=True).index(node) - - @staticmethod - def get_indices(nodes): - """Returns a dictionary with node, index pairs. - - This is preferred over get_index method for larger number of nodes, - because it is more optimal in performance. - - eg: - { - '|side': 3, - '|top': 1, - '|pSphere1': 4, - '|persp': 0, - '|front': 2 - } - - Returns: - dict: index by node - """ - nodes = cmds.ls(nodes, long=True) # enforce long names - node_indices = dict() - cached_children = dict() - for node in nodes: - parent = node.rsplit("|", 1)[0] - if parent not in cached_children: - cached_children[parent] = Reorder.get_children_with_index(parent) # noqa: E501 - - node_indices[node] = cached_children[parent][node] - return node_indices - - @staticmethod - def set_index(node, index): - if not node: - return - cmds.reorder(node, front=True) - cmds.reorder(node, r=index) - - @staticmethod - def set_indices(node_indices): - """Set node order by node to index dict. - - Args: - node_indices (dict): Node name to index dictionary - - """ - if not isinstance(node_indices, dict): - raise TypeError( - "Reorder.set_indices() requires a dictionary with " - "(node, index) pairs as input. " - "`{0}` is an invalid input type.".format( - type(node_indices).__name__) - ) - - if not node_indices: - return - - # force nodes to the back to not influence each other during reorder - cmds.reorder(node_indices.keys(), back=True) - - for node, index in sorted(node_indices.items(), - key=operator.itemgetter(1)): - Reorder.set_index(node, index) - - @staticmethod - def sort(nodes, key=lambda x: x.rsplit("|", 1)[-1], reverse=False): - """Sorts the node in scene by the key function. - - Default sorting key is alphabetically by using the object's short name. - """ - nodes = cmds.ls(nodes, long=True) # ensure long paths - if not nodes: - return - - # Group by parent to sort nodes per parent - parents = Reorder.group_by_parent(nodes) - - for child_nodes in parents.values(): - - node_indices = Reorder.get_indices(child_nodes) - indices = sorted(node_indices.values()) - - new_indices = { - node: indices[i] for i, node in - enumerate(sorted(child_nodes, key=key, reverse=reverse)) - } - Reorder.set_indices(new_indices) - - @staticmethod - def reverse(nodes): - nodes = cmds.ls(nodes, long=True) # ensure long paths - if not nodes: - return - - # Group by parent to sort nodes per parent - parents = Reorder.group_by_parent(nodes) - - for child_nodes in parents.values(): - - node_indices = Reorder.get_indices(child_nodes) - indices = sorted(node_indices.values(), reverse=False) - - iterable = enumerate(sorted(node_indices.items(), - key=operator.itemgetter(1), - reverse=True)) - new_indices = { - node: indices[i] for i, (node, _old_index) in iterable - } - Reorder.set_indices(new_indices) - - @staticmethod - def align_bottom(nodes): - """Reorder to the lowest (most back) of node in `nodes`.""" - nodes = cmds.ls(nodes, long=True) # ensure long paths - if not nodes: - return - - # Group by parent to sort nodes per parent - parents = Reorder.group_by_parent(nodes) - for child_nodes in parents.values(): - - # Reorder.set_index forces to front and then moves all objects - # together (so they will be stacked together). Then it applies the - # index as relative offset, we can use that here to our advantage. - # And it is a lot faster than Reorder.set_indices in that scenario. - index_per_node = Reorder.get_indices(child_nodes) - back_index = max(index_per_node.values()) - new_front_index = back_index - len(child_nodes) + 1 - Reorder.set_index(child_nodes, new_front_index) - - @staticmethod - def align_top(nodes): - """Reorder to the highest (most front) of node in `nodes`.""" - nodes = cmds.ls(nodes, long=True) # ensure long paths - if not nodes: - return - - # Group by parent to sort nodes per parent - parents = Reorder.group_by_parent(nodes) - for childNodes in parents.values(): - - # Reorder.set_index forces to front and then moves all objects - # together (so they will be stacked together). Then it applies the - # index as relative offset, we can use that here to our advantage. - # And it is a lot faster than Reorder.set_indices in that scenario. - index_per_node = Reorder.get_indices(childNodes) - front_index = min(index_per_node.values()) - Reorder.set_index(childNodes, front_index) - - @staticmethod - def move(nodes, relative, wrap=True): - """ Reorder by the given relative amount. """ - # TODO: Implement the disabling of wrapping around when at bottom. - if not nodes: - return - cmds.reorder(nodes, r=relative) - - @staticmethod - def to_bottom(nodes): - """Reorder to all the way to the bottom.""" - if not nodes: - return - cmds.reorder(nodes, back=True) - - @staticmethod - def to_top(nodes): - """Reorder to all the way to the top.""" - if not nodes: - return - cmds.reorder(nodes, front=True) - - @staticmethod - def order_to(nodes): - """Reorder the nodes to the order of the input list. - - Tip: - If you pass this your current selection list it will reorder - the nodes to the order of your selection. - - """ - nodes = cmds.ls(nodes, long=True) # ensure long paths - if not nodes: - return - - # Make a dictionary of the input order so we can optimize the look-up - # of the index in the order of the input `nodes`. - selected_order = {node: i for i, node in enumerate(nodes)} - - # Group by parent since we want to sort nodes under its current parent - parents = Reorder.group_by_parent(nodes) - for child_nodes in parents.values(): - - # Get the current indices - node_indices = Reorder.get_indices(child_nodes) - - # We get the original indices so we can position to those same - # positions, albeit with the new ordering of the nodes. - orig_indices = sorted(node_indices.values()) - - # Order the nodes by current selection (input list) and then apply - # the list of indices from `nodeIndices` in low-to-high order. - new_indices = dict( - zip(sorted(node_indices.keys(), - key=lambda x: selected_order[x]), - orig_indices) - ) - Reorder.set_indices(new_indices) + """ + node = cmds.ls(node, long=True)[0] # enforce long names + parent = node.rsplit("|", 1)[0] + if not parent: + return cmds.ls(assemblies=True, long=True).index(node) + else: + return cmds.listRelatives(parent, + children=True, + fullPath=True).index(node) diff --git a/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py b/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py index 2de4594f47..b07c7e9a70 100644 --- a/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py +++ b/client/ayon_core/hosts/maya/plugins/template/load_placeholder.py @@ -7,7 +7,7 @@ from ayon_core.pipeline.workfile.workfile_template_builder import ( from ayon_core.hosts.maya.api.lib import ( get_container_transforms, get_node_parent, - Reorder + get_node_index_under_parent ) from ayon_core.hosts.maya.api.workfile_template_builder import ( MayaPlaceholderPlugin, @@ -127,6 +127,6 @@ class MayaPlaceholderLoadPlugin(MayaPlaceholderPlugin, PlaceholderLoadMixin): # Move loaded nodes in index order next to their placeholder node cmds.reorder(node, back=True) - index = Reorder.get_index(placeholder.scene_identifier) + index = get_node_index_under_parent(placeholder.scene_identifier) cmds.reorder(node, front=True) cmds.reorder(node, relative=index + 1) From 8e2f3235c6ab6704e744ffe2cd977b22940f24b4 Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Tue, 30 Apr 2024 12:58:08 +0200 Subject: [PATCH 4/5] Remove unused import --- client/ayon_core/hosts/maya/api/lib.py | 1 - 1 file changed, 1 deletion(-) diff --git a/client/ayon_core/hosts/maya/api/lib.py b/client/ayon_core/hosts/maya/api/lib.py index ff94459a31..e7361c6910 100644 --- a/client/ayon_core/hosts/maya/api/lib.py +++ b/client/ayon_core/hosts/maya/api/lib.py @@ -6,7 +6,6 @@ from pprint import pformat import sys import uuid import re -import operator import json import logging From 14df54020b4feae55e995f0ecd396af9b86f842a Mon Sep 17 00:00:00 2001 From: Roy Nieterau Date: Wed, 1 May 2024 00:02:19 +0200 Subject: [PATCH 5/5] Resolve merge conflict, implement change from `develop` --- client/ayon_core/hosts/maya/api/workfile_template_builder.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/client/ayon_core/hosts/maya/api/workfile_template_builder.py b/client/ayon_core/hosts/maya/api/workfile_template_builder.py index d518d3933c..f4f9a34983 100644 --- a/client/ayon_core/hosts/maya/api/workfile_template_builder.py +++ b/client/ayon_core/hosts/maya/api/workfile_template_builder.py @@ -194,6 +194,11 @@ class MayaPlaceholderPlugin(PlaceholderPlugin): # Hide placeholder and add them to placeholder set node = placeholder.scene_identifier + # If we just populate the placeholders from current scene, the + # placeholder set will not be created so account for that. + if not cmds.objExists(PLACEHOLDER_SET): + cmds.sets(name=PLACEHOLDER_SET, empty=True) + cmds.sets(node, addElement=PLACEHOLDER_SET) cmds.hide(node) cmds.setAttr("{}.hiddenInOutliner".format(node), True)