Open Scene and get references

578 views
Skip to first unread message

Totally Zen

unread,
Jul 19, 2021, 7:36:02 PM7/19/21
to Python Programming for Autodesk Maya
I need to open a scene without loading in maya and get its references, to later work on some to load() correctly
*** I would like to use only pymel ***

code example:
import maya.cmds as cmds
sceneNameToOpen = "C:\my_scene\animal.mb"

cmds.file(sceneNameToOpen, loadReferenceDepth = 'none', open=1, buildLoadSettings=1):
mel.eval('PreloadReferenceEditor;')

Justin Israel

unread,
Jul 19, 2021, 9:18:07 PM7/19/21
to python_in...@googlegroups.com

--
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/cbedc1bf-2d09-4921-8d95-8795855708d1n%40googlegroups.com.

Totally Zen

unread,
Jul 19, 2021, 9:32:08 PM7/19/21
to Python Programming for Autodesk Maya
this command:
pmc.system.openFile(sceneNameToOpen, force=True, loadReferenceDepth = 'none', buildLoadSettings=True)
result:
# C:/my_scene/animal_RIG_A001.v002.mb
# C:/my_scene/animal_RIG_A001.v002.mb
# C:/my_scene/animal_RIG_A001.v002.mb

nsettings = range(cmds.selLoadSettings(numSettings=1, query=1))
ids = [str(i) for i in nsettings if i]
result = cmds.selLoadSettings(ids, fileName=1, query=1)
print result
result:
[u'C:/my_scene/animal_RIG_A001.v002.mb', u'C:/my_scene/animal_RIG_A001.v002.mb', u'C:/my_scene/animal_RIG_A001.v002.mb']

for item in result:
     item.load()
ERROR:
Error: AttributeError: file <maya console> line 27: 'unicode' object has no attribute 'load' #

This is because my result type is coming "unicode" and not as a : <class 'pymel.core.system.FileReference'>

and remembering that I want to run my code to find the references and enable and disable without having to load it into maya.

Justin Israel

unread,
Jul 20, 2021, 5:35:04 AM7/20/21
to python_in...@googlegroups.com


On Tue, 20 Jul 2021, 1:32 pm Totally Zen, <atu...@gmail.com> wrote:
this command:
pmc.system.openFile(sceneNameToOpen, force=True, loadReferenceDepth = 'none', buildLoadSettings=True)
result:
# C:/my_scene/animal_RIG_A001.v002.mb
# C:/my_scene/animal_RIG_A001.v002.mb
# C:/my_scene/animal_RIG_A001.v002.mb


You started here by using the pymel api.



nsettings = range(cmds.selLoadSettings(numSettings=1, query=1))
ids = [str(i) for i in nsettings if i]
result = cmds.selLoadSettings(ids, fileName=1, query=1)
print result
result:
[u'C:/my_scene/animal_RIG_A001.v002.mb', u'C:/my_scene/animal_RIG_A001.v002.mb', u'C:/my_scene/animal_RIG_A001.v002.mb']

for item in result:
     item.load()
ERROR:
Error: AttributeError: file <maya console> line 27: 'unicode' object has no attribute 'load' #

This is because my result type is coming "unicode" and not as a : <class 'pymel.core.system.FileReference'>

But then you switched to using the commands api which only deals in strings. You should keep using pymel if that it what you wanted. 



and remembering that I want to run my code to find the references and enable and disable without having to load it into maya.


Em segunda-feira, 19 de julho de 2021 às 22:18:07 UTC-3, justin...@gmail.com escreveu:

On Tue, 20 Jul 2021, 11:36 am Totally Zen, <atu...@gmail.com> wrote:
I need to open a scene without loading in maya and get its references, to later work on some to load() correctly
*** I would like to use only pymel ***

code example:
import maya.cmds as cmds
sceneNameToOpen = "C:\my_scene\animal.mb"

cmds.file(sceneNameToOpen, loadReferenceDepth = 'none', open=1, buildLoadSettings=1):
mel.eval('PreloadReferenceEditor;')

--
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/cbedc1bf-2d09-4921-8d95-8795855708d1n%40googlegroups.com.

--
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.

Totally Zen

unread,
Jul 20, 2021, 8:32:15 AM7/20/21
to Python Programming for Autodesk Maya
Yes, in a new code I changed it to use only pymel, but I still have the result of the type "unicode" and if it's not a type : <class 'pymel.core.system.FileReference'> I can't use load() or unload() .. :(

does anyone have any solution?

Justin Israel

unread,
Jul 20, 2021, 4:12:31 PM7/20/21
to python_in...@googlegroups.com
I see what you mean now. Yes the API docs say that it is meant to return a unicode string for the fileName parameter. I haven't played with this API before so I can only guess as to what is going on and had a quick play to try and get your expected results. 

When you do buildLoadSettings=True, and loadReferenceDepth='none', Maya only checks the paths and doesn't load or create any nodes. It saves an internal settings list for the preload reference editor so you can be selective about what to actually load. This can be controlled with the selLoadSettings function and by default all the references will start out as deferred (unloaded). You could selectively load some of them, but that doesn't seem to be what you are after right now. 

import pymel.core as pmc

scene = "/tmp/refs.mb"
pmc.openFile(scene, force=True, loadReferenceDepth='none', buildLoadSettings=True)
pmc.getReferences()
# Oh no, it's currently empty!
# {}

But if you then tell Maya to load the scene with those previously built settings, you should be able to get your FileReferences.

pmc.openFile(scene, force=True, loadSettings="implicitLoadSettings")
pmc.getReferences()     # or:   list(pmc.iterReferences())
# Result: {
 u'ball1': FileReference(u'/tmp/ball1.mb', refnode=u'ball1RN'),
 u'cone1': FileReference(u'/tmp/cone1.mb', refnode=u'cone1RN'),
 u'cube1': FileReference(u'/tmp/cube1.mb', refnode=u'cube1RN')} #

You could also select the ones you want to have loaded up front:

pmc.openFile(scene, force=True, loadReferenceDepth='none', buildLoadSettings=True)
ret = pmc.selLoadSettings(numSettings=True, query=True)
ids = [str(i) for i in range(ret) if i]
paths = pmc.selLoadSettings(ids, fileName=True, q=True)

# tell the ball reference to load
pmc.system.selLoadSettings(['1'], e=True, deferReference=0)
pmc.system.selLoadSettings(ids, q=True, deferReference=1)
# Result: [False, True, True] # 

pmc.openFile(scene, force=True, loadSettings="implicitLoadSettings")

Hopefully this is what you were after?


Totally Zen

unread,
Jul 20, 2021, 7:39:53 PM7/20/21
to Python Programming for Autodesk Maya
Very good what happened to me, I'll see with more time. 

If I could somehow get the RN from the references I would need to open the scene. 

I would use: 
      fileRN = pmc.system.FileReference(pathOrRefNode="Animal_RIG_A001_v005RN") 
      fileRN.unload() 

So I think I could. Thank you so much for your time helping me.

Totally Zen

unread,
Jul 20, 2021, 8:06:03 PM7/20/21
to Python Programming for Autodesk Maya

I managed to get to the part I wanted... 

After having the result value as a UNICODE, 

if we look at it's the path... and using that path I do the following. 

   for res in result: 
          fileRn = pmc.system.FileReference(pathOrRefNode=res) 
          print(type(fileRn)) ### Returns to a pymel class 
          print(fileRn.refNode) ### and I can get the refNode too 

I think that's it so far, lol thanks a lot for your help!

Totally Zen

unread,
Jul 20, 2021, 8:17:57 PM7/20/21
to Python Programming for Autodesk Maya
hey, a million excuses, I was wrong, I didn't pay attention that I was with the scene open while I was testing, so I still haven't got it, lol Apologies.

Justin Israel

unread,
Jul 20, 2021, 9:44:14 PM7/20/21
to python_in...@googlegroups.com
This would tell you the reference nodes:
refnodes = pmc.selLoadSettings(ids, referenceNode=True, q=True)


Totally Zen

unread,
Jul 21, 2021, 3:36:41 PM7/21/21
to Python Programming for Autodesk Maya
I'll need to do more tests, but I'll tell you what I'm looking for with all this, maybe I'm trying to do something that is hopeless! What I want to do is a way to export a Yeti cache, but without needing to open the scene in maya, I would just run the code showing where the project.mb is and let's say that underneath I would do all my export logic. Because if you need to open a scene, it's faster. Could I explain the purpose!
Reply all
Reply to author
Forward
0 new messages