How to source python scripts

2,939 views
Skip to first unread message

Beau Garcia

unread,
Nov 3, 2008, 9:42:43 PM11/3/08
to python_inside_maya
Hi,
Sorry to ask such a basic question, but basically i want to source and run a externally stored python script. Similarly to using "source melScript.mel; melScript;" commands in Mel. Ive tried doing this by saving the .py file as the same name as the main function used and then placing it in a scripts folder. Then after re-loading maya, I tried to run the function name ,  but no luck.  Any help would be great. Thanks

Beau

Shawn Patapoff

unread,
Nov 4, 2008, 12:28:52 PM11/4/08
to python_in...@googlegroups.com
Python uses import, and reload.

Sooo, if your script 'myScript.py' is in your scripts path you need to import it:
import myScript

then call a function in the script:
myScript.myFunc()

If you're editing scripts, you need to reload them to get any changes.

Hope that helps,
Cheers

kurian os ™ ®കോപ്പിയടിച്ചാല്©ഗോതമ്പുണ്ട!

unread,
Nov 4, 2008, 12:30:20 PM11/4/08
to python_in...@googlegroups.com
What Shawn is told is the goodway
but make sure you have __init__.py in your folder
--
സ്നേഹിക്കയില്ല ഞാന്‍
നോവുമാത്മാവിനെ സ്നേഹിച്ചിടാത്തൊരു
തത്വശാസ്ത്രത്തെയും

Sylvain Berger

unread,
Nov 4, 2008, 12:33:19 PM11/4/08
to python_in...@googlegroups.com
First you need to make sure that the path where the python file is save is in your python path, you can usse your computer environment variable or maya maya.env file.  I prefer using the maya.env file (located in your the maya pref folder)

Then let's say the python file is named myPythonFunctions.py and in this file you have the doStuff() function

in Maya you type:

# import all the function contained in the python script in the main namespace
import myPythonFunctions 
# make sure all the functions are up to date (this let's you edit the python file in an external editor without having to reload maya to update the script)
reload(myPythonFunctions)

# call the doStuf function
myPythonFunctions.soStuff()

Hope this helps


I always use this method, I edit in Eclipse and simply import, reload and execute my functions without having to connect Eclipse with Maya.
I usually create a shelf button containing the 3 lines, edit the script in eclipse, save, go in maya and hit the shelf button to run the command, go back in eclipse, edit some more, save ... you get the idea :)

Good luck
--
They say, "Evil prevails when good men fail to act." What they ought to say is, "Evil prevails."
Nicolas Cage as Yuri Orlov in Lord of War.

Shawn Patapoff

unread,
Nov 4, 2008, 12:43:44 PM11/4/08
to python_in...@googlegroups.com
What does the __init__.py do for Maya outside of a class? I several custom PYTHONPATH setup and I just use sys.path.append() to add any custom script paths in my userSetup.py. Any other workflow would be great to hear.

kurian os ™ ®കോപ്പിയടിച്ചാല്©ഗോതമ്പുണ്ട!

unread,
Nov 4, 2008, 12:48:17 PM11/4/08
to python_in...@googlegroups.com
If you are using svn setup or like env setting it will help lost like you can simply use
myScript.blalala

Sylvain Berger

unread,
Nov 4, 2008, 1:22:34 PM11/4/08
to python_in...@googlegroups.com
This is my setup:
all my scripts are located on my server  //myServer/pythonScripts
in the maya.env file I have added this path to the python path
PYTHONPATH =  //myServer/pythonScripts

in this folder I have sub folders (also called packages)
animation/
modeling/
rendering/
etc.

the root folder (pythonScript folder) and all the sub-folders have a __init__.py file (an empty file name __init__.py) , this basically tell python that these folders should be included by the import command

so if I want to use a function located in a script from the animation sub-folder (animation package) I type the following:
from animation import animFunctions

or if i want to acces a function located in a script located in the modeling sub-folder I type:
from animation import script01

then I can call the functions of these scripts
animFunctions.function1()
or 
script01.funciton1()

alternatively I can also rename the package in the import function.
from animation import animFunctions as anim
then call the function using the name
anim.function1()

I find this setup simple and clean, each scripts are located in a corresponding sub-folder yet they are all accessible and all this with only one PYTHONPATH setup.


Hope this makes sense

Farsheed Ashouri

unread,
Nov 4, 2008, 1:26:21 PM11/4/08
to python_in...@googlegroups.com
Use execfile function.

Sincerely,
Farsheed.


Beau Garcia

unread,
Nov 4, 2008, 7:03:09 PM11/4/08
to python_in...@googlegroups.com
Awesome, that was just what i was looking for. Thanks everyone for you help, its greatly appreciated! Cheers

Beau
Reply all
Reply to author
Forward
0 new messages