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

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

marten.zanderBH2FZ
Participant Participant
535 Views
3 Replies
Message 1 of 4

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

marten.zanderBH2FZ
Participant
Participant

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!

0 Likes
Accepted solutions (1)
536 Views
3 Replies
Replies (3)
Message 2 of 4

jmreinhart
Advisor
Advisor
Accepted solution
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 🙂

0 Likes
Message 3 of 4

marten.zanderBH2FZ
Participant
Participant

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

0 Likes
Message 4 of 4

marten.zanderBH2FZ
Participant
Participant

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

0 Likes