Skip to content

Instantly share code, notes, and snippets.

@BigRoy
Last active June 18, 2021 09:18
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/fbf391314e2130567382ae5324efb941 to your computer and use it in GitHub Desktop.
Save BigRoy/fbf391314e2130567382ae5324efb941 to your computer and use it in GitHub Desktop.
Maya 2020 reproducable case of reference edits for string attributes stripping whitespaces in a string value
from maya import cmds, mel
import maya.OpenMaya as om
# Point filepath to a mayaAscii file that has at least one transform node in it
filepath = r"path/to/maya_scene_to_reference.ma"
# Reference the file
nodes = cmds.file(filepath, reference=True, type="mayaAscii", ignoreVersion=True, gl=True, mergeNamespacesOnClash=True, namespace="camera", options="v=0;", returnNewNodes=True)
ref = cmds.ls(nodes, type="reference")[0]
# Add attribute to the node and set the value
transform = cmds.ls(nodes, type="transform")[0]
cmds.addAttr(transform, longName="myAttr", dataType="string")
cmds.setAttr(transform + ".myAttr", "hello world. testing with many whitespaces.", type="string")
# Example:
# maya.cmds
print cmds.referenceQuery(ref, editStrings=True, editCommand="setAttr")[0]
# setAttr |camera:camera.myAttr -type "string" "hello world. testing with many whitespaces."
# Above is invalid, note the missing whitespaces.
# Example:
# mel
print mel.eval('referenceQuery -editStrings -editCommand "setAttr" "{}"'.format(ref))[0]
# setAttr |camera:camera.myAttr -type "string" "hello world. testing with many whitespaces."
# Above is invalid, same issue.
# Example
# maya.OpenMaya API
sel = om.MSelectionList()
sel.add(ref)
o_ref = om.MObject()
sel.getDependNode(0, o_ref)
it_edit = om.MItEdits(o_ref)
while not it_edit.isDone():
if it_edit.currentEditType() == om.MEdit.kSetAttrEdit:
# Directly from MItEdits
print it_edit.currentEditString()
# setAttr "camera:camera.myAttr" -type "string" "hello world. testing with many whitespaces."
# Using MEdit.getString()
edit = it_edit.edit()
print edit.getString()
# setAttr "camera:camera.myAttr" -type "string" "hello world. testing with many whitespaces."
# Using MSetAttrEdit
set_attr_edit = it_edit.setAttrEdit()
print set_attr_edit.getString()
# setAttr "camera:camera.myAttr" -type "string" "hello world. testing with many whitespaces."
it_edit.next()
# Example:
# maya.OpenMaya.MPlug.getSetAttrCmds() - get edits directly from reference node "edits" attribute
sel = om.MSelectionList()
sel.add(ref)
o_ref = om.MObject()
sel.getDependNode(0, o_ref)
fn_dep = om.MFnDependencyNode(o_ref)
edits_plug = fn_dep.findPlug("edits")
commands = []
edits_plug.getSetAttrCmds(commands, om.MPlug.kAll, True)
print commands
# This is invalid because there is no valid data as long as the reference is loaded
ref_filename_w_copy_number = cmds.referenceQuery(ref, filename=True)
cmds.file(ref_filename_w_copy_number, unloadReference=True)
commands = []
edits_plug.getSetAttrCmds(commands, om.MPlug.kAll, True)
print commands
print next(c for c in commands if "hello world." in c) # print only the line related to the setAttr
# 2 "|camera:camera" "myAttr" " -type \"string\" \"hello world. testing with many whitespaces.\""
# This actually DOES correctly output the string - but I'd rather have it from something else with edits
# that are more trivial to parse and work whilst the reference is loaded.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment