Very simple script, newbie

Hello scripters.

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=srlake314;30825]Hello scripters.

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 :stuck_out_tongue:
Python:


joints = ['a', 'b', 'c']
for joint in joints:
    print joint

Let me know if you have more questions.

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)

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)

You can also get user input using the promptDialog

resp = cmds.promptDialog(t='Joint Creator', m='Number of joints to create:', button=['Accept', 'Cancel'])
if resp == 'Accept':
    input = cmds.promptDialog(q=True)
    try:
        num_joints = int(input)
    except ValueError:
        cmds.error("Not a valid number: {0}".format(input))

    make_joints(num_joints)

Thank you! That’s what I was looking for!

[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)

[/QUOTE]

Ok the more I look @ the code Theo, the more questions I have.

  1. results = []…what format goes in there? What do you mean to hold all the joints made?
  2. 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?
  3. so new_joint will generate a joint @ 0=x, j=y, z=0?
  4. if May will parent to the last automatically, then what’s the results.append(new_joint) for?
  5. what’s the return results do?
  6. why print make_joints(10), why 10?

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.

https://www.reddit.com/r/learnpython/ has some linked resources that are good to begin learning.

  1. results = …what format goes in there? What do you mean to hold all the joints made?
  2. 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?
  3. so new_joint will generate a joint @ 0=x, j=y, z=0?
  4. if May will parent to the last automatically, then what’s the results.append(new_joint) for?
  5. what’s the return results do?
  6. 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.