Skip to content

Instantly share code, notes, and snippets.

@davidlatwe
Forked from mottosso/README.md
Created November 2, 2017 06:43
Show Gist options
  • Save davidlatwe/bce9d427fe46bbcd7053f4c906dd9fab to your computer and use it in GitHub Desktop.
Save davidlatwe/bce9d427fe46bbcd7053f4c906dd9fab to your computer and use it in GitHub Desktop.
Nodes with unique IDs

Nodes with unique IDs in Maya

unique_id1.py associates a new ID to every DAG-node in the scene, unique_id2.py does the same but keeps existing IDs as-is. Finally, unique_id3.py updates IDs and ensures that no duplicate ID exists in the scene.

"""Assign a unique identifier to ever DAG node within Maya
The problem: existing IDs are discarded.
"""
import uuid
from maya import cmds
def update_id():
"""Update the unique identifier of every node"""
for node in cmds.ls(dag=True):
attribute = node + ".uuid"
if not cmds.objExists(attribute):
cmds.addAttr(node,
longName="uuid",
dataType="string")
new_id = str(uuid.uuid4())
cmds.setAttr(attribute, new_id, type="string")
update_id()
"""Assign a unique identifier to ever DAG node within Maya
The problem: there may be duplicate IDs
"""
import uuid
from maya import cmds
def update_id():
"""Update the unique identifier of every node"""
ids = list()
for node in cmds.ls(dag=True):
attribute = node + ".uuid"
if not cmds.objExists(attribute):
cmds.addAttr(node,
longName="uuid",
dataType="string")
cmds.setAttr(attribute, str(uuid.uuid4()), type="string")
update_id()
"""Assign a unique identifier to ever DAG node within Maya
Also keep existing ID's and update any duplicates.
"""
import uuid
import time
from maya import cmds
def generate_id():
return str(uuid.uuid4())
def test_uniqueness():
ids = list()
for attr in cmds.ls("*.uuid"):
id = cmds.getAttr(attr)
assert id not in ids
ids.append(id)
def update_id():
"""Update the unique identifier of every node"""
start = time.time()
ids = list()
for node in cmds.ls(dag=True):
attribute = node + ".uuid"
if not cmds.objExists(attribute):
cmds.addAttr(node,
longName="uuid",
dataType="string")
new_id = generate_id()
ids.append(new_id)
print "No id: %s - creating" % node
else:
# Look for duplicate IDs
existing_id = cmds.getAttr(attribute)
if not existing_id in ids:
ids.append(existing_id)
print "Existing unique id: %s" % node
continue
new_id = generate_id()
print "Duplicate id: %s - updating" % node
print "Setting the attribute on %s" % node
cmds.setAttr(attribute, new_id, type="string")
end = time.time()
test_uniqueness()
print "Update took: %.3f ms" % ((end - start) * 1000)
update_id()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment