0

I am currently working on a script to make a simple crane-like rig.

I have a variable to define the amount of joints I want. I also have a variable for the amount of groups/controllers I want. These amounts may vary so the script would be semi-"dynamic".

So what I end up with is a couple of joints (joint1, joint2, joint3, ... etc) and a couple of groups (group1, group2, group3, ... etc).

My problem is that I do not know how I would go about parenting my "group1" to "joint1" and "group2" with "joint2" etc. all in one go. Since I want to be able to change the amounts of joints and groups I can't hardcode it.

Any help would be appreciated :)

Hampus
  • 11
  • 5

2 Answers2

1

could something like this help you ?

grp = cmds.ls('group*')
nbs = [int(n.split('group')[-1]) for n in grp]
grpDic = dict(zip(nbs, grp))

joint = cmds.ls('joint*', type='joint')
nbs = [int(n.split('joint')[-1]) for n in joint]
jointDic = dict(zip(nbs, joint))

common = list(set(grpDic.keys())&set(jointDic.keys()))

for i in common:
    cmds.parent(grpDic[i], jointDic[i])

EDIT : include nurbs parenting

# filter by nurbs type    
nurbs_sh = cmds.ls('nurbsCircle*', type='nurbsCurve')
# get the transform node of this nurbs
nurbs_tr = cmds.listRelatives(nurbs_sh, p=1)
nbs = [int(n.split('nurbsCircle')[-1]) for n in nurbs_tr]
curveDic = dict(zip(nbs, nurbs_tr))

common = list(set(grpDic.keys())&set(curveDic.keys()))
# nurbs parent to group
for i in common:
    cmds.parent(curveDic[i], grpDic[i])
DrWeeny
  • 2,487
  • 1
  • 14
  • 17
  • That worked perfectly for parenting my groups to the joints! Thank you so much! I assumed I could use the same formula to parent my nurbsCircles(nurbscircle1,2,3 etc) to my groups(1,2,3 etc) aswell by changing the name attributes, however I can't seem to get it to work. I seem to get more than just the transform data and I assume that's why it doesn't work. I am also wondering why I have to define the type of the joints but not the groups? Would I have to define the type of my nurbscircles aswell for them to be able to parent to the groups? – Hampus Oct 09 '18 at 22:04
  • I am using -type because creating joints, there is a node somewhere with joint in his name (eve if I've worked on a really simple scene file) I've just assumed that you had a list of known named node. If you use type nurbsCurve, you will get shapes and will have to find the transform to parent it. I'll try something tomorrow when I'll have maya. My point in this code was : you can build an ID dictionnary and associate everything. In this case, there is one group, one joint but you could work with multiple group to parent to joints. Im not a rigging guy so I try to take things as broad as I can – DrWeeny Oct 09 '18 at 23:53
  • Ah I see, I managed to put my nurbs in a dictionary but looking into my dictionary its filled with ALOT of different nurbs data and trying to parent them to the groups doesnt do anything, not even a error message. But as you said I need to get the transform data from the nurbs and parent that to the groups? I really appreciate you taking your time to help me out, I was totally stuck before. Great community :) – Hampus Oct 10 '18 at 09:42
  • i've added some piece of code for building the nurbs dictionnary – DrWeeny Oct 10 '18 at 09:54
1

@DrWeeny's example will take what existing objects and existing joints and parent them together. If you want to just start with the geometry and and automatically add joints to it, you could try something like this:

import re

def add_joints_to_selected(orient = 'xyz'):
    selection = cmds.ls(sl=True)
    cmds.select(d=True)
    joints = []
    for geo in selection:
        pivot = cmds.xform(geo, q=True, rp=True, ws=True)
        suffix = '0'
        raw_name = re.findall( "\d$", geo)
        if raw_name:
            suffix = raw_name[-1]       
        jnt = cmds.joint(n = "joint_" + suffix, p=pivot)
        cmds.parent(geo, jnt)
        joints.append(jnt)
    if orient:
        cmds.joint(joints[:-1], e=True, oj = orient)

add_joints_to_selected('xyz')  # or add_joints_to_selected(None)

This one gets the pivot points of the selected nodes and makes joints for each one (in the order you selected them). If you supply a joint order like 'xyz' or 'yzx' it will align the joints as if you'd drawn them by hand; otherwise the joints are world aligned. The only tricky bit is using a regular expression to grab the suffixes from the existing node names (it falls back to "0" if you don't have a number suffix)

theodox
  • 12,028
  • 3
  • 23
  • 36
  • This is a very interesting read and something I would like to apply in the future, although for my script it wont really be necessary to apply those functions. But thank you so much for the reply and knowledge :) – Hampus Oct 09 '18 at 22:06
  • oh yeah in my example I assume you know already your hierarchy. For your script, you arer selecting the groups in order to draw your joints ? And yeah, I would defnitly use regex for naming pattern. – DrWeeny Oct 10 '18 at 00:02