Remove reference edits via Python?

6,278 views
Skip to first unread message

Fredrik Averpil

unread,
Oct 30, 2014, 7:46:35 AM10/30/14
to python_in...@googlegroups.com

Hi,

I’m having problems performing this via Python. If using the UI, you would right click the reference and choose to list the reference edits. Then you can select each edit and hit “Remove reference edit”.

I’ve successfully been able to list reference edits in various ways with:

cmds.referenceQuery( "soffaRN", editStrings=True )
Result: [u'parent -s -r "|kuddar:pillows1" "|model2670"',
 u'parent -s -r "|kuddarRNfosterParent1|pillow_04" "|model2670|kuddar:pillows1"',
 u'setAttr |soffa:model2670.translate -type "double3" -506.529608 65.636552 493.906544',
 u'setAttr |soffa:model2670.rotate -type "double3" 212.335372 0 -91.855932'] #

cmds.referenceQuery( "soffaRN", editNodes=True )
Result: [u'|kuddar:pillows1',
 u'|model2670',
 u'|kuddarRNfosterParent1|pillow_04',
 u'|model2670|kuddar:pillows1',
 u'|soffa:model2670',
 u'|soffa:model2670'] # 

cmds.referenceQuery( "soffaRN", editAttrs=True )
Result: [u'translate', u'rotate'] #

But I can’t seem to find a way to take any of those responses and actually remove each edit.
Any ideas?

Regards,
Fredrik

Fredrik Averpil

unread,
Oct 30, 2014, 7:51:12 AM10/30/14
to python_in...@googlegroups.com
And if it makes it easier to somehow solve this ... I'm looking to remove *all* reference edits.



Luke Harris

unread,
Oct 30, 2014, 6:14:09 PM10/30/14
to python_in...@googlegroups.com
I was doing this recently with pymel. Maya says that you can now remove ref edits while the reference is loaded, but I've noticed this isn't always the case, so I still unload it first for safety

for ref in pm.listReferences():
    ref.unload()
    for edit in ref.getReferenceEdits():
        # this way you can selectively remove edits
        pm.ReferenceEdit(edit, fileReference=ref).remove(force=True)
    ref.load()

But if you want to remove ALL edits it might be faster to do a clean(), which removes failed edits. If the ref is unloaded then all edits are failed :)
for ref in pm.listReferences():
    ref.unload()
    ref.clean()
    ref.load()


Fredrik Averpil

unread,
Oct 31, 2014, 3:37:14 AM10/31/14
to python_in...@googlegroups.com

Hi Luke, this is very useful.

I save out all the edits to an editMB file before cleaning it. And when I save out that file as well as when I re-apply it later down the road, Maya crashes sporadically. Currently, I’m not unloading the reference when performing the edits export and the edits import. Perhaps that could be why it’s crashing on me? (it’s not always crashing)

Your pymel examples looks really clean. I managed to do it without pymel, but it’s a bit more cumbersome. I wonder if pymel is a more stable way of dealing with this?

Here’s what I did to remove all edits (on a loaded reference):

    # Remove all edits
    ref = 'myrefRN'
    nodes = cmds.referenceQuery( ref, editNodes=True )
    attr_types = cmds.referenceQuery( ref, editAttrs=True )
    for node in nodes:
        for attr_type in attr_types:
            for edit_command in ['addAttr', 'connectAttr', 'deleteAttr', 'disconnectAttr', 'parent', 'setAttr', 'lock', 'unlock']:
                cmds.referenceEdit( node+'.'+attr_type, failedEdits=True, successfulEdits=True, removeEdits=True, editCommand=edit_command)

// Fredrik

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2ee0b079-d794-4d84-956e-d465ae245e22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fredrik Averpil

unread,
Oct 31, 2014, 3:39:25 AM10/31/14
to python_in...@googlegroups.com

Whoops … I meant to write that Maya crashes when I export the editMB (sometimes) and later down the road it also crashes (sometimes) when I save down the edits into the actual scene (“Save reference edits”).

By the way, this is how I save down the reference edits on a loaded reference:

filepath = '..............'
cmds.file(filepath, saveReference=True, force=True)

Marcus Ottosson

unread,
Oct 31, 2014, 3:58:02 AM10/31/14
to python_in...@googlegroups.com
Crashing reference updates is surprisingly common I think. One stable way I've gone about it in the past is to update the .ma file. The results are the same, except Maya won't have to keep the old and new reference in memory while switching; which has been, in my experience, the cause of the majority of crashes.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Fredrik Averpil

unread,
Oct 31, 2014, 4:15:31 AM10/31/14
to python_in...@googlegroups.com

One stable way I've gone about it in the past is to update the .ma file.

How do you mean, exactly?
Do you mean launching a separate process such as maya -batch -command "[mel code]" -file hello.ma or do you mean reading in the .ma contents and modifying it with Python?

Marcus Ottosson

unread,
Oct 31, 2014, 4:27:54 AM10/31/14
to python_in...@googlegroups.com
You can either modify the .ma in a text-editor, as it is text, or through Python via something like regular expressions. Each reference will be clearly prefixed by it's unique command in the header of the file, along with its absolute path.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Eduardo Grana

unread,
Oct 31, 2014, 6:56:37 AM10/31/14
to python_in...@googlegroups.com
Hello Fredrik,
Regarding the remove all edits,
have you tryed the referenceEdit command with the flag removeEdits?
probably is safer, but you have to unload the reference first.
Cheers!
Eduardo


For more options, visit https://groups.google.com/d/optout.



--

Fredrik Averpil

unread,
Oct 31, 2014, 11:59:08 AM10/31/14
to python_in...@googlegroups.com

Yes, however, that just simply does not work at all.

Try this;

for ref in cmds.ls(references=True):
    cmds.referenceEdit( ref, removeEdits=True)

Here in my scene, nothing happens. No reference edits was removed.

Eduardo Grana

unread,
Oct 31, 2014, 12:13:55 PM10/31/14
to python_in...@googlegroups.com
Hey Fredrik,
Your right, it doesn't do anything! hahaha

try this:


import maya.cmds as cmds

for ref in cmds.ls(references=True):
cmds.file(unloadReference=ref)
cmds.file (cr=ref) # cleanReference

thats how the reference editor does it...
(C:/Program Files/Autodesk/Maya2013/scripts/others/referenceEditorPanel.mel)

Cheers!
Eduardo

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Fredrik Averpil

unread,
Oct 31, 2014, 12:42:54 PM10/31/14
to python_in...@googlegroups.com
Aaahhh... yes, that did work. Thank you!

gemi...@thelittlegiantz.com

unread,
Oct 9, 2018, 2:12:42 PM10/9/18
to Python Programming for Autodesk Maya
Hi Frederik,

I faced the same issue with you, searching all over google. And i think i found the problem.

it seems you need to use failedEdits and successfulEdits flags into it.

ex: cmds.referenceEdit( ref, removeEdits=True, failedEdits =True, successfulEdits =True)


Good Luck !
Gemilang Rahmandhika

verstaer...@googlemail.com

unread,
Aug 8, 2019, 5:02:04 AM8/8/19
to Python Programming for Autodesk Maya
thanks

my script was never working fully correctly ... only a couple of reference-edit were removed
now I realized that I have to take to unload and reload the refs... now it works perfectly

verstaer...@googlemail.com

unread,
Aug 8, 2019, 5:02:08 AM8/8/19
to Python Programming for Autodesk Maya
just in case someone needs a flexible python script to remove certain edits:

def removeRefedits(attr='fileTextureName',edit_command = 'setAttr',mode='remove',refNode='ani:*RN*',unloadRef=True ):
'''
removes certain refedits - refnodes need to be unload before running the script
@refNode is important to search in the right refNodes - please adjust for current shot/asset you're working 'MDL:*RN*', 'ani:*RN*'
'''
if mode == 'return':
fileNodes = []
refs = cmds.ls(refNode)
for ref in refs:
#print ref
nodes = cmds.referenceQuery( ref, editNodes=True )
attr_types = cmds.referenceQuery( ref, editAttrs=True )
if mode == 'remove':
fileNodes = []
for node in nodes:
if 'file' in node:
fileNodes.append(node)
#print node
if mode == 'remove':
if unloadRef==True:
cmds.file(unloadReference=ref)
for fNode in fileNodes:
cmds.referenceEdit( fNode+'.'+attr, failedEdits=True, successfulEdits=True, removeEdits=True, editCommand=edit_command)
print 'removed refedits from',fNode
if unloadRef==True:
cmds.file(loadReference=ref)
if mode == 'return':
if attr == 'fileTextureName':
if fileNodes:
return fileNodes
Reply all
Reply to author
Forward
0 new messages