{"id":205,"date":"2008-08-27T05:18:06","date_gmt":"2008-08-27T12:18:06","guid":{"rendered":"http:\/\/www.rodgreen.com\/?p=205"},"modified":"2008-08-27T05:18:06","modified_gmt":"2008-08-27T12:18:06","slug":"sourcing-a-python-script-through-mel","status":"publish","type":"post","link":"https:\/\/www.rodgreen.com\/?p=205","title":{"rendered":"Sourcing a Python script through Mel"},"content":{"rendered":"<p>So you want to do something python that&#8217;s not supported in Mel but are unsure how exactly to link the two.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p>OK so lets start with the python side of things.<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>Python:<\/p>\n<p>&nbsp;<\/p>\n<p>You have a file called &quot;myFirstScript.py&quot; inside your &#8216;Documentsmaya2008scripts&#8217; directory and you want to execute it.<\/p>\n<p>&nbsp;<\/p>\n<pre style=\"margin-left: 40px;\">\n#\tmyFirstScript.py\t#\n\n######################\n#\timports\n######################\n\nimport maya.mel as mel\n\n######################\n#\tfunctions\n######################\ndef helloWorld():\n\tprint('Hello World! (python native)')\n\tmel.eval('print &quot;Hello World! (mel via python)\\n&quot;')\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now that seems simple enough?&nbsp; From what most of the docs say all you need to type in a python window is:<\/p>\n<pre style=\"margin-left: 40px;\">\nimport myFirstScript\nmyFirstScript.helloWorld()\n<\/pre>\n<p>However you&#8217;ll find this doesn&#8217;t work! You&#8217;ll get an error something like:<\/p>\n<pre style=\"margin-left: 40px;\">\n# Error: line 1: No module named myFirstScript\n# Traceback (most recent call last):\n#&nbsp;&nbsp; File &quot;&lt;maya console&gt;&quot;, line 1, in &lt;module&gt;\n# ImportError: No module named myFirstScript #\n<\/pre>\n<p>For some reason Autodesk messed up the default paths for the python search path so you have to add one extra:<\/p>\n<pre style=\"margin-left: 80px;\">\nimport sys\nsys.path.append(path) # where path equals the full path to the place you put your script i.e.<\/pre>\n<pre style=\"margin-left: 80px;\">\nimport sys\nsys.path.append('C:\/Users\/Rod\/Documents\/maya\/2008\/scripts' ) # Note: No trailing slash!!!!\n<\/pre>\n<p>What Autodesk seem to have done is added an extra forward slash at the end of the path where python doesn&#8217;t want it.&nbsp; If you type &#8216;sys.path&#8217; in a python script window and press enter you&#8217;ll see the full list of default paths it&#8217;ll search through.<\/p>\n<pre style=\"margin-left: 40px;\">\n# Result: ['C:\\Program Files\\Autodesk\\Maya2008\\bin',\n'C:\\Program Files\\Autodesk\\Maya2008\\bin\\python25.zip',\n'C:\\Program Files\\Autodesk\\Maya2008\\Python\\DLLs',\n'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib',\n'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib\\plat-win',\n'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib\\lib-tk',\n'C:\\Program Files\\Autodesk\\Maya2008\\bin',\n'C:\\Program Files\\Autodesk\\Maya2008\\Python',\n'C:\\Program Files\\Autodesk\\Maya2008\\Python\\lib\\site-packages',\nu'C:\/Users\/Rod\/Documents\/maya\/2008\/prefs\/scripts',\nu'C:\/Users\/Rod\/Documents\/maya\/2008\/scripts\/',\nu'C:\/Users\/Rod\/Documents\/maya\/scripts'] #\n<\/pre>\n<p>Hopefully they fix it with the next update but until then if you want your python scripts in that dir or any other you&#8217;ll need to do the sys.path.append command.<\/p>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>Mel:<\/p>\n<p>Now we&#8217;ve got the python script importing correctly how do we execute this via Mel?<\/p>\n<pre style=\"margin-left: 40px;\">\npython(&quot;import myFirstScript&quot;); \/\/ Source or 'import' the myFirstScript.py as a new module\npython(&quot;myFirstScript.helloWorld()&quot;); \/\/ Execute the helloWorld function inside the myFirstScript module\n<\/pre>\n<p>&nbsp;<\/p>\n<p>That&#8217;s it!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So you want to do something python that&#8217;s not supported in Mel but are unsure how exactly to link the two.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,7,11,13,15],"tags":[54,37,41,56,58],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-art","category-maya-art","category-technical-art","category-tools","category-tutorials","tag-maya-art","tag-mel","tag-python","tag-technical-art","tag-tutorials"],"_links":{"self":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=205"}],"version-history":[{"count":0,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=\/wp\/v2\/posts\/205\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rodgreen.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}