Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Created December 20, 2023 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigRoy/bc5dc4224719890e50fd271adc29efc2 to your computer and use it in GitHub Desktop.
Save BigRoy/bc5dc4224719890e50fd271adc29efc2 to your computer and use it in GitHub Desktop.
Maya force an attribute value during context, even for referenced, locked and animated attributes
from maya import cmds
import maya.api.OpenMaya as om
import contextlib
def pairwise(iterable):
"""s -> (s0,s1), (s2,s3), (s4, s5), ..."""
a = iter(iterable)
return zip(a, a)
def get_plug(attr):
sel = om.MSelectionList()
sel.add(attr)
plug = sel.getPlug(0)
return plug
@contextlib.contextmanager
def ignore_reference_edits(ref_node):
"""During context do not register any reference edits to this reference node"""
sel = om.MSelectionList()
sel.add(ref_node)
obj = sel.getDependNode(0)
fn_ref = om.MFnReference(obj)
original = fn_ref.ignoreReferenceEdits()
try:
fn_ref.setIgnoreReferenceEdits(True)
yield
finally:
fn_ref.setIgnoreReferenceEdits(original)
@contextlib.contextmanager
def no_inputs(plug):
connections = cmds.listConnections(plug,
source=True,
destination=False,
plugs=True,
connections=True) or []
if not connections:
# Do nothing if no inputs
yield
return
try:
for dest, src in pairwise(connections):
cmds.disconnectAttr(src, dest, nextAvailable=False)
yield
finally:
for dest, src in pairwise(connections):
cmds.connectAttr(src, dest, force=True, nextAvailable=False)
@contextlib.contextmanager
def force_unlock(attr):
"""Unlock attribute via API during context.
This also works for referenced nodes.
"""
plug = get_plug(attr)
original = plug.isLocked
try:
plug.isLocked = False
yield
finally:
plug.isLocked = original
@contextlib.contextmanager
def attr_free_to_change(attr, disable_reference_edits=True):
"""Make attribute free to change during context.
This unlocks the attribute and breaks incoming connections.
After the context the lock state is reverted and any connections
are reconnected.
It's up to the user to make sure that at the end of the context
the attribute does not have any animations or connections, otherwise
the attribute is not ensured to revert correctly.
"""
with contextlib.ExitStack() as stack:
# Make sure any connect/disconnect does not get marked as reference
# edit. Note that during the context this also means that any changes
# during the context are not registered as reference edits either!
if disable_reference_edits and cmds.referenceQuery(attr, isNodeReferenced=True):
ref_node = cmds.referenceQuery(attr, referenceNode=True)
stack.enter_context(ignore_reference_edits(ref_node))
stack.enter_context(force_unlock(attr))
stack.enter_context(no_inputs(attr))
yield
camera = "cameraShape1"
plug = f"{camera}.overscan"
with attr_free_to_change(plug):
value = cmds.getAttr(plug)
try:
cmds.setAttr(plug, 1.0)
# Now camera overscan is 1.0 - even if the attribute had incoming connections
# or was locked originally. After the context, it's reverted. This allows us
# to now FORCE a playblast e.g. to be at overscan 1.0 but leave the user's scene
# in tact afterwards
print(cmds.getAttr(plug))
cmds.currentTime(cmds.currentTime(q=True) + 1)
print(cmds.getAttr(plug))
finally:
cmds.setAttr(plug, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment