<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Get directory of current script in Maya Programming Forum</title>
    <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7754361#M10409</link>
    <description>&lt;P&gt;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):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;def doIt():
    print "CurrentDir: ", __file__&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now if you want to use it in Maya you usually do&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import printScriptDir
printScriptDir.doIt()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Tue, 06 Feb 2018 08:50:40 GMT</pubDate>
    <dc:creator>haggi_master</dc:creator>
    <dc:date>2018-02-06T08:50:40Z</dc:date>
    <item>
      <title>Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7738949#M10403</link>
      <description>&lt;P&gt;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&lt;/P&gt;&lt;P&gt;I've tried:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;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]))&lt;/PRE&gt;&lt;P&gt;All of which point to: C:\Program Files\Autodesk\Maya2017\bin&lt;/P&gt;&lt;P&gt;Whereas I hoped that it would pijnt to something like: D:\myprojects\Maya\something\scripts&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas? Cheers&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 09:44:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7738949#M10403</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-01-31T09:44:14Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7739596#M10404</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;import maya.cmds as cmds&lt;BR /&gt;import os.path&lt;BR /&gt;&lt;BR /&gt;projectDirectory = cmds.workspace(q=True, rd=True)&lt;BR /&gt;if os.path.exists(projectDirectory+"scripts"):&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print (projectDirectory+"scripts")&lt;BR /&gt;else:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Current Workspace doesnt have Scriprs folder"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hope this will help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;BR /&gt;Rajasekaran Surjen.&lt;/P&gt;</description>
      <pubDate>Wed, 31 Jan 2018 13:29:35 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7739596#M10404</guid>
      <dc:creator>rajasekaransurjen</dc:creator>
      <dc:date>2018-01-31T13:29:35Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7742731#M10405</link>
      <description>&lt;P&gt;If you need the path of the script just use __file__.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g.:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;os.path.dirname(__file__)&lt;/PRE&gt;</description>
      <pubDate>Thu, 01 Feb 2018 12:18:27 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7742731#M10405</guid>
      <dc:creator>haggi_master</dc:creator>
      <dc:date>2018-02-01T12:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7751395#M10406</link>
      <description>&lt;P&gt;That works fine from the command pront, but doesn't work from within Maya&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import os
print os.path.dirname(__file__)
# Error: NameError: file &amp;lt;maya console&amp;gt; line 2: name '__file__' is not defined # &lt;/PRE&gt;</description>
      <pubDate>Mon, 05 Feb 2018 12:01:02 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7751395#M10406</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-05T12:01:02Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7751411#M10407</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 12:09:22 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7751411#M10407</guid>
      <dc:creator>haggi_master</dc:creator>
      <dc:date>2018-02-05T12:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7752230#M10408</link>
      <description>&lt;P&gt;Ok. Now I'm confused. &lt;img id="smileyfrustrated" class="emoticon emoticon-smileyfrustrated" src="https://forums.autodesk.com/i/smilies/16x16_smiley-frustrated.png" alt="Smiley Frustrated" title="Smiley Frustrated" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Say, for clarififcation that the script exists, and is dragged &amp;amp; dropped into the script editor it then doesn't seem to work.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Feb 2018 16:19:06 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7752230#M10408</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-02-05T16:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7754361#M10409</link>
      <description>&lt;P&gt;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):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;def doIt():
    print "CurrentDir: ", __file__&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now if you want to use it in Maya you usually do&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;import printScriptDir
printScriptDir.doIt()&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Feb 2018 08:50:40 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/7754361#M10409</guid>
      <dc:creator>haggi_master</dc:creator>
      <dc:date>2018-02-06T08:50:40Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/11632589#M10410</link>
      <description>&lt;P&gt;The way it worked for me is to get the path of the current plugin using cmds.pluginInfo.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;current_path = cmds.pluginInfo("&amp;lt;my_plugin&amp;gt;", query=True, path=True)
plugin_path = os.path.dirname(current_path)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Dec 2022 19:56:14 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/11632589#M10410</guid>
      <dc:creator>msilvaFT5HF</dc:creator>
      <dc:date>2022-12-20T19:56:14Z</dc:date>
    </item>
    <item>
      <title>Re: Get directory of current script</title>
      <link>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/12322927#M10411</link>
      <description>i don't think this is going to work because if you can import printScriptDir what is the problem?&lt;BR /&gt;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</description>
      <pubDate>Sun, 22 Oct 2023 18:11:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/maya-programming-forum/get-directory-of-current-script/m-p/12322927#M10411</guid>
      <dc:creator>omidt_gh</dc:creator>
      <dc:date>2023-10-22T18:11:39Z</dc:date>
    </item>
  </channel>
</rss>

