Introducing the commands around the reference. It is a compilation of only the frequently used items rather than explanations.
~~ Already posted on the Advent Calendar with similar content, but don't worry! ~~
Try to get the reference node from the referenced object.
First, I will introduce the wrong way.
node = "ref:pSphere1"
namespace = node.split(":")[0]
RNnode = namespace + 'RN'
# Result: u'refRN' #
It looks fine, but it doesn't work if the object namespace changes.
Here's the right way
node = "ref:pSphere1"
RNnode = cmds.referenceQuery(node, referenceNode=True)
# Result: u'refRN' #
You can get the name of the RN node without any problem even if the namespace of the original object changes.
Frequently used items
If you don't know withoutCopyNumber, you'll be addicted to it, so be careful.
https://help.autodesk.com/cloudhelp/2019/JPN/Maya-Tech-Docs/CommandsPython/referenceEdit.html
node = "ref:pSphere1"
RNnode = cmds.referenceQuery(node, referenceNode=True)
#Get the reference namespace
cmds.referenceQuery(RNnode, namespace=True)
# Result: u':ref' #
#Get the reference file path
cmds.referenceQuery(RNnode, filename=True)
# Result: u'C:/Users/ponty/Desktop/ref.ma' #
#Result of filename is'C:/Users/ponty/Desktop/ref.ma(1)'If so, use this.
#Get the reference file path
cmds.referenceQuery(RNnode, filename=True, withoutCopyNumber=True)
# Result: u'C:/Users/ponty/Desktop/ref.ma' #
#Get reference load status
cmds.referenceQuery(RNnode, isLoaded=True)
# Result: True #
#Get a list of referenced objects
cmds.referenceQuery(RNnode, nodes=True)
"""
Result: [
u'ref:pSphere1',
u'ref:pSphereShape1',
u'ref:shapeEditorManager',
u'ref:poseInterpolatorManager',
u'ref:layerManager',
u'ref:defaultLayer',
u'ref:renderLayerManager',
u'ref:defaultRenderLayer',
u'ref:polySphere1',
u'ref:uiConfigurationScriptNode',
u'ref:sceneConfigurationScriptNode'
]
"""
Let's put together a list of asset information in the scene in a dictionary.
def getRefarenceInfo():
ret = []
refNodes = cmds.ls(references=True)
for RNnode in refNodes:
ref = {}
ref.update({
'namespace' : cmds.referenceQuery(RNnode, namespace=True),
'filenam' : cmds.referenceQuery(RNnode, filename=True),
'w_filenam' : cmds.referenceQuery(RNnode, filename=True, withoutCopyNumber=True),
'isLoaded' : cmds.referenceQuery(RNnode, isLoaded=True),
'nodes' : cmds.referenceQuery(RNnode, nodes=True),
'node' : cmds.referenceQuery(RNnode, nodes=True)[0],
})
ret.append(ref)
return ret
"""
# Result: [{'filenam': u'C:/Users/ponty/Desktop/cube.ma{1}',
'isLoaded': True,
'namespace': u':ref',
'node': u'ref:pCube1',
'nodes': [u'ref:pCube1',
u'ref:pCubeShape1',
u'ref:shapeEditorManager',
u'ref:poseInterpolatorManager',
u'ref:layerManager',
u'ref:defaultLayer',
u'ref:renderLayerManager',
u'ref:defaultRenderLayer',
u'ref:polyCube1',
u'ref:uiConfigurationScriptNode',
u'ref:sceneConfigurationScriptNode'],
'w_filenam': u'C:/Users/ponty/Desktop/cube.ma'},
{'filenam': u'C:/Users/ponty/Desktop/ref.ma{2}',
'isLoaded': True,
'namespace': u':ref1',
'node': u'ref1:pSphere1',
'nodes': [u'ref1:pSphere1',
u'ref1:pSphereShape1',
u'ref1:shapeEditorManager',
u'ref1:poseInterpolatorManager',
u'ref1:layerManager',
u'ref1:defaultLayer',
u'ref1:renderLayerManager',
u'ref1:defaultRenderLayer',
u'ref1:polySphere1',
u'ref1:uiConfigurationScriptNode',
u'ref1:sceneConfigurationScriptNode'],
'w_filenam': u'C:/Users/ponty/Desktop/ref.ma'}] #
"""
I often use it in my actual work.
#Creating a reference
cmds.file('C:/Users/ponty/Desktop/ref.ma', reference=True, ns='ref')
# Result: u'C:/Users/ponty/Desktop/ref.ma' #
#Reference remove
node = "ref:pSphere1"
RNnode = cmds.referenceQuery(node, referenceNode=True)
RNpath = cmds.referenceQuery(RNnode, filename=True)
cmds.file(RNpath, removeReference=True)
#Reference replacement
node = "ref:pSphere1"
RNnode = cmds.referenceQuery(node, referenceNode=True)
cmds.file('C:/Users/ponty/Desktop/cube.ma', lr=RNnode)
If you need to read many of the same characters in the reference, it will be easier to read them from the command.
cmds.file('C:/Users/ponty/Desktop/characterA.ma', reference=True, ns='CH_A_1')
cmds.file('C:/Users/ponty/Desktop/characterA.ma', reference=True, ns='CH_A_2')
cmds.file('C:/Users/ponty/Desktop/characterA.ma', reference=True, ns='CH_A_3')
cmds.file('C:/Users/ponty/Desktop/characterB.ma', reference=True, ns='CH_B_1')
cmds.file('C:/Users/ponty/Desktop/characterB.ma', reference=True, ns='CH_B_2')
cmds.file('C:/Users/ponty/Desktop/characterB.ma', reference=True, ns='CH_B_3')
If you manually operate the file, you will suffer later, so let's automate it as much as possible!
that's all!