Get directory of current script

Get directory of current script

Anonymous
Not applicable
15,232 Views
8 Replies
Message 1 of 9

Get directory of current script

Anonymous
Not applicable

I'm trying to reference an image that's in the same directory as the script. There's no Maya file used or associated with the script

I've tried:

 

print os.path.dirname(os.path.realpath(sys.argv[0]))
print os.path.split(maya.cmds.__file__)[0]
print sys.argv[0]
print os.path.abspath(os.path.dirname(sys.argv[0]))

All of which point to: C:\Program Files\Autodesk\Maya2017\bin

Whereas I hoped that it would pijnt to something like: D:\myprojects\Maya\something\scripts

 

Any ideas? Cheers

0 Likes
Accepted solutions (1)
15,233 Views
8 Replies
Replies (8)
Message 2 of 9

rajasekaransurjen
Collaborator
Collaborator

Hi,

I think you want to print the current workspace's script path. The following script will print the current work space's script folder path if it is there.

 

 

import maya.cmds as cmds
import os.path

projectDirectory = cmds.workspace(q=True, rd=True)
if os.path.exists(projectDirectory+"scripts"):
    print (projectDirectory+"scripts")
else:
    print "Current Workspace doesnt have Scriprs folder"

 

 

 

 

hope this will help.

 

Best regards,
Rajasekaran Surjen.

0 Likes
Message 3 of 9

haggi_master
Advocate
Advocate

If you need the path of the script just use __file__.

 

e.g.:

 

os.path.dirname(__file__)
0 Likes
Message 4 of 9

Anonymous
Not applicable

That works fine from the command pront, but doesn't work from within Maya

 

import os
print os.path.dirname(__file__)
# Error: NameError: file <maya console> line 2: name '__file__' is not defined # 
0 Likes
Message 5 of 9

haggi_master
Advocate
Advocate

No it does not. In your original question you asked for a way to find out the path of the current script because you want to find an image in the same directory as the script. And the method works fine if you have a script file. If you try this from within maya you do not have a script file and so it cannot work.

0 Likes
Message 6 of 9

Anonymous
Not applicable

Ok. Now I'm confused. Smiley Frustrated

 

Say, for clarififcation that the script exists, and is dragged & dropped into the script editor it then doesn't seem to work.

0 Likes
Message 7 of 9

haggi_master
Advocate
Advocate
Accepted solution

The usual way if you want to execute a script file from within Maya is to import the module. Let's imagine you have a script file called printScriptDir.py and it contains a function called doIt() (the script contains __file__ but for some reason it is not visible in the script below):

 

 

def doIt():
    print "CurrentDir: ", __file__

 

Now if you want to use it in Maya you usually do

 

 

import printScriptDir
printScriptDir.doIt()

 

This way you have a script file which is executed and the __file__ variable works fine. But if you copy the content of the script file into Maya's script editor, you do not have a file any more and therefore the __file__ variable cannot work.

Message 8 of 9

msilvaFT5HF
Community Visitor
Community Visitor

The way it worked for me is to get the path of the current plugin using cmds.pluginInfo.

 

current_path = cmds.pluginInfo("<my_plugin>", query=True, path=True)
plugin_path = os.path.dirname(current_path)

 

Message 9 of 9

omidt_gh
Enthusiast
Enthusiast
i don't think this is going to work because if you can import printScriptDir what is the problem?
the problem is the current folder is not defined and we want to access exactly the current path. if we are able to import directly from the current path we do not have to add it to sys.path
0 Likes