Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active December 20, 2024 09:53
Show Gist options
  • Save BigRoy/30e2b9d209069604c7b5f23ab2420af3 to your computer and use it in GitHub Desktop.
Save BigRoy/30e2b9d209069604c7b5f23ab2420af3 to your computer and use it in GitHub Desktop.
Example script using AYON Maya integration 'cbId' to perform a post-process 'connection' between source and target cbId in the current scene
# Perform a 'connection' between dag nodes in Maya based on their cbId
from typing import Dict
from maya import cmds
from ayon_maya.api import lib
mapping: Dict[str, str] = {
# Data mapping is like:
"source-id": "target-id",
# Example:
"ac991b60b4cb11eeb7fc7730318c5a70:c0ce772a9052": "ac991b60b4cb11eeb7fc7730318c5a70:f328201a4e8b"
}
def connect(source_node: str, target_node: str):
"""Process a source to a node id.
This defines what to do between any found mapping
of nodes.
"""
src = f"{source_node}.worldMatrix[0]"
dest = f"{target_node}.offsetParentMatrix"
if cmds.isConnected(src, dest):
# Ignore if already connected
return
cmds.connectAttr(src, dest, force=True)
nodes_by_id: Dict[str, str] = {}
nodes = cmds.ls(dag=True, long=True) # All dag nodes
for node in nodes:
node_id = lib.get_id(node)
if not node_id:
continue
nodes_by_id[node_id] = node
for source_id, target_id in mapping.items():
if source_id in nodes_by_id:
if target_id not in nodes_by_id:
print(f"Target {target_id} not found for source {source_id}")
continue
source_node = nodes_by_id[source_id]
target_node = nodes_by_id[target_id]
connect(source_node, target_node)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment