generated from ynput/ayon-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 18
Implement loading directly into a Maya USD Proxy Shape #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
BigRoy
wants to merge
10
commits into
ynput:develop
Choose a base branch
from
BigRoy:enhancement/maya_usd_loaders
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f37fc9f
Implement loading directly into a Maya USD Proxy Shape
BigRoy b011ec2
Merge branch 'develop' into enhancement/maya_usd_loaders
BigRoy 6f159e4
Merge branch 'develop' into enhancement/maya_usd_loaders
BigRoy 8403100
Fix USD stage access
BigRoy 90d8690
Fix id access
BigRoy 150be6c
Merge branch 'develop' into enhancement/maya_usd_loaders
BigRoy 529a085
Merge branch 'develop' into enhancement/maya_usd_loaders
BigRoy 8d83dec
Merge branch 'develop' into enhancement/maya_usd_loaders
BigRoy 0f9b4b5
Merge branch 'develop' into enhancement/maya_usd_loaders
antirotor f211ce0
Merge branch 'develop' into enhancement/maya_usd_loaders
moonyuet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from ayon_core.pipeline.constants import AVALON_CONTAINER_ID | ||
| from maya import cmds | ||
| from pxr import Sdf | ||
|
|
||
|
|
||
| def remove_spec(spec): | ||
| """Delete Sdf.PrimSpec or Sdf.PropertySpec | ||
|
|
||
| Also see: | ||
| https://forum.aousd.org/t/api-basics-for-designing-a-manage-edits-editor-for-usd/676/1 # noqa | ||
| https://gist.github.com/BigRoy/4d2bf2eef6c6a83f4fda3c58db1489a5 | ||
|
|
||
| """ | ||
| if spec.expired: | ||
| return | ||
|
|
||
| if isinstance(spec, Sdf.PrimSpec): | ||
| # PrimSpec | ||
| parent = spec.nameParent | ||
| if parent: | ||
| view = parent.nameChildren | ||
| else: | ||
| # Assume PrimSpec is root prim | ||
| view = spec.layer.rootPrims | ||
| del view[spec.name] | ||
|
|
||
| elif isinstance(spec, Sdf.PropertySpec): | ||
| # Relationship and Attribute specs | ||
| del spec.owner.properties[spec.name] | ||
| else: | ||
| raise TypeError(f"Unsupported spec type: {spec}") | ||
|
|
||
|
|
||
| def iter_ufe_usd_selection(): | ||
| """Yield Maya USD Proxy Shape related UFE paths in selection. | ||
|
|
||
| The returned path are the Maya node name joined by a command to the | ||
| USD prim path. | ||
|
|
||
| Yields: | ||
| str: Path to UFE path in USD stage in selection. | ||
|
|
||
| """ | ||
| for path in cmds.ls(selection=True, ufeObjects=True, long=True, | ||
| absoluteName=True): | ||
| if "," not in path: | ||
| continue | ||
|
|
||
| node, ufe_path = path.split(",", 1) | ||
| if cmds.nodeType(node) != "mayaUsdProxyShape": | ||
| continue | ||
|
|
||
| yield path | ||
|
|
||
|
|
||
| def containerise_prim(prim, | ||
| name, | ||
| namespace, | ||
| context, | ||
| loader): | ||
| """Containerise a USD prim. | ||
|
|
||
| Arguments: | ||
| prim (pxr.Usd.Prim): The prim to containerise. | ||
| name (str): Name to containerize. | ||
| namespace (str): Namespace to containerize. | ||
| context (dict): Load context (incl. representation). | ||
| name (str): Name to containerize. | ||
| loader (str): Loader name. | ||
|
|
||
| """ | ||
| for key, value in { | ||
| "ayon:schema": "openpype:container-2.0", | ||
| "ayon:id": AVALON_CONTAINER_ID, | ||
| "ayon:name": name, | ||
| "ayon:namespace": namespace, | ||
| "ayon:loader": loader, | ||
| "ayon:representation": context["representation"]["id"], | ||
| }.items(): | ||
| prim.SetCustomDataByKey(key, str(value)) |
156 changes: 156 additions & 0 deletions
156
client/ayon_maya/plugins/load/load_maya_usd_add_maya_reference.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| # -*- coding: utf-8 -*- | ||
| import contextlib | ||
|
|
||
| from ayon_core.pipeline import load | ||
| from ayon_maya.api.usdlib import ( | ||
| containerise_prim, | ||
| iter_ufe_usd_selection | ||
| ) | ||
|
|
||
| from maya import cmds | ||
| import mayaUsd | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def no_edit_mode(prim, restore_after=True): | ||
| """Ensure MayaReference prim is not in edit mode during context""" | ||
| pulled_node = mayaUsd.lib.PrimUpdaterManager.readPullInformation(prim) | ||
| ufe_path = None | ||
| try: | ||
| # remove edit state if pulled | ||
| if pulled_node: | ||
| import mayaUsdUtils | ||
| assert mayaUsdUtils.isPulledMayaReference(pulled_node) | ||
| cmds.mayaUsdDiscardEdits(pulled_node) | ||
|
|
||
| # Discarding the edits directly selects the prim | ||
| # so we can get the UFE path from selection | ||
| ufe_path = cmds.ls(selection=True, ufeObjects=True, long=True)[0] | ||
|
|
||
| yield prim, ufe_path, pulled_node | ||
| finally: | ||
| if restore_after and pulled_node and ufe_path: | ||
| cmds.mayaUsdEditAsMaya(ufe_path) | ||
|
|
||
|
|
||
| class MayaUsdProxyAddMayaReferenceLoader(load.LoaderPlugin): | ||
| """Read USD data in a Maya USD Proxy | ||
|
|
||
| TODO: It'd be much easier if this loader would be capable of returning the | ||
| available containers in the scene based on the AYON URLs inside a USD | ||
| stage. That way we could potentially avoid the need for custom metadata | ||
| keys, stay closer to USD native data and rely solely on the | ||
| AYON:asset=blue,subset=modelMain,version=1 url | ||
|
|
||
| """ | ||
|
|
||
| product_types = {"*"} | ||
| representations = ["*"] | ||
| extensions = ["ma", "mb"] | ||
|
|
||
| label = "USD Add Maya Reference" | ||
| order = 1 | ||
| icon = "code-fork" | ||
| color = "orange" | ||
|
|
||
| identifier_key = "ayon_identifier" | ||
|
|
||
| def load(self, context, name=None, namespace=None, options=None): | ||
|
|
||
| selection = list(iter_ufe_usd_selection()) | ||
| assert len(selection) == 1, "Select only one PRIM please" | ||
| ufe_path = selection[0] | ||
| path = self.filepath_from_context(context) | ||
| # Make sure we can load the plugin | ||
| cmds.loadPlugin("mayaUsdPlugin", quiet=True) | ||
| import mayaUsdAddMayaReference | ||
|
|
||
| namespace = "test" | ||
| prim = mayaUsdAddMayaReference.createMayaReferencePrim( | ||
| ufe_path, | ||
| path, | ||
| namespace, | ||
| # todo: add more of the arguments | ||
| # mayaReferencePrimName Nameprim_name, | ||
| # groupPrim (3-tuple, group name, type and kind) | ||
| # variantSet (2-tuple, variant set name and variant name) | ||
| ) | ||
| if not prim: | ||
| # Failed to add a reference | ||
| raise RuntimeError(f"Failed to add a reference at {ufe_path}") | ||
|
|
||
| containerise_prim( | ||
| prim, | ||
| name=name, | ||
| namespace=namespace or "", | ||
| context=context, | ||
| loader=self.__class__.__name__ | ||
| ) | ||
|
|
||
| return prim | ||
|
|
||
| def _update_reference_path(self, prim, filepath): | ||
| """Update MayaReference prim 'mayaReference' in nearest prim spec""" | ||
|
|
||
| from pxr import Sdf | ||
|
|
||
| # We want to update the authored opinion in the right place, e.g. | ||
| # within a VariantSet if it's authored there. We go through the | ||
| # PrimStack to find the first prim spec that authors an opinion | ||
| # on the 'mayaReference' attribute where we have permission to | ||
| # change it. This could technically mean we're altering it in | ||
| # layers that we might not want to (e.g. a published USD file?) | ||
| stack = prim.GetPrimStack() | ||
| for prim_spec in stack: | ||
| if "mayaReference" not in prim_spec.attributes: | ||
| # prim spec defines no opinion on mayaRefernce attribute? | ||
| continue | ||
|
|
||
| attr = prim_spec.attributes["mayaReference"] | ||
| if attr.permission != Sdf.PermissionPublic: | ||
| print(f"Not allowed to edit: {attr}") | ||
| continue | ||
|
|
||
| if filepath != attr.default: | ||
| print( | ||
| f"Updating {attr.path} - {attr.default} -> {filepath}") | ||
| attr.default = filepath | ||
|
|
||
| # Attribute is either updated or already set to | ||
| # the value in that layer | ||
| return | ||
|
|
||
| # Just define in the current edit layer? | ||
| attr = prim.GetAttribute("mayaReference") | ||
| attr.Set(filepath) | ||
|
|
||
| def update(self, container, context): | ||
| # type: (dict, dict) -> None | ||
| """Update container with specified representation.""" | ||
|
|
||
| prim = container["prim"] | ||
| representation = context["representation"] | ||
| filepath = self.filepath_from_context(context) | ||
|
|
||
| with no_edit_mode(prim): | ||
| self._update_reference_path(prim, filepath) | ||
|
|
||
| # Update representation id | ||
| # TODO: Do this in prim spec where we update reference path? | ||
| prim.SetCustomDataByKey( | ||
| "ayon:representation", str(representation["_id"]) | ||
| ) | ||
|
|
||
| def switch(self, container, context): | ||
| self.update(container, context) | ||
|
|
||
| def remove(self, container): | ||
| # type: (dict) -> None | ||
| """Remove loaded container.""" | ||
|
|
||
| from ayon_maya.api.usdlib import remove_spec | ||
|
|
||
| prim = container["prim"] | ||
| with no_edit_mode(prim, restore_after=False): | ||
| for spec in prim.GetPrimStack(): | ||
| remove_spec(spec) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean it is only allowed one geometry in the loaded asset(or can we load with multiple assets)? Maybe we can exclude some families to load this as we can load stuff via this loader if it is also layout product(which is mostly with multiple assets).

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error means that you need to SELECT a PRIM inside the Maya USD Proxy to 'reference into'. Unfortunately since it 'references into a prim' there isn't really something more intuitive I could think of whilst still allowing to load anywhere inside the USD hierarchy.