PDA

View Full Version : Going modular w/Python


EightBit
02-15-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.

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

UI tools:
Say I have a UI panel that I use frequently, so I want to store it in an external script:
In myTools.py 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 jointFixer.py?

Thanks

NaughtyNathan
02-15-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 myTools.py
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 superNewTool.py
import myTools

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

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
mt.anotherFunction()
etc...

hope that helps...
:nathaN

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

CGTalk Moderation
02-17-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.