What I'm trying to do is this. I have a dictionary laid out as such:
legJointConnectors = {'L_hip_jnt': ['L_knee_jnt'], 'L_knee_jnt': ['L_ankle_jnt'], 'L_ankle_jnt': ['L_ball_jnt'], 'L_ball_jnt': ['L_toe_jnt']}
What I want to be able to do is iterate through this, but change the L_ to R_. Here's how I tried to do it, but it failed, because it's expecting a string and I'm passing it a list.
for key, value in legJointConnectors.iteritems():
if side == 'L':
cmds.connectJoint(value, key, pm=True)
else:
key = re.sub('L_', 'R_', key)
value = re.sub('L_', 'R_', value)
cmds.connectJoint(value, key, pm=True)
Obviously I'm not doing this correctly, so how can I do this? Should I create an empty d开发者_C百科ictionary and populate it with the necessary data on the fly? Or is there a better way to do it? Thanks for the help!
[edit] The if side == 'L' is testing to see what side we're currently working on. This script is being used within Maya, so I'm creating joints based on the side and then connecting them.
Based off of KennyTM's suggestion I tried this:
for key, value in legJointConnectors.iteritems():
if side == 'L':
cmds.connectJoint(value, key, pm=True)
else:
for v in value:
value = 'R_' + v[2:]
for k in key:
key = 'R_' + k[2:]
print key
print value
but while it returns the correct key, the value returns as R_
Do the substitution on every element of the list then.
for key, value in legJointConnectors.iteritems():
if side != 'L':
key = 'R_' + key[2:]
value = ['R_' + v[2:] for v in value]
cmds.connectJoint(value, key, pm=True)
(BTW, it is better to use v.replace('L_', 'R_')
, or just 'R_' + v[2:]
to perform the replacement then using regex for this.)
精彩评论