views:

171

answers:

2

Hi, I am new to Maya and trying to figure out how to use Python in Maya. I wanted to create a shelf in Maya and when I click that shelf, it will execute a file containing python code.

First thing, I figured out that we can't simply "source" python script. I followed tutorial from http://www.rtrowbridge.com/blog/2008/11/maya-python-import-scripts/, so now I have a function psource(). In my shelf, I can just call psource("myPythonScript")

My problem is I have to somehow register psource() when Maya first loaded. Do you have any idea how to do this?

Thanks!

A: 

I like to use

exec(open('c:\whatever\whatever\scriptname.py'))

See if that works for you! :)

Filhote
A: 

Which version of Maya are you running? If later than 8.5, Maya has python built in. Any python scripts you put in your local Maya script directory gets automatically sourced. You can inside the script editor source and run python scripts.

To automatically run: 1. Create a userSetup.mel file in myDocs\maya\mayaVersion\scripts 2. Inside the userSetup, use this syntax to import and run scripts:

python("from package import module"); python("module.method(\"passedVar1\", \"passedVar2\")");

Hope that helps

P.S Same syntax applies for shelf buttons. Just have to make sure that you have your python path set for Maya so that your code can be found. The local script directory is already included.....

Christian