This is a very simple script that you all will laugh me out of the group, but I’m starting small and working my way up to a much larger project. Baby steps.
So here we go. In Maya, I need to write a ver basic script that generates joints. That’s it. I was thinking a for loop, but I can’t seem to get it to work.
The bigger idea will be to take a users input and place that in a variable numJnts for example and that will determine the number of iterations.
Other variables will be added, but I wnated to just get the basic loop to work first.
This is a very simple script that you all will laugh me out of the group, but I’m starting small and working my way up to a much larger project. Baby steps.
So here we go. In Maya, I need to write a ver basic script that generates joints. That’s it. I was thinking a for loop, but I can’t seem to get it to work.
The bigger idea will be to take a users input and place that in a variable numJnts for example and that will determine the number of iterations.
Other variables will be added, but I wnated to just get the basic loop to work first.
Thanks.[/QUOTE]
HAHAHAHAHAHAHAHAHAAHAHAH! No, just kidding
Python:
joints = ['a', 'b', 'c']
for joint in joints:
print joint
import maya.cmds as cmds
def make_joints(how_many):
results = [] # to hold all the joints we make
for j in range(how_many): # loop from 0 to how_many
new_joint = cmds.joint(p = (0, j, 0)) # make the joint. Maya will parent it to the last one automatically
results.append(new_joint) # add it to the list
cmds.warning("created " + str(results)) # print out what we just did
return results
print make_joints(10)
That gives you a basic idea of the structure: You use maya.cmds to get the commands to work with, and the loop just calls the command with arguments you pass in (in this example, it positions each joint 1 unit above the previous one)
[QUOTE=Theodox;30827]if you want to make the joints:
import maya.cmds as cmds
def make_joints(how_many):
results = [] # to hold all the joints we make
for j in range(how_many): # loop from 0 to how_many
new_joint = cmds.joint(p = (0, j, 0)) # make the joint. Maya will parent it to the last one automatically
results.append(new_joint) # add it to the list
cmds.warning("created " + str(results)) # print out what we just did
return results
print make_joints(10)
Ok the more I look @ the code Theo, the more questions I have.
results = []…what format goes in there? What do you mean to hold all the joints made?
range (how many): isn’t the format usually something like, rang (start, end):but numbers (0,5) for example or can it be a single number that is -1 from what the user gives?
so new_joint will generate a joint @ 0=x, j=y, z=0?
if May will parent to the last automatically, then what’s the results.append(new_joint) for?
From your original post it sounds like you’re interested in learning scripting in Maya, and not just producing a one-off script. If that is the case it sounds like you should find some websites that teach beginning python. You’ll learn the answers to those questions pretty quickly.
results = …what format goes in there? What do you mean to hold all the joints made?
range (how many): isn’t the format usually something like, rang (start, end):but numbers (0,5) for example or can it be a single number that is -1 from what the user gives?
so new_joint will generate a joint @ 0=x, j=y, z=0?
if May will parent to the last automatically, then what’s the results.append(new_joint) for?
what’s the return results do?
why print make_joints(10), why 10?
result = []
this just makes an empty list, so you have a place to store the results. Just making things happen is rarely what you want to do - you’ll want to be able to chain things together and so each piece needs to tell the others what it’s doing
range with one argument starts at 0, and in this case that’s what we need.
new_joint doesn’t exactly generate the joint, it’s the result of the command “joint”. But yes, it holds the name of the new joint that gets created by the command. Here we are only hanging on to it long enough to add it to the result list but in a more complicated script we could do other things knowing what joint we had just made
results.append(new_joint)
adds the name of the new joint to the output list. The parenting behavior comes from Maya – by default whenever you make a joint it will be auto-parented to the last selected joint if there is one .
return results
sends the list of results back to whoever called the def. In this case it’s not really necessary – but you could have a second def that called this one and needed to know which joints had been created. All the built in maya commands work the same way – when the script calls cmds.joint() it is getting a return value which is the name of the joint.
and 10 is just an example: you can put any number you want in there. If you wanted to ask the user for a number you would do something like @capper’s example, which asks the user for a number and then passes it in place of the 10 in the example.