Incorrect Python Joint Chain Creation

This function creates a straight joint chain emanating from the origin.
It is working, although all the joints are being created from the origin.
How would I adjust this function so they are created normally in a hierarchy ?

def createSimpleSkeleton(joints): # joints = # of joints to create
cmds.select(clear =True)
bones = []
pos = [0,0,0]

for i in range(0, joints):
pos[1] = i * 4
bones.append(cmds.joint(p=pos))
cmds.select(bones[0], replace = True)

createSimpleSkeleton(4)

It is impossible to comprehend the logic without seeing the indentation of the code.

“Simple is better than complex.”
Don’t overcomplicate things unnecessarily.

  1. When you create joint, it will be parented to currently selected object
  2. When you create any node (joint, locator, etc.), it will be automatically selected

Combining these two statements, you just simply create each joint in a loop, and it will become child of previously created joint (1st joint will be parented to the world, since you’ve cleared selection, 2nd joint will be parented to the 1st, since 1st is now selected, etc.) - for this purpose, no need to keep track of joints in, hmm, “bones” list…

import maya.cmds as cmds

def create_simple_skeleton(joints):

    cmds.select(clear=True)
    pos = [0, 0, 0]

    for i in range(joints):
        pos[1] = i * 4
        cmds.joint(p=pos)

create_simple_skeleton(4)

P.S. As stated above, it would be cool if you could format your code nicely when posting questions.

Yes you are so right, about keeping things simple. Works perfect, Thank you!
About the formatting, I always indent / format correctly, but when I hit the post
button all formatting gets justified to the left. Is there a setting i’m missing ??
Is it the pre-formatted text button ? Sorry, total beginner question.

Yes …sorry about that non formatted post… It was formatted when I copied it over, but when I hit ‘post’
everything gets left justified. To solve this , do I select the preformatted text button ??

By default for me, there’s a preview of exactly what my post will look like. If you don’t see that when you’re making a post, you may have to press the >> button at the bottom right of the reply box.

Then you can try things out, and see what your changes will actually look like.

Are you putting your code inside 3 backtick characters?

Start with 3 backticks, enter new line, type/paste your formatted code, enter new line, and then close it all with 3 backticks again.

  • backtick - it’s NOT a single quote character (it’s also called backquote); usually (or always) on English keyboards it’s located at top-most left corner of alphabetic part of keyboard (below ‘escape’ key, left of ‘1’ and above ‘tab’).

Here’s an example of how it looks like when typing some code, and preview of it on the right side:

def test_format():
    if 1 == 1:
        print('Good formatting!')

If your preview is not showing, this is the button mentioned by @tfox_TD that you should press to show it:

Ok, yes…thanks for all the explanation. How could I simplify this code still using a function ?


def SolveIssues(arg1, arg2):
    if arg1 == problems:
        print(problems)
    else: print('None')
    
    if arg2 == rabbit_holes:
        print(rabbit_holes)
    else: print('None')
    
    
problems = [ 'stay focused',
              ' use drive', 
        'have persistence'] 
                     
rabbit_holes = [ 'pick your battles',
                'use your strengths', 
                     'have patience']
                     
SolveIssues(problems, rabbit_holes)