Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I get the filepath of a script that is loaded into the script editor?

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
marten.zanderBH2FZ
321 Views, 3 Replies

How do I get the filepath of a script that is loaded into the script editor?

Hi fellow Maya Programmers!

I need some help figuring out a script's filepath from within maya using python. I have a certain script file located somewhere on my harddrive. Let's assume the file is called "C:/Users/Username/Desktop/test.py". In Maya's Script Editor I open the script file using "File" -> "Open Script..." Menu. A new Tab in my script editor appears, displaying the content of my test.py. What I am trying to do now, is run the test.py and print the folder location where that file (test.py) is stored. 

Is there anyway to extract that path information?

Thanks in Advance!

Labels (2)
3 REPLIES 3
Message 2 of 4

import maya.cmds as cmds
def getScriptEditorPaths():
    # Get the name of the scriptEditorPanel
    allPanels = cmds.getPanel( all=True )
    
    scriptPanel = [x for x in allPanels if 'scriptEditor' in x]
    if not scriptPanel:
        return []
        
    scriptPanel = scriptPanel[0]
    
    # Step through the layouts of the script editor until we find the tablayout  
    f1 = cmds.layout(scriptPanel, q = True, childArray = True)
    f1, f2 = cmds.layout(formLayout1, q = True, childArray = True)
    p1 = cmds.layout(f2, q = True, childArray = True)
    sf, p2 = cmds.layout(p1, q = True, childArray = True)
    t1 = cmds.layout(p2, q = True, childArray = True)
    
    filePaths = []
    for eachChildLayout in cmds.tabLayout(t1, q = True, childArray = True):
        scrollFieldExec = cmds.layout(eachChildLayout, q = True, childArray = True)
        filePaths.append(cmds.cmdScrollFieldExecuter(scrollFieldExec, q = True, filename = True))
    
    return filePaths
    
getScriptEditorPaths()

This should work 🙂

Message 3 of 4

Hey man, thank you for your reply. unfortunately I get an error saying that global name "formLayout1" is not defined.

Message 4 of 4

Okay, I actually fixed it by myself already. I replaced "formLayout1" with f1[0] :). It's working, thanks a lot!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report