PDA

View Full Version : A resource for learning Python


JaredTaylor
10-26-2010, 11:13 AM
Is this a good resource for learning python, and will it help me in accomplishing my goals?

My goals / what I want to know & do: I want to learn to write a GUI for my character rigs, and I want to write an auto rig.

I'd love it if someone took a look through the documentation and let me know if it's worth my time to learn or not. :)

If not, does anyone have a better resource out there that'll teach me what I want to know?

http://openbookproject.net/thinkcs/python/english2e/

I've been going through it, working things out, doing the exercises, but I still don't have a single clue how in the hell I'd start making an auto rigger or write a GUI for my character's rig.

Thanks

ginodauri
10-26-2010, 12:06 PM
Are you new to programing?
Don't expect to learn this things in one week or so.
You have to learn 3 things.
1.Building GUIs in maya
2.Things about maya attributes,connections...etc
3.Python as a tool to accomplished two things above.

This is good book for people new to programming:

http://www.amazon.com/Learning-Python-Second-Mark-Lutz/dp/0596002815

This one for people with some experience with programming:

http://www.amazon.com/Core-Python-Programming-Wesley-Chun/dp/tags-on-product/0132269937

surreal-reality
10-26-2010, 04:33 PM
Personally, I think the best way to learn is to start doing. Reading a book is helpful, but until you need a certain function, or aspect of python or anything else, you won't remember it until you can associate it to a use.

This Site (http://www.tutorialspoint.com/python/index.htm) + maya's python command reference is literally all I've ever needed material wise, to learn how to make gui's and some basic scripts in maya. I'm not an expert, but I taught my self from not knowing scripting in the least, to a decent amount from those 2 sources only.
Oh, and alot of echo all commands too : )

the command reference is really your best bet though for maya gui's specifically, since they have examples for every function.

Hope that helps!

JaredTaylor
10-26-2010, 08:53 PM
Hey guys, thank you for the links.

Yes I'm totally new to programming, did a little MEL but not enough to get by.

I'll see how I go with what you guys posted. In maya I could learn MEL by watching the commands it echo'd, but for python, it doesn't seem to echo any.

I suppose I should install pymel?

surreal-reality
10-26-2010, 09:19 PM
Sorry, to clarify about the echo all commands thing, yeah it does output mel, but since python is basically a wrapper for mel, the commands, flags, and data types are mostly the same, just a syntax difference.

for example:
polyExtrude -ch 1 -lz 1
in python would look like
cmds.polyExtrude(ch = 1, lz = 1)

once you know how to translate what's left between one and the other, it's alot more helpful.

also, put:
import maya.mel as mel
at the top of your script, and anything you can't end up getting to work in python(I've run into a few commands that mysteriously would error, or crash, etc) you can run directly, with:
mel.eval("polyExtrude -ch 1 -lz 1;")
which is basically pythons command to execute the string of a mel command.

JaredTaylor
10-26-2010, 09:29 PM
Thanks, thats so useful!

What command should I be using to build a GUI for:

1) an autorig
2) a character's rig UI

efecto
10-26-2010, 09:48 PM
Definitely check out Chad's python for maya documentation.

http://www.chadvernon.com/blog/resources/python-scripting-for-maya-artists/

I haven't read through myself but a couple of my colleagues have found it useful to pick up python.

Stellios
10-26-2010, 09:48 PM
Sorry, to clarify about the echo all commands thing, yeah it does output mel, but since python is basically a wrapper for mel, the commands, flags, and data types are mostly the same, just a syntax difference.

.


newbie quesiton: does this then mean there is nothing in python you cant do in MEL?

bendingiscool
10-26-2010, 11:00 PM
Only in terms of maya commands, python is handy for none maya based stuff that you may want to do within your scripts, I use its string and reg' ex' stuff loads.

Plus, it supports 'Object Orientated Programming' meaning you can split your code into handy chunks for larger programs.

cheers,
Chris

surreal-reality
10-26-2010, 11:03 PM
Virtualistic:

If you go to creativeCrash, and download some autorig scripts, you can get an idea of how to go about building a ui for that. Since every ui's different depending on how you want it, and what exactly you want in it, it's kind of hard to tell you the exact commands for making a ui.

Some people design character ui's in flash, and that's totally out of my league, sorry. They are much nicer to look at but you should be able to get something usable going just with maya's layouts styles.
Open your script editor and go to help > python command reference and under category browse around the window section. Everything related to ui is in there. Copy paste the examples at the end of each functions page, and see what it does, and what works best for you.

Stellios:

You're basically right, cmds.emit for example though fatally crashes maya if I try and emit color or anything but position. However, it's mel counterpart, with exact same variables / flags, works perfectly.

Another thing I like it for is functions, or commands built from many of maya's more basic functions. Like converting a selection of verts, to faces, or vice versa. There isn't a command for this in the python help docs, or even in the mel. I only found out about them by turning on echo all.

My point is something like that, there is no python counterpart, because it's made up of many functions. For me, this is the perfect time to use mel.eval()

JaredTaylor
10-26-2010, 11:16 PM
All the auto riggers I'm seeing there are mel, not python. Can you point me to a python one?

Edit: I found one, but it looks like a nightmare, I wouldn't know which ones to look at, because I can't tell if they're any good or not (or well written).

Cheers

JaredTaylor
10-27-2010, 09:56 AM
90% of the time that there's an error, it doesn't tell me which line it is on. Any way to change that?

Kobes
10-27-2010, 10:05 AM
Why don't you create it in MEL as you stated you had a basic understanding of that. Seems like a more logical starting point.

If your using the script editor to code in
Go to up to the History option menu. Then check the "line numbers in errors" option.

Kobes

pearblossom
10-28-2010, 03:58 PM
You can also go to the 'Python in Maya' google group and check out their resources under 'Getting Started'. This helped me a lot!

JaredTaylor
10-28-2010, 07:39 PM
Thanks for that--

I have a question, if I write a pymel script, can it only be used by people who have pymel installed?

nottoshabi
10-28-2010, 09:36 PM
If you want to learn python that is fine go right ahead. To build an auto rigger and Maya UI's mel is the way to go. That's why most of the auto riggers you see out there are done in mel. cause you don't need python to do simple joint placements and create curves and connection nodes.

I would use python to create a save weight plug in where Maya has to calculate a grip of information and spit it back to me at reasonable time. Mirroring blndShapes or creating pipeline tools something that can not be done in mel. Or other software needs to talk to Maya, I seen pipeline tools that are done in python and talk to Maya throw mel.

I think using maya to create an auto rigger is overly complicated. There are so many auto rigging scriopts done in mel, and they work perfectly. I did mine in mel, its supper easy to just adjust if I need to.

JaredTaylor
10-28-2010, 10:26 PM
Making a autorig and gui are my priorities, but I also want to learn python so I can become a decent TD one day; I want to be able to use a language that can be applied to more than just maya, and I'd like more capability than MEL has to offer. I've also been told that python is more user friendly and cleaner, whereas MEL can have ridiculous code for simple text operations in some areas.

I will eventually, most likely, learn MEL too, but it is not a priority to me at all, I see myself going farther by knowing python.

At the moment I'm trying to understand classes and how to use them, I get the general concept, I've gone through countless resources trying to figure out how to use them but I just don't get it.

When making a GUI it looks something like this, right?

class myUI()
def __init__(self)
self.name = "myUI"
self.title = "my user interface"

if (cmds.window(self.name, q=1, exists=1)): cmds.deleteUI(self.name)

Well, I understand that I'm making an object named myUI, I don't understand what __init__self is doing despite googling and finding a page on exactly that, I understand what self.name and self.title are doing, and the if command is checking if the window is open, I don't know what the q is, query?

But that's not even the real problem, I don't understand how to write -my own- classes. I don't know why the def init self is there, I don't know when to use what, and what I can use, it's all just nonsense to me, I can't make any sense of it.

Any ideas?

NaughtyNathan
10-28-2010, 11:20 PM
Jared, you are trying to do far too much far too quickly. forget all about classes, they are simply a different programming paradigm and arguably entirely unnecessary in the context of Maya scripting tasks (99% of tasks at least).

you need to start small. firstly for UI. if you haven't already done so, open up the Maya documentation and read the chapter on creating UIs, (User Guide > General > MEL and Expressions > Creating interfaces) the example code is all MEL, but the fundamentals it teaches you are the same in python, and basic python commands are almost identical to the MEL:

string $window = `window -w 200 -h 300 -tlb 1 "myWindow"`; //MEL
window = cmds.window('myWindow', w=200, h=300, tlb=1) #python
You don't have to learn MEL, but you should have an idea about how the syntax works, and how it translates to python.

open the python command documentation for window and look at the example code. And don't forget that every UI command has a document page that explains what it is, how it works, what it's options are and gives python example code that demonstrate how it can be used.

you shouldn't try to run before you can walk, and you seem dead-set on climbing into the pilot seat of a Cessna. first make sure you understand how to make a basic UI with buttons, sliders and whatever else you may need, without it actually doing anything in the scene. Keep asking Qs here of course, but you can learn ALL this just from the Maya documentation.

:nathaN

JaredTaylor
10-29-2010, 12:07 AM
Thanks a lot for that information, much appreciated.

I thought from what I've been doing that classes and such were necessary, guess I've been a bit mislead.

I've never really programmed before, however I have lived around code whether I'm using maya, linux, making websites-- so I understand a little of how to read it, just not how to write it.

When learning MEL I just copied the commands out of the script editor, I have been going through the maya documentation too for python. And it's helping a lot.

But like you said I'm jumping in the deep end of the pool - Something I always do, it's like my own curse.

For now I will work on building basic UIs that do nothing as you suggested, then slowly work on implementing features as I go and figure out how MEL translates into python.

By the way, is it easy to create a custom object, that doesn't exist in Maya, like a custom locator? I'm assuming you need to access openGL and that it would be very hard. Just something that I've been curious about, because I saw 3dbuzz's autorigging tutorial on their website (can't afford it though) and they were using custom objects.

Thanks a heap.

JaredTaylor
10-29-2010, 06:39 AM
Thanks to the feedback here I have made some progress, and I think it might be useful to update my progress as I go, so here's my script so far.

I want to have tabs, so I'm going to change it for that. Also the commands on the buttons won't actually be like the way they are, I was just thinking of the order I wanted to do things.

I have a huge problem, the code below won't open the window unless I run it without the cmds.deleteUI part the first time. How do I make it so it deletes the window if it's open, while having it actually open the window and not stopping with an error saying that UI doesn't exist, it's like it's not even seeing the "IF" part, if means if, not always!

It works perfectly if the window is already open!

import maya.cmds as cmds # create variables
bt = cmds.button
tx = cmds.text
# create UI
if cmds.window(myUI, exists=True) : cmds.deleteUI(myUI)
myUI = cmds.window(title='Custom Rigging Interface V 0.01 - by Jared Taylor', iconName='CRI', w=400, h=650)
cmds.columnLayout(columnAttach=('both', 5), rowSpacing=5, columnWidth=390)
tx('----------------------- PRERIG -----------------------', bgc=(.354,.405,.662))
bt(label='Step One: Create Skeleton')
bt(label='Step Two: Orient Joints')
tx('----------------------- SPINE -----------------------', bgc=(.354,.405,.662))
bt(label='Step Three: Rig IK Spine & Create FK Spine joints')
bt(label='Step Four: Rig FK Spine')
tx('----------------------- LEGS -----------------------', bgc=(.354,.405,.662))
bt(label='Step Five: Rig IK Legs')
bt(label='Step Six: Create RL Foot Joints')
bt(label='Step Seven: Mirror & Orient RL Foot Joints')
tx('Step Eight: Manually orient the RL toe joint to be FLAT with the ground', bgc=(1,.405,.662))
bt(label='Step Nine: Rig RL Foot')
tx('----------------------- ARMS -----------------------', bgc=(.354,.405,.662))
bt(label='Step Ten: Rig IK Arms')
bt(label='Step Eleven: Rig FK Arms')
bt(label='Step Twelve: Finalize Arm Rig')
tx('----------------------- HANDS -----------------------', bgc=(.354,.405,.662))
tx('Step Thirteen: Manually orient thumb so that it curls into the hand on Z axis', bgc=(1,.405,.662))
bt(label='Step Thirteen: Rig Three Finger One Thumb Hand')
bt(label='Step Fourteen: Rig Four Finger One Thumb Hand')
tx('----------------------- STRETCH -----------------------', bgc=(.354,.405,.662))
bt(label='Step Fifteen: Rig Stretchy Spine')
bt(label='Step Sixteen: Rig Stretchy Legs')
bt(label='Step Seventeen: Rig Stretchy Arms')
cmds.showWindow(myUI)


And here is my edited UI:

Still need help with the above issues if anyone would be so kind :)

edit: Also the width and height parameters don't work. I restarted maya and still no luck. I've got width set to 800 and height to 100, it's still using the old heights/widths.

import maya.cmds as cmds
# create variables

bt = cmds.button
tx = cmds.text


# create UI
myUI=cmds.window(title='Custom Rigging Interface V 0.01 - by Jared Taylor', iconName='CRI', width=400, height=100)
form = cmds.formLayout()
tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)
cmds.formLayout( form, edit=True, attachForm=((tabs, 'top', 0), (tabs, 'left', 0), (tabs, 'bottom', 0), (tabs, 'right', 0)) )

child1 = cmds.rowColumnLayout('Part One: PreRig')
tx('----------------------- PRERIG -----------------------', bgc=(.354,.405,.662))
bt(label='Step One: Create Skeleton')
bt(label='Step Two: Orient Joints')
bt(label='Step Three: Create Heirarchy')
tx('Step Four: Place geometry into appropriate groups in heirarchy', align='left', bgc=(1,.405,.662))
tx(' The geometry that will be bound goes into the bound', align='left', bgc=(1,.405,.662))
tx(' geo group, the geo that is constrained goes into the', align='left', bgc=(1,.405,.662))
tx(' scaleable geo group', align='left', bgc=(1,.405,.662))
cmds.setParent( '..' )
child2 = cmds.rowColumnLayout('Part Two: Spine')
tx('----------------------- SPINE -----------------------', bgc=(.354,.405,.662))
bt(label='Step One: Rig IK Spine & Create FK Spine joints')
bt(label='Step Two: Rig FK Spine')
bt(label='Step Three: Clean Up Heirarchy')
cmds.setParent( '..' )
child3 = cmds.rowColumnLayout('Part Three: Legs && Feet')
tx('----------------------- LEGS -----------------------', bgc=(.354,.405,.662))
bt(label='Step One: Rig IK Legs')
tx('----------------------- FEET -----------------------', bgc=(.354,.405,.662))
bt(label='Step Two: Create RL Foot Joints')
bt(label='Step Three: Mirror & Orient RL Foot Joints')
tx('Step Four: Manually orient the RL toe joint to be FLAT with the ground', bgc=(1,.405,.662))
bt(label='Step Five: Rig RL Foot')
cmds.setParent( '..' )
child4 = cmds.rowColumnLayout('Part Four: Arms && Hands')
tx('----------------------- ARMS -----------------------', bgc=(.354,.405,.662))
bt(label='Step Ten: Rig IK Arms')
bt(label='Step Eleven: Rig FK Arms')
bt(label='Step Twelve: Finalize Arm Rig')
tx('----------------------- HANDS -----------------------', bgc=(.354,.405,.662))
tx('Step Thirteen: Manually orient thumb so that it curls into the hand on Z axis', bgc=(1,.405,.662))
bt(label='Step Thirteen: Rig Three Finger One Thumb Hand')
bt(label='Step Fourteen: Rig Four Finger One Thumb Hand')
cmds.setParent( '..' )
child5 = cmds.rowColumnLayout('Part Five: Squash and Stretch')
tx('----------------------- STRETCH -----------------------', bgc=(.354,.405,.662))
bt(label='Step Fifteen: Rig Stretchy Spine')
bt(label='Step Fifteen: Rig Squashy Spine')
bt(label='Step Sixteen: Rig Stretchy Legs')
bt(label='Step Seventeen: Rig Stretchy Arms')


cmds.tabLayout(tabs)

cmds.showWindow(myUI)

NateH
10-29-2010, 07:56 AM
That's a known *issue (Feature) of Maya, when you create a window, the width/height only effects the size the first time the script is run (Assuming the window has a unique name). That's so that windows can remember the size/position the user sets for them.

You can work around this by setting the size after it is shown by editing the window in place:
window myWindow -e -wh W H;

JaredTaylor
10-29-2010, 07:59 AM
That's a known *issue (Feature) of Maya, when you create a window, the width/height only effects the size the first time the script is run (Assuming the window has a unique name). That's so that windows can remember the size/position the user sets for them.

You can work around this by setting the size after it is shown by editing the window in place:
window myWindow -e -wh W H;

Great to know!

But that's not it, even on startup, it wont set to the correct width/height, and that comment doesn't work either. I'm using it this way:

cmds.window(myUI, e=True, w=800, h=100)

AAAH it's remembering the height/width even when I restart maya, just resized it and tested.

edit: oh my, I put the code before showWindow, silly me.

JaredTaylor
10-29-2010, 01:20 PM
Aah help, I can't set rotation orders with python!

I'm trying

import maya.mel as mm mm.setAttr("l_indA_jnt")


But... its just saying the module doesn't have setAttr.

Silly me, the answer was in this very thread :/ late nights, you know...

import maya.mel as mel mel.eval("setAttr l_indA_jnt.rotateOrder 3;")

NaughtyNathan
10-29-2010, 01:40 PM
setAttr is a maya command, so appears in maya.cmds along with all the other commands we think of as "MEL".

you are already importing maya cmds, you only need to import maya.mel when you want to issue some MEL script, not when using a Maya command. yeah I know, it is kinda confusing to a programming n00b, sorry.

import maya.cmds as cmds
cmds.setAttr('left_arm.rotateX', 45)
You also need to get the hang of getting node names as variables and operating on those variables, rather than hard-coding specific object names, which is a useless dead-end programatically.

for example, don't create a locator called "left_arm" and then reference this:

# this is how NOT to do it:
cmds.spaceLocator(n='left_arm')
cmds.setAttr('left_arm.rotateX', 45)

#this is how to do it:
myLocator = cmds.spaceLocator(n='left_arm')
cmds.setAttr(myLocator+'.rotateX', 45)
By all means still name your nodes, but you never know with 100% certainty that maya WILL actually name your node what you told it to, so grabbing the name as a variable and then referencing this ensures you are affecting the right node. Besides, if you decide to change the name in your code, you only have to modify one line of code.

and finally, I have NO IDEA where you picked up that crazy "alias" method of programming but please ditch it. No-one codes like that, it obfuscates your code, making it harder to read and practically impossible to reuse properly, let alone test in isolation.

:nathaN

JaredTaylor
10-29-2010, 01:49 PM
I didn't want to rotate the arm, I wanted to change the rotation order, research tells me there's a bug and it says it doesn't exist (it does), so I just used the mel.eval() command.

I am now going through my "code" and setting things as variables, not aliases.

Am I doing it right?

Or am I meant to also replace all the 'cj with cmds.createJoint' (an example) also?

# create skeleton

import maya.cmds as cmds
import sys
import maya.mel as mel

bt = cmds.button
tx = cmds.text
sl = cmds.select
cj = cmds.joint
sa = cmds.setAttr

### START CREATE SKELETON SCRIPT

def war(message):
sys.stdout.write(message)

def crSkeletonFour(self):
# create joints for spine
hipIK = cj(n='hipIK_jnt', p=(0, 10, 0))
spineIK1 = cj(n='spineIK_1_jnt', p=(0, 12, 0))
spineIK2 = cj(n='spineIK_2_jnt', p=(0, 14, 0))
spineIK3 = cj(n='spineIK_3_jnt', p=(0, 16, 0))
spineIK4 = cj(n='spineIK_4_jnt', p=(0, 18, 0))
chestIK = cj(n='chestIK_jnt', p=(0, 20, 0))
neckBase = cj(n='neckBase_jnt', p=(0,22,0))
neck = cj(n='neck_jnt', p=(0,23,0))
head = cj(n='head_jnt', p=(0,26,0))
headTip = cj(n='headTip_joint', p=(0,30,0))
# create joints for arm
sl(chestIK)
lClav = cj(n='l_clav_jnt', p=(1, 21, 0))
lShldr = cj(n='l_shldr_jnt', p=(2, 21, 0))
lElbow = cj(n='l_elbow_jnt', p=(6, 21, 0))
lForearm = cj(n='l_forearm_jnt', p=(8, 21, 0))
lWrist = cj(n='l_wrist_jnt', p=(10, 21, 0))
lHand = cj(n='l_hand_jnt', p=(11, 21, 0))
# create joints for the leg
sl(hipIK)

NaughtyNathan
10-29-2010, 02:10 PM
it doesn't matter what you actually wanted to do with setAttr Jared, it was just an example to show you how to use the setAttr command. By "alias" programming I'm talking about this:


bt = cmds.button

bt(l='label', etc...)
This is just plain odd. I can only assume you are doing it because it's less typing, but it's contrary to all good coding style conventions, and how 99% of code is written. I can practically guarentee that far fewer people here are likely to help you with your code if that's how it looks.

The only thing you are going to gain from coding like this is confusion.

:nathaN

JaredTaylor
10-29-2010, 02:15 PM
I thought that was how people did it, I guess I've been mislead and I'll change it ASAP, I don't want to form any bad habits this early on.

I'm a little bit stuck though. If you take a look through my code, I've defined each joint as a variable, but I want to mirror those joints and somehow have the variable's set too.

Do I have to go through each joint and type ' rShldr = r_shldr_jnt ' ? I'm sure there's a better way.

Is this accurate now?

My code looks like this:

Edit: Also, I can't use:

cmds.mirrorJoint(lClav, mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))

Has to be a string not a variable I think. Is there a way around that? I'm sure there is.

# create skeleton

import maya.cmds as cmds
import sys
import maya.mel as mel

### START CREATE SKELETON SCRIPT

def war(message):
sys.stdout.write(message)

def crSkeletonFour(self):
# create joints for spine
hipIK = cmds.joint(n='hipIK_jnt', p=(0, 10, 0))
spineIK1 = cmds.joint(n='spineIK_1_jnt', p=(0, 12, 0))
spineIK2 = cmds.joint(n='spineIK_2_jnt', p=(0, 14, 0))
spineIK3 = cmds.joint(n='spineIK_3_jnt', p=(0, 16, 0))
spineIK4 = cmds.joint(n='spineIK_4_jnt', p=(0, 18, 0))
chestIK = cmds.joint(n='chestIK_jnt', p=(0, 20, 0))
neckBase = cmds.joint(n='neckBase_jnt', p=(0,22,0))
neck = cmds.joint(n='neck_jnt', p=(0,23,0))
head = cmds.joint(n='head_jnt', p=(0,26,0))
headTip = cmds.joint(n='headTip_joint', p=(0,30,0))
# create joints for arm
cmds.select(chestIK)
lClav = cmds.joint(n='l_clav_jnt', p=(1, 21, 0))
lShldr = cmds.joint(n='l_shldr_jnt', p=(2, 21, 0))
lElbow = cmds.joint(n='l_elbow_jnt', p=(6, 21, 0))
lForearm = cmds.joint(n='l_forearm_jnt', p=(8, 21, 0))
lWrist = cmds.joint(n='l_wrist_jnt', p=(10, 21, 0))
lHand = cmds.joint(n='l_hand_jnt', p=(11, 21, 0))
# create joints for the leg
cmds.select(hipIK)
lHip = cmds.joint(n='l_hip_jnt', p=(3,10,0))
lKnee = cmds.joint(n='l_knee_jnt', p=(3,6,0))
lAnkle = cmds.joint(n='l_ankle_jnt', p=(3,2,0))
lFoot = cmds.joint(n='l_foot_jnt', p=(3,0,2))
lToe = cmds.joint(n='l_toe_jnt', p=(3,0,4))
cmds.select(cl=True)
lRLHeel = cmds.joint(n='l_rlHeel_jnt', p=(3,0,0))
# create joints for four fingers
cmds.select(lHand)
lMidA = cmds.joint(n='l_midA_jnt', p=(12,21,0))
lMidB = cmds.joint(n='l_midB_jnt', p=(13,21,0))
lMidC = cmds.joint(n='l_midC_jnt', p=(14,21,0))
lMidD = cmds.joint(n='l_midD_joint', p=(15,21,0))
cmds.select(lHand)
lPnkA = cmds.joint(n='l_pnkA_jnt', p=(12,21,-1))
lPnkB = cmds.joint(n='l_pnkB_jnt', p=(13,21,-1))
lPnkC = cmds.joint(n='l_pnkC_jnt', p=(14,21,-1))
lPnkD = cmds.joint(n='l_pnkD_joint', p=(15,21,-1))
cmds.select(lHand)
lIndA = cmds.joint(n='l_indA_jnt', p=(12,21,1))
lIndB = cmds.joint(n='l_indB_jnt', p=(13,21,1))
lIndC = cmds.joint(n='l_indC_jnt', p=(14,21,1))
lIndD = cmds.joint(n='l_indD_joint', p=(15,21,1))
cmds.select(lHand)
lThbA = cmds.joint(n='l_thbA_jnt', p=(11.261,21,0.939))
lThbB = cmds.joint(n='l_thbB_jnt', p=(11.969,21,1.693))
lThbC = cmds.joint(n='l_thbC_jnt', p=(12.659,21,2.426))
lThbD = cmds.joint(n='l_thbD_joint', p=(13.665,21,3.498))
cmds.select(cl=True)


war('Four Finger Skeleton Built Successfully')


def crSkeletonFive(self):
# create joints for spine
hipIK = cmds.joint(n='hipIK_jnt', p=(0, 10, 0))
spineIK1 = cmds.joint(n='spineIK_1_jnt', p=(0, 12, 0))
spineIK2 = cmds.joint(n='spineIK_2_jnt', p=(0, 14, 0))
spineIK3 = cmds.joint(n='spineIK_3_jnt', p=(0, 16, 0))
spineIK4 = cmds.joint(n='spineIK_4_jnt', p=(0, 18, 0))
chestIK = cmds.joint(n='chestIK_jnt', p=(0, 20, 0))
neckBase = cmds.joint(n='neckBase_jnt', p=(0,22,0))
neck = cmds.joint(n='neck_jnt', p=(0,23,0))
head = cmds.joint(n='head_jnt', p=(0,26,0))
headTip = cmds.joint(n='headTip_joint', p=(0,30,0))
# create joints for arm
cmds.select(chestIK)
lClav = cmds.joint(n='l_clav_jnt', p=(1, 21, 0))
lShldr = cmds.joint(n='l_shldr_jnt', p=(2, 21, 0))
lElbow = cmds.joint(n='l_elbow_jnt', p=(6, 21, 0))
lForearm = cmds.joint(n='l_forearm_jnt', p=(8, 21, 0))
lWrist = cmds.joint(n='l_wrist_jnt', p=(10, 21, 0))
lHand = cmds.joint(n='l_hand_jnt', p=(11, 21, 0))
# create joints for the leg
cmds.select(hipIK)
lHip = cmds.joint(n='l_hip_jnt', p=(3,10,0))
lKnee = cmds.joint(n='l_knee_jnt', p=(3,6,0))
lAnkle = cmds.joint(n='l_ankle_jnt', p=(3,2,0))
lFoot = cmds.joint(n='l_foot_jnt', p=(3,0,2))
lToe = cmds.joint(n='l_toe_jnt', p=(3,0,4))
cmds.select(cl=True)
lRLHeel = cmds.joint(n='l_rlHeel_jnt', p=(3,0,0))
# create joints for four fingers
cmds.select(lHand)
lMidA = cmds.joint(n='l_midA_jnt', p=(12,21,0.333))
lMidB = cmds.joint(n='l_midB_jnt', p=(13,21,0.333))
lMidC = cmds.joint(n='l_midC_jnt', p=(14,21,0.333))
lmidD = cmds.joint(n='l_midD_joint', p=(15,21,0.333))
cmds.select(lHand)
lPnkA = cmds.joint(n='l_pnkA_jnt', p=(12,21,-1))
lPnkB = cmds.joint(n='l_pnkB_jnt', p=(13,21,-1))
lPnkC = cmds.joint(n='l_pnkC_jnt', p=(14,21,-1))
lPnkD = cmds.joint(n='l_pnkD_joint', p=(15,21,-1))
cmds.select(lHand)
lIndA = cmds.joint(n='l_indA_jnt', p=(12,21,1))
lIndB = cmds.joint(n='l_indB_jnt', p=(13,21,1))
lIndC = cmds.joint(n='l_indC_jnt', p=(14,21,1))
lIndD = cmds.joint(n='l_indD_joint', p=(15,21,1))
cmds.select('lHand')
lRngA = cmds.joint(n='l_rngA_jnt', p=(12,21,-0.333))
lRngB = cmds.joint(n='l_rngB_jnt', p=(13,21,-0.333))
lRngC = cmds.joint(n='l_rngC_jnt', p=(14,21,-0.333))
lRngD = cmds.joint(n='l_rngD_joint', p=(15,21,-0.333))
cmds.select(lHand)
lThbA = cmds.joint(n='l_thbA_jnt', p=(11.261,21,0.939))
lThbB = cmds.joint(n='l_thbB_jnt', p=(11.969,21,1.693))
lThbC = cmds.joint(n='l_thbC_jnt', p=(12.659,21,2.426))
lThbD = cmds.joint(n='l_thbD_joint', p=(13.665,21,3.498))
cmds.select(cl=True)
war('Five Finger Skeleton Built Successfully')

### START MIRROR JOINTS SCRIPT ###

def mrJoints(self):
cmds.mirrorJoint(lClav, mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))
cmds.mirrorJoint(lHip, mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))
cmds.mirrorJoint(lRLHeel, mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))

war('Joints Mirrored Successfully')

### START ORIENT JOINTS SCRIPT ###

def orJoints(self):
cmds.joint('l_clav_jnt', e=True, oj='xyz', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('l_hip_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('r_clav_jnt', e=True, oj='xyz', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('r_hip_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('neckBase_jnt', e=True, oj='yxz', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('l_hand_jnt', 'r_hand_jnt', e=True, oj='none', secondaryAxisOrient='xup', zso=True)
cmds.joint('l_indA_jnt', 'l_midA_jnt', 'l_pnkA_jnt', 'l_thbA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('r_indA_jnt', 'r_midA_jnt', 'r_pnkA_jnt', 'r_thbA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)

if cmds.objExists('l_rngA_jnt'):
cmds.joint('l_rngA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
mel.eval("setAttr l_rngA_jnt.rotateOrder 3;")
mel.eval("setAttr l_rngB_jnt.rotateOrder 3;")
mel.eval("setAttr l_rngC_jnt.rotateOrder 3;")

if cmds.objExists('r_rngA_jnt'):
cmds.joint('l_rngA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
mel.eval("setAttr r_rngA_jnt.rotateOrder 3;")
mel.eval("setAttr r_rngB_jnt.rotateOrder 3;")
mel.eval("setAttr r_rngC_jnt.rotateOrder 3;")

mel.eval("setAttr l_indA_jnt.rotateOrder 3;")
mel.eval("setAttr l_indB_jnt.rotateOrder 3;")
mel.eval("setAttr l_indC_jnt.rotateOrder 3;")
mel.eval("setAttr l_midA_jnt.rotateOrder 3;")
mel.eval("setAttr l_midB_jnt.rotateOrder 3;")
mel.eval("setAttr l_midC_jnt.rotateOrder 3;")
mel.eval("setAttr l_pnkA_jnt.rotateOrder 3;")
mel.eval("setAttr l_pnkB_jnt.rotateOrder 3;")
mel.eval("setAttr l_pnkC_jnt.rotateOrder 3;")
mel.eval("setAttr l_thbA_jnt.rotateOrder 3;")
mel.eval("setAttr l_thbB_jnt.rotateOrder 3;")
mel.eval("setAttr l_thbC_jnt.rotateOrder 3;")

mel.eval("setAttr r_indA_jnt.rotateOrder 3;")
mel.eval("setAttr r_indB_jnt.rotateOrder 3;")
mel.eval("setAttr r_indC_jnt.rotateOrder 3;")
mel.eval("setAttr r_midA_jnt.rotateOrder 3;")
mel.eval("setAttr r_midB_jnt.rotateOrder 3;")
mel.eval("setAttr r_midC_jnt.rotateOrder 3;")
mel.eval("setAttr r_pnkA_jnt.rotateOrder 3;")
mel.eval("setAttr r_pnkB_jnt.rotateOrder 3;")
mel.eval("setAttr r_pnkC_jnt.rotateOrder 3;")
mel.eval("setAttr r_thbA_jnt.rotateOrder 3;")
mel.eval("setAttr r_thbB_jnt.rotateOrder 3;")
mel.eval("setAttr r_thbC_jnt.rotateOrder 3;")

war('Four Finger Skeleton Joints Oriented Successfully')




And just to make sure I'm doing this right, here's my UI code:

import maya.cmds as cmds

from cri_crSkeleton import *




# print to status line with command WAR

def war(message):
sys.stdout.write(message)



# create UI
myUI=cmds.window(title='Custom Rigging Interface V 0.1 - by Jared Taylor', iconName='CRI', width=385, height=100)
form = cmds.formLayout()
tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)
cmds.formLayout( form, edit=True, attachForm=((tabs, 'top', 0), (tabs, 'left', 0), (tabs, 'bottom', 0), (tabs, 'right', 0)) )

child1 = cmds.rowColumnLayout('Part One: PreRig')
cmds.text('----------------------- PRERIG -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step One: Create Skeleton with four fingers', bgc=(0,0.7,0.3), command=crSkeletonFour)
cmds.button(label='Step One: Create Skeleton with five fingers', bgc=(0.0,0.3,0.7), command=crSkeletonFive)
cmds.button(label='Step Two: Mirror Joints', bgc=(0.6,0.6,0.5),command=mrJoints)
cmds.button(label='Step Three: Orient Joints', bgc=(0,0.7,0.3), command=orJoints)
cmds.button(label='Step Four: Create Heirarchy', bgc=(0.6,0.6,0.5))
cmds.text('Step Five: Place geometry into appropriate groups in heirarchy', align='left', bgc=(1,.405,.662))
cmds.text(' The geometry that will be bound goes into the bound', align='left', bgc=(1,.405,.662))
cmds.text(' geo group, the geo that is constrained goes into the', align='left', bgc=(1,.405,.662))
cmds.text(' scaleable geo group', align='left', bgc=(1,.405,.662))
cmds.setParent( '..' )
child2 = cmds.rowColumnLayout('Part Two: Spine')
cmds.text('----------------------- SPINE -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step One: Rig IK Spine && Create FK Spine joints', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Two: Orient FK Spine Joints', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Two: Rig FK Spine', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Three: Clean Up Heirarchy', bgc=(0.6,0.6,0.5))
cmds.setParent( '..' )
child3 = cmds.rowColumnLayout('Part Three: Legs && Feet')
cmds.text('----------------------- LEGS -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step One: Rig IK Legs', bgc=(0.6,0.6,0.5))
cmds.text('----------------------- FEET -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step Two: Create RL Foot Joints', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Three: Mirror & Orient RL Foot Joints', bgc=(0.6,0.6,0.5))
cmds.text('Step Four: Manually orient the RL toe joint to be FLAT with the ground', bgc=(1,.405,.662))
cmds.button(label='Step Five: Rig RL Foot', bgc=(0.6,0.6,0.5))
cmds.setParent( '..' )
child4 = cmds.rowColumnLayout('Part Four: Arms && Hands')
cmds.text('----------------------- ARMS -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step Ten: Rig IK Arms', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Eleven: Rig FK Arms', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Twelve: Finalize Arm Rig', bgc=(0.6,0.6,0.5))
cmds.text('----------------------- HANDS -----------------------', bgc=(.354,.405,.662))
cmds.text('Step Thirteen: Manually orient thumb so that it curls into the hand on Z axis', bgc=(1,.405,.662))
cmds.button(label='Step Thirteen: Rig Four Finger Hand', bgc=(0,0.7,0.3))
cmds.button(label='Step Fourteen: Rig Five Finger Hand', bgc=(0,0.3,0.7))
cmds.setParent( '..' )
child5 = cmds.rowColumnLayout('Part Five: Squash and Stretch')
cmds.text('----------------------- STRETCH -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step Fifteen: Rig Stretchy Spine', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Fifteen: Rig Squashy Spine', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Sixteen: Rig Stretchy Legs', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Seventeen: Rig Stretchy Arms', bgc=(0.6,0.6,0.5))
cmds.setParent( '..' )
child7 = cmds.rowColumnLayout('Part Six: Binding')
cmds.button(label='Select Joints to be Weighted: Four Fingers', bgc=(0,0.7,0.3), h=100)
cmds.button(label='Select Joints to be Weighted: Five Fingers', bgc=(0,0.3,0.7), h=100)
cmds.setParent( '..' )

cmds.tabLayout(tabs)

cmds.showWindow(myUI)

cmds.window(myUI, e=True, w=385, h=100)

# end of script


I'm a total beginner, only ever copied commands out of MEL, probably did that wrong too, so this is all a learning experience for me. I have no idea of proper programming etiquette.

NaughtyNathan
10-29-2010, 02:58 PM
that's all looking far better now, however, you don't need "self" in the function definitions (you only need that when working with classes), and you still could do with cleaning out all those mel.evals. Also, you could ditch the war function and just use the print command.

as for the variable names - the problem you have, which I didn't appreciate at first, is that you need to reference the names of the joints you create inside a different function, so the variable names are no longer "in scope". There are several ways you could handle this, but I think for simplicities sake for now as you are still learning the basics, just use the hard-coded names as you have already done.

:nathaN

JaredTaylor
10-29-2010, 03:03 PM
that's all looking far better now, however, you don't need "self" in the function definitions (you only need that when working with classes), and you still could do with cleaning out all those mel.evals. Also, you could ditch the war function and just use the print command.

as for the variable names - the problem you have, which I didn't appreciate at first, is that you need to reference the names of the joints you create inside a different function, so the variable names are no longer "in scope". There are several ways you could handle this, but I think for simplicities sake for now as you are still learning the basics, just use the hard-coded names as you have already done.

:nathaN

Thanks for the info,

Print doesn't print to the status line, only the script editor in python, it does in mel. The war function prints to the status line.

I'll do as you suggest for the joints, but could you provide a sample script for mirroring joints... if you have the time and patience :) I have scoured all of google, and it just doesn't exist.

Cheers

NaughtyNathan
10-29-2010, 03:09 PM
Print doesn't print to the status line, only the script editor in python, it does in mel.yes it does, it's just that all prints in python have a newline added automatically. To circumvent this (and you will need to know this, not just for printing to the status line in Maya) put a , at the end:

print 'Joints created successfully!',
which shows up fine in the status line (no newline!).

I have no experience with joints I'm afraid so I have no idea what a mirror joint script is supposed to do. doesn't the mirrorJoint command do what you want?

:nathaN

JaredTaylor
10-29-2010, 03:15 PM
Yep, that's what I'm using.

But here's the dilemma.

I define each joint being created for the center and left side as a variable.

When I use mirrorJoint it mirrors the entire chain, so if I mirror the l_clav_jnt it will mirror the shoulder, elbow, hand, fingers etc., and I don't know how to set those as variables too.

I know you said to just do it the hard coded way, but I'd like to know how to do it properly.

Thanks for the tip about the print command, I read a thread here on cgtalk that told me to do it this way :/

JaredTaylor
10-29-2010, 04:21 PM
My script wont work, I made it clean up by grouping several things, but it wont run, because the object it is going to group doesn't exist yet. I can't even bring up the UI to make the things. It's looking for them before they exist.

Also out of curiosity, how would I print something like this as l_r_hip_jnt

def left():
'l_'
def right():
'r_'
def hip():
'hip_jnt'

print left right hip

JaredTaylor
10-29-2010, 11:47 PM
I think I'll elaborate on my above problem.

Here's my scripts, and keep in mind, the GUI buttons are examples of what I want, I haven't cleaned them up, and I'm now using the if command to check whether there's 4/5 fingers rather than providing separate buttons (just saying so I don't look like as much a noob as I want .5 days ago!)

So what is the problem?

Well, when I try to run my GUI it wont run! It tells me it can't find the hipIK_jnt, because it doesn't exist. Of course it doesn't exist, you have to press the button to make it exist. I don't think I can figure it out, but I'm going to play with the if commands and what not and see what happens.

Here is my GUI script:

import maya.cmds as cmds

from cri_crSkeleton import *





# create UI
myUI=cmds.window(title='Custom Rigging Interface V 0.1 - by Jared Taylor', iconName='CRI', width=385, height=100)
form = cmds.formLayout()
tabs = cmds.tabLayout(innerMarginWidth=5, innerMarginHeight=5)
cmds.formLayout( form, edit=True, attachForm=((tabs, 'top', 0), (tabs, 'left', 0), (tabs, 'bottom', 0), (tabs, 'right', 0)) )

child1 = cmds.rowColumnLayout('Part One: PreRig')
cmds.text('----------------------- PRERIG -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step One: Create Skeleton with four fingers', bgc=(0,0.7,0.3), command=crSkeletonFour)
cmds.button(label='Step One: Create Skeleton with five fingers', bgc=(0.0,0.3,0.7), command=crSkeletonFive)
cmds.button(label='Step Two: Mirror Joints', bgc=(0.6,0.6,0.5), command=mrJoints)
cmds.button(label='Step Three: Orient Joints', bgc=(0,0.7,0.3), command=orJoints)
cmds.button(label='Step Four: Create Heirarchy', bgc=(0.6,0.6,0.5), command=heirFix)
cmds.text('Step Five: Place geometry into appropriate groups in heirarchy', align='left', bgc=(1,.405,.662))
cmds.text(' The geometry that will be bound goes into the bound', align='left', bgc=(1,.405,.662))
cmds.text(' geo group, the geo that is constrained goes into the', align='left', bgc=(1,.405,.662))
cmds.text(' scaleable geo group', align='left', bgc=(1,.405,.662))
cmds.setParent( '..' )
child2 = cmds.rowColumnLayout('Part Two: Spine')
cmds.text('----------------------- SPINE -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step One: Rig IK Spine && Create FK Spine joints', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Two: Orient FK Spine Joints', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Two: Rig FK Spine', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Three: Clean Up Heirarchy', bgc=(0.6,0.6,0.5), command=heirFix)
cmds.setParent( '..' )
child3 = cmds.rowColumnLayout('Part Three: Legs && Feet')
cmds.text('----------------------- LEGS -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step One: Rig IK Legs', bgc=(0.6,0.6,0.5))
cmds.text('----------------------- FEET -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step Two: Create RL Foot Joints', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Three: Mirror & Orient RL Foot Joints', bgc=(0.6,0.6,0.5))
cmds.text('Step Four: Manually orient the RL toe joint to be FLAT with the ground', bgc=(1,.405,.662))
cmds.button(label='Step Five: Rig RL Foot', bgc=(0.6,0.6,0.5))
cmds.setParent( '..' )
child4 = cmds.rowColumnLayout('Part Four: Arms && Hands')
cmds.text('----------------------- ARMS -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step Ten: Rig IK Arms', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Eleven: Rig FK Arms', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Twelve: Finalize Arm Rig', bgc=(0.6,0.6,0.5))
cmds.text('----------------------- HANDS -----------------------', bgc=(.354,.405,.662))
cmds.text('Step Thirteen: Manually orient thumb so that it curls into the hand on Z axis', bgc=(1,.405,.662))
cmds.button(label='Step Thirteen: Rig Four Finger Hand', bgc=(0,0.7,0.3))
cmds.button(label='Step Fourteen: Rig Five Finger Hand', bgc=(0,0.3,0.7))
cmds.setParent( '..' )
child5 = cmds.rowColumnLayout('Part Five: Squash and Stretch')
cmds.text('----------------------- STRETCH -----------------------', bgc=(.354,.405,.662))
cmds.button(label='Step Fifteen: Rig Stretchy Spine', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Fifteen: Rig Squashy Spine', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Sixteen: Rig Stretchy Legs', bgc=(0.6,0.6,0.5))
cmds.button(label='Step Seventeen: Rig Stretchy Arms', bgc=(0.6,0.6,0.5))
cmds.setParent( '..' )
child7 = cmds.rowColumnLayout('Part Six: Binding')
cmds.button(label='Select Joints to be Weighted: Four Fingers', bgc=(0,0.7,0.3), h=100)
cmds.button(label='Select Joints to be Weighted: Five Fingers', bgc=(0,0.3,0.7), h=100)
cmds.setParent( '..' )

cmds.tabLayout(tabs)

cmds.showWindow(myUI)

cmds.window(myUI, e=True, w=385, h=100)

# end of script

And here is my cri_crSkeleton.py script (which contains the skeleton creation, orientation, mirroring, heirarchy cleanup scripts)

# create skeleton

import maya.cmds as cmds
import sys
import maya.mel as mel

### START CREATE SKELETON SCRIPT

def crSkeletonFour(self):
# create joints for spine
cmds.joint(n='hipIK_jnt', p=(0, 10, 0))
cmds.joint(n='spineIK_1_jnt', p=(0, 12, 0))
cmds.joint(n='spineIK_2_jnt', p=(0, 14, 0))
cmds.joint(n='spineIK_3_jnt', p=(0, 16, 0))
cmds.joint(n='spineIK_4_jnt', p=(0, 18, 0))
cmds.joint(n='chestIK_jnt', p=(0, 20, 0))
cmds.joint(n='neckBase_jnt', p=(0,22,0))
cmds.joint(n='neck_jnt', p=(0,23,0))
cmds.joint(n='head_jnt', p=(0,26,0))
cmds.joint(n='headTip_joint', p=(0,30,0))
# create joints for arm
cmds.select('chestIK_jnt')
cmds.joint(n='l_clav_jnt', p=(1, 21, 0))
cmds.joint(n='l_shldr_jnt', p=(2, 21, 0))
cmds.joint(n='l_elbow_jnt', p=(6, 21, 0))
cmds.joint(n='l_forearm_jnt', p=(8, 21, 0))
cmds.joint(n='l_wrist_jnt', p=(10, 21, 0))
cmds.joint(n='l_hand_jnt', p=(11, 21, 0))
# create joints for the leg
cmds.select('hipIK_jnt')
cmds.joint(n='l_hip_jnt', p=(3,10,0))
cmds.joint(n='l_knee_jnt', p=(3,6,0))
cmds.joint(n='l_ankle_jnt', p=(3,2,0))
cmds.joint(n='l_foot_jnt', p=(3,0,2))
cmds.joint(n='l_toe_jnt', p=(3,0,4))
cmds.select(cl=True)
cmds.joint(n='l_rlHeel_jnt', p=(3,0,0))
# create joints for four fingers
cmds.select('l_hand_jnt')
cmds.joint(n='l_midA_jnt', p=(12,21,0))
cmds.joint(n='l_midB_jnt', p=(13,21,0))
cmds.joint(n='l_midC_jnt', p=(14,21,0))
cmds.joint(n='l_midD_joint', p=(15,21,0))
cmds.select('l_hand_jnt')
cmds.joint(n='l_pnkA_jnt', p=(12,21,-1))
cmds.joint(n='l_pnkB_jnt', p=(13,21,-1))
cmds.joint(n='l_pnkC_jnt', p=(14,21,-1))
cmds.joint(n='l_pnkD_joint', p=(15,21,-1))
cmds.select('l_hand_jnt')
cmds.joint(n='l_indA_jnt', p=(12,21,1))
cmds.joint(n='l_indB_jnt', p=(13,21,1))
cmds.joint(n='l_indC_jnt', p=(14,21,1))
cmds.joint(n='l_indD_joint', p=(15,21,1))
cmds.select('l_hand_jnt')
cmds.joint(n='l_thbA_jnt', p=(11.261,21,0.939))
cmds.joint(n='l_thbB_jnt', p=(11.969,21,1.693))
cmds.joint(n='l_thbC_jnt', p=(12.659,21,2.426))
cmds.joint(n='l_thbD_joint', p=(13.665,21,3.498))
cmds.select(cl=True)


print('Four Finger Skeleton Built Successfully'),


def crSkeletonFive(self):
# create joints for spine
cmds.joint(n='hipIK_jnt', p=(0, 10, 0))
cmds.joint(n='spineIK_1_jnt', p=(0, 12, 0))
cmds.joint(n='spineIK_2_jnt', p=(0, 14, 0))
cmds.joint(n='spineIK_3_jnt', p=(0, 16, 0))
cmds.joint(n='spineIK_4_jnt', p=(0, 18, 0))
cmds.joint(n='chestIK_jnt', p=(0, 20, 0))
cmds.joint(n='neckBase_jnt', p=(0,22,0))
cmds.joint(n='neck_jnt', p=(0,23,0))
cmds.joint(n='head_jnt', p=(0,26,0))
cmds.joint(n='headTip_joint', p=(0,30,0))
# create joints for arm
cmds.select('chestIK_jnt')
cmds.joint(n='l_clav_jnt', p=(1, 21, 0))
cmds.joint(n='l_shldr_jnt', p=(2, 21, 0))
cmds.joint(n='l_elbow_jnt', p=(6, 21, 0))
cmds.joint(n='l_forearm_jnt', p=(8, 21, 0))
cmds.joint(n='l_wrist_jnt', p=(10, 21, 0))
cmds.joint(n='l_hand_jnt', p=(11, 21, 0))
# create joints for the leg
cmds.select('hipIK_jnt')
cmds.joint(n='l_hip_jnt', p=(3,10,0))
cmds.joint(n='l_knee_jnt', p=(3,6,0))
cmds.joint(n='l_ankle_jnt', p=(3,2,0))
cmds.joint(n='l_foot_jnt', p=(3,0,2))
cmds.joint(n='l_toe_jnt', p=(3,0,4))
cmds.select(cl=True)
cmds.joint(n='l_rlHeel_jnt', p=(3,0,0))

# create joints for four fingers
cmds.select('l_hand_jnt')
cmds.joint(n='l_midA_jnt', p=(12,21,0.333))
cmds.joint(n='l_midB_jnt', p=(13,21,0.333))
cmds.joint(n='l_midC_jnt', p=(14,21,0.333))
cmds.joint(n='l_midD_joint', p=(15,21,0.333))
cmds.select('l_hand_jnt')
cmds.joint(n='l_pnkA_jnt', p=(12,21,-1))
cmds.joint(n='l_pnkB_jnt', p=(13,21,-1))
cmds.joint(n='l_pnkC_jnt', p=(14,21,-1))
cmds.joint(n='l_pnkD_joint', p=(15,21,-1))
cmds.select('l_hand_jnt')
cmds.joint(n='l_indA_jnt', p=(12,21,1))
cmds.joint(n='l_indB_jnt', p=(13,21,1))
cmds.joint(n='l_indC_jnt', p=(14,21,1))
cmds.joint(n='l_indD_joint', p=(15,21,1))
cmds.select('l_hand_jnt')
cmds.joint(n='l_rngA_jnt', p=(12,21,-0.333))
cmds.joint(n='l_rngB_jnt', p=(13,21,-0.333))
cmds.joint(n='l_rngC_jnt', p=(14,21,-0.333))
cmds.joint(n='l_rngD_joint', p=(15,21,-0.333))
cmds.select('l_hand_jnt')
cmds.joint(n='l_thbA_jnt', p=(11.261,21,0.939))
cmds.joint(n='l_thbB_jnt', p=(11.969,21,1.693))
cmds.joint(n='l_thbC_jnt', p=(12.659,21,2.426))
cmds.joint(n='l_thbD_joint', p=(13.665,21,3.498))
cmds.select(cl=True)

print('Five Finger Skeleton Built Successfully'),

### START MIRROR JOINTS SCRIPT ###

def mrJoints(self):
cmds.mirrorJoint('l_clav_jnt', mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))
cmds.mirrorJoint('l_hip_jnt', mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))
cmds.mirrorJoint('l_rlHeel_jnt', mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))
cmds.rename('r_rlHeer_jnt', 'r_rlHeel_jnt')

print('Joints Mirrored Successfully'),

### START ORIENT JOINTS SCRIPT ###

def orJoints(self):
cmds.joint('l_clav_jnt', e=True, oj='xyz', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('l_hip_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('r_clav_jnt', e=True, oj='xyz', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('r_hip_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('neckBase_jnt', e=True, oj='yxz', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('l_hand_jnt', 'r_hand_jnt', e=True, oj='none', secondaryAxisOrient='xup', zso=True)
cmds.joint('l_indA_jnt', 'l_midA_jnt', 'l_pnkA_jnt', 'l_thbA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
cmds.joint('r_indA_jnt', 'r_midA_jnt', 'r_pnkA_jnt', 'r_thbA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)

if cmds.objExists('l_rngA_jnt'):
cmds.joint('l_rngA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
mel.eval("setAttr l_rngA_jnt.rotateOrder 3;")
mel.eval("setAttr l_rngB_jnt.rotateOrder 3;")
mel.eval("setAttr l_rngC_jnt.rotateOrder 3;")

if cmds.objExists('r_rngA_jnt'):
cmds.joint('l_rngA_jnt', e=True, oj='xzy', secondaryAxisOrient='xup', ch=True, zso=True)
mel.eval("setAttr r_rngA_jnt.rotateOrder 3;")
mel.eval("setAttr r_rngB_jnt.rotateOrder 3;")
mel.eval("setAttr r_rngC_jnt.rotateOrder 3;")

mel.eval("setAttr l_indA_jnt.rotateOrder 3;")
mel.eval("setAttr l_indB_jnt.rotateOrder 3;")
mel.eval("setAttr l_indC_jnt.rotateOrder 3;")
mel.eval("setAttr l_midA_jnt.rotateOrder 3;")
mel.eval("setAttr l_midB_jnt.rotateOrder 3;")
mel.eval("setAttr l_midC_jnt.rotateOrder 3;")
mel.eval("setAttr l_pnkA_jnt.rotateOrder 3;")
mel.eval("setAttr l_pnkB_jnt.rotateOrder 3;")
mel.eval("setAttr l_pnkC_jnt.rotateOrder 3;")
mel.eval("setAttr l_thbA_jnt.rotateOrder 3;")
mel.eval("setAttr l_thbB_jnt.rotateOrder 3;")
mel.eval("setAttr l_thbC_jnt.rotateOrder 3;")

mel.eval("setAttr r_indA_jnt.rotateOrder 3;")
mel.eval("setAttr r_indB_jnt.rotateOrder 3;")
mel.eval("setAttr r_indC_jnt.rotateOrder 3;")
mel.eval("setAttr r_midA_jnt.rotateOrder 3;")
mel.eval("setAttr r_midB_jnt.rotateOrder 3;")
mel.eval("setAttr r_midC_jnt.rotateOrder 3;")
mel.eval("setAttr r_pnkA_jnt.rotateOrder 3;")
mel.eval("setAttr r_pnkB_jnt.rotateOrder 3;")
mel.eval("setAttr r_pnkC_jnt.rotateOrder 3;")
mel.eval("setAttr r_thbA_jnt.rotateOrder 3;")
mel.eval("setAttr r_thbB_jnt.rotateOrder 3;")
mel.eval("setAttr r_thbC_jnt.rotateOrder 3;")

print('Joints Oriented Successfully'),

### CREATE HEIRARCHY ###

def heirFix():
cmds.group(em=True, n='skeleton_grp_doNOTTouch')
cmds.parent('hipIK_jnt', 'l_rlHeel_jnt', 'r_rlHeel_jnt', 'skeleton_grp_doNOTTouch')
cmds.group(em=True, n='ikH_grp_doNOTTouch')
cmds.group(em=True, n='scaleable_geo_grp')
cmds.group(em=True, n='bound_geo_grp')
cmds.group(em=True, n='clst_grp_doNOTTouch')
cmds.group(em=True, n='ctrl_grp')
cmds.group(em=True, n='geo_grp')
cmds.parent('bound_geo_grp', 'scaleable_geo_grp', 'geo_grp')
cmds.group('skeleton_grp_doNOTTouch', 'clst_grp_doNOTTouch', 'geo_grp', 'ikH_grp_doNOTTouch', n="rig_doNOTTouch")
charName = raw_input("What is your Characters or Rigs Name? ")
cmds.group(em=True, n=charName)
cmds.parent( 'ctrl_grp', 'rig_doNOTTouch', charName)

JaredTaylor
10-30-2010, 12:36 AM
I found out why it was happening, I didn't have (self) in the definition, soon as I put it in, everything was magically fixed, didn't you say somewhere I didn't need it?

JaredTaylor
10-30-2010, 04:57 AM
How do you call a function from another function inside a function?

Example:


def functionA(self):
jointA = cmds.joint(n='a_jnt')
jointB = cmds.joint(n='b_jnt')
def functionB(self):
jointC = cmds.joint(n='c_jnt')
cmds.parent(jointA, jointC)

Will give an error saying that joint A is not defined in functionB, how do I tell it to look in functionA to get the 'jointA' variable?



This is my script, my UI has buttons calling each function.

# create the spine


import maya.cmds as cmds
import sys
import maya.mel as mel

def rigSpineIK(self):
spineIKH = cmds.ikHandle(sj='hipIK_jnt', ee='chestIK_jnt', sol='ikSplineSolver', pcv=False, ns=2, n='spine_ikH')
spineCRVShape = cmds.ikHandle(spineIKH[0], q=True, curve=True)

cmds.select(spineCRVShape)
cmds.pickWalk(d='up')
spineCRV = cmds.rename('spine_crv')

spineLen = cmds.arclen(ch=1)
cmds.rename(spineLen, 'spineArcLen')
mel.eval("clusterCurve;")

hipIKCtrl = cmds.circle(c=(0,0,0), nr=(0,1,0), sw=360, r=1, d=3, ut=0, tol=1.07639e-005, s=8, ch=0, n='hipIK_ctrl')
chestIKCtrl = cmds.circle(c=(0,0,0), nr=(0,1,0), sw=360, r=1, d=3, ut=0, tol=1.07639e-005, s=8, ch=0, n='chestIK_ctrl')

hipIKCtrlO = cmds.group(hipIKCtrl, n='hipIK_ctrl_0')
chestIKCtrlO = cmds.group(chestIKCtrl, n='chestIK_ctrl_0')

pcDeleteHip = cmds.parentConstraint('hipIK_jnt', hipIKCtrlO)
pcDeleteChest = cmds.parentConstraint('chestIK_jnt', chestIKCtrlO)

cmds.delete(pcDeleteHip)
cmds.delete(pcDeleteChest)

cmds.select(chestIKCtrlO)
cmds.scale(3,3,3)
cmds.rotate(0,0,90)
cmds.makeIdentity(apply=True, t=0, r=1, s=1, n=0)

cmds.select(hipIKCtrlO)
cmds.scale(3,3,3)
cmds.makeIdentity(apply=True, t=0, r=1, s=1, n=0)

cmds.parentConstraint(chestIKCtrl, 'cluster5Handle', mo=True)
cmds.parentConstraint(chestIKCtrl, 'cluster4Handle', mo=True)
cmds.parentConstraint(hipIKCtrl, 'cluster1Handle', mo=True)
cmds.parentConstraint(hipIKCtrl, 'cluster2Handle', mo=True)
cmds.parentConstraint(hipIKCtrl, chestIKCtrl, 'cluster3Handle', mo=True)

### MAKE THE FK JOINTS ###


hipFK = cmds.duplicate('hipIK_jnt', po=True, n='hipFK_joint')
chestFK = cmds.duplicate('chestIK_jnt', po=True, n='chestFK_joint')
spineOneFK = cmds.duplicate('spineIK_1_jnt', po=True, n='spineFK_1_joint')
spineTwoFK = cmds.duplicate('spineIK_3_jnt', po=True, n='spineFK_2_joint')

cmds.parent(spineOneFK, hipFK)
cmds.parent(spineTwoFK, spineOneFK)
cmds.parent(chestFK, spineTwoFK)

### ORIENT FK JOINTS AND SET ROTATION ORDER ###

def rigSpineFKor(self):
cmds.makeIdentity(hipFK, chestFK, spineOneFK, spineTwoFK, apply=True, t=1, r=1, s=1, n=0, jointOrient=True)
cmds.joint(hipFK, chestFK, spineOneFK, spineTwoFK, e=True, oj='xyz', secondaryAxisOrient='yup', ch=True, zso=True)

mel.eval("setAttr spineFK_1_joint.rotateOrder 1;")
mel.eval("setAttr spineFK_2_joint.rotateOrder 1;")

### RIG FK JOINTS ###
def rigSpineFK(self):
spineFKOneCtrl = cmds.circle(c=(0,0,0), nr=(0,1,0), sw=360, r=1, d=3, ut=0, tol=1.07639e-005, s=8, ch=0, n='spineFK_1_ctrl')
cmds.scale(3,3,3)
cmds.makeIdentity(apply=True, t=0, r=0, s=1, n=0)

spineFKTwoCtrl = cmds.circle(c=(0,0,0), nr=(0,1,0), sw=360, r=1, d=3, ut=0, tol=1.07639e-005, s=8, ch=0, n='spineFK_2_ctrl')
cmds.scale(3,3,3)
cmds.makeIdentity(apply=True, t=0, r=0, s=1, n=0)

spineFKOneCtrlGrp = cmds.group(spineFKOneCtrl, n='spineFK_1_ctrl_0')
spineFKTwoCtrlGrp = cmds.group(spineFKTwoCtrl, n='spineFK_2_ctrl_0')

deleteMeSpineOneFK = cmds.parentConstraint(spineOneFK, spineFKOneCtrlGrp)
deleteMeSpineTwoFK = cmds.parentConstraint(spineTwoFK, spineFKTwoCtrlGrp)

cmds.delete(deleteMeSpineOneFK)
cmds.delete(deleteMeSpineTwoFK)

spineFKCTRLOnePC = cmds.parentConstraint(spineFKOneCtrl, spineOneFK)
spineFKCTRLTwoPC = cmds.parentConstraint(spineFKTwoCtrl, spineTwoFK)

cmds.parent(spineFKTwoCtrlGrp, spineFKOneCtrl)

hipIKCtrlSDK = cmds.group(hipIKCtrl, n='hipIK_ctrl_SDK')
chestIKCtrlSDK = cmds.group(chestIKCtrl, n='chestIK_ctrl_SDK')

spineFKCTRLTwoSDKPC = cmds.parentConstraint(spineFKTwoCtrl, chestIKCtrlSDK, mo=True)

mel.eval("setAttr spineFK_1_ctrl.rotateOrder 1;")
mel.eval("setAttr spineFK_2_ctrl.rotateOrder 1;")

cmds.expression(s='spine_ikH.twist = (spineFK_1_ctrl.rotateY + spineFK_2_ctrl.rotateY + chestIK_ctrl.rotateY)')

COGCtrl = cmds.circle(c=(0,0,0), nr=(0,1,0), sw=360, r=1, d=1, ut=0, tol=1.07639e-005, s=4, ch=0, n='COG_ctrl')
COGCtrlO = cmds.group(COGCtrl, n='COG_ctrl_0')
cmds.rotate(0,45,0, COGCtrlO)
cmds.scale(5,5,5, COGCtrlO)
cmds.makeIdentity(COGCtrlO, apply=True, t=0, r=1, s=1, n=0)

deletemehipIKtoCOGPC = cmds.parentConstraint('hipIK_jnt', COGCtrlO)

cmds.delete(deletemehipIKtoCOGPC)

cmds.parent(spineFKOneCtrlGrp, hipIKCtrlO, chestIKCtrlO, COGCtrl)

JaredTaylor
10-30-2010, 12:21 PM
Just spent the past five hours trying to fix it, and it only got worse. Reverted back to my previous version.

Really need help, just lost an entire day of coding because I don't know some form of code that'd make this work :shrug:

Someone help me out here before I pull my hair out... some more.

JaredTaylor
11-01-2010, 02:05 PM
As if anyone still reads this, haven't had help in ages.. but.. I don't need it AS much anymore, I just solved it on my own! And damn am I happy.

I was reading the resource posted at the top of this page, made it up to 'lists', was about to give up out of boredom and had a minor epiphany, popped up maya all happy like knowing that it wouldn't work, but hey, guess what:

lArmJoints = [cmds.joint(p=(1,0,0), n='l_shldr_jnt'), cmds.joint(p=(2,0,0), n='l_elbow_jnt')]
rArmJoints = [cmds.mirrorJoint(lArmJoints[0], mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))]
print(lArmJoints)
print(rArmJoints)

>>>[u'l_shldr_jnt', u'l_elbow_jnt']
>>>[[u'r_shldr_jnt', u'r_elbow_jnt']]


But, I still need your guys help.

I'm able to have each joint stored in a string now, but I don't know how to use something that is stored inside of a local function inside another local function, and since my buttons have to have command=functionName to call to, (is there a better way?), there's no point defining variables simply because I can't call to them in my other scripts. =/

Here is an example, and for the love of god, gimme a hand this time! At least tell me what I need to read up on.

def functionA(self):
lArmJoints = [cmds.joint(p=(1,0,0), n='l_shldr_jnt'), cmds.joint(p=(2,0,0), n='l_elbow_jnt')]
rArmJoints = [cmds.mirrorJoint(lArmJoints[0], mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))]
<magical command that rigs a joint chain with the press of a button!>


in the UI it would have

cmds.button(label='rig functionA', command=functionA)


And then in another script or in the same script as functionA exists I'd have:


def functionB(self):
<magic rigging script #2!.... that needs to call to the list to retrieve the elbow joint...>


And the UI would have a button with the command pointing to functionB, which would not work, because local variables can not call to local lists/strings in other local variables, ergo, I'm screwed.

How do I get around this, what am I looking for, is there something I should be reading up on to fix it?

Thanks for the help so far, and hopefully, future help!




-----------------------------EDIT--------------------

Huge post just got huger.

I figured out how to define global variables, too easy really.... in fact, suspiciously easy, do wish someone had just mentioned it here though. I read somewhere that global variables in python are BAD, can anyone explain why?

Here's what my example script would now look like.

import maya.cmds as cmds

def functionA(self):
lArmJoints = [cmds.joint(p=(1,0,0), n='l_shldr_jnt'), cmds.joint(p=(2,0,0), n='l_elbow_jnt')]
rArmJoints = [cmds.mirrorJoint(lArmJoints[0], mirrorYZ=True, mirrorBehavior=True, searchReplace=('l_', 'r_'))]
global lArmJoints
global rArmJoints


def functionB(self):
print lArmJoints

And then a UI that calls to function A then function B.

CGTalk Moderation
11-01-2010, 02:05 PM
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.