View Full Version : Going modular w/Python

02 February 2011, 03:19 PM
I work by myself, and I spend a lot of time writing python scripts.
Thus far I have only used self contained scripts, but I think I'd like to start modularizing.
Perhaps this is too much to ask, but could someone show me the code to get started?
There are 2 goals I'd like to accomplish - reuse defs() and reuse UI panels.

Say I want to be able call a def() that is sitting in a external script.
So in, I have
def addRotations():
my code...
How do I import addRotations into

UI tools:
Say I have a UI panel that I use frequently, so I want to store it in an external script:
In I have:
rotationTools = cmds.columnLayout(adj=1, cat = ('left', 1), cal=('left'))
misc layouts elements and controls...

How do import the rotationTools panel into the UI section of


02 February 2011, 04:37 PM
you do it exactly the same way you would do it anytime you use the import command and call functions like you do in Maya with maya.cmds... :)

e.g. you have a .py script called
def addRotations():
blah, blay py code...

def anotherFunction():
blah, blah, more py code, etc...
now, anytime you want to use either of those functions in a NEW py script you just import it and call it's functions:
this is now another script called
import myTools

def somenewFunction(*args):
x = 5
result = myTools.addRotation()
print result

and you can use all the other import tricks too, like:
import myTools as mt

def somenewFunction(*args):
x = 5
result = mt.addRotation()
print result

hope that helps...

02 February 2011, 06:35 AM
Thanks a lot Nathan, that's what I was looking for.

CGTalk Moderation
02 February 2011, 06:35 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.