0

I am trying to create a parent constraint between the elements in two lists as follows:

    import maya.cmds as mc
    objCtl= mc.ls('red_ctl', 'green_ctl', 'blue_ctl')
    objJt= mc.ls('red_jt', 'green_jt', 'blue_jt')

    for item in objCtl:
        ctl= item
        makeParentCons= mc.parentConstraint(ctl, objJt, mo=True, w=1)

I'm getting a parentConstraint with multiple drivers for each joint. I know that I have to iterate between the lists but i just don't know how to do it. I've tried with zip(), append

2 Answers2

0

I do neither have any Maya experience nor the software available. However, as far as I understood your question (with some guessing) and after having a look at Maya's documentation for both the ls command and the parentConstraint command, it seems that you need to generate pairs of the elements from both lists in order to create the constraint between those two elements.

Maya's ls command seems to return a list of elements and parentConstraint takes several arguments (at least two elements/objects and some additional config parameters) to create a constraint between the given elements.

So going a step back and abstracting the problem from the Maya-related stuff to 'pure Python', you basically want to get pairs from the lists and pass each pair to another function. In general this can be done like this:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# define sample data, using tuples here, but can be lists as well
# needs to be adopted to the mc.ls() command
objCtl = ('red_ctl', 'green_ctl', 'blue_ctl')
objJt = ('red_jt', 'green_jt', 'blue_jt')

# generate element-wise pairs using zip()
pairs = zip(objCtl, objJt)

# iterate through zip-object and print elements of each pair
# print() needs to be changed to the `mc.parentConstraint()` command
for ctl, jt in pairs:
    print(ctl, jt)

The output of this snippet given above is

red_ctl red_jt
green_ctl green_jt
blue_ctl blue_jt

and should be suitable/adoptable for constraint generation.

albert
  • 8,027
  • 10
  • 48
  • 84
  • Thank you very much, your answer solved my problem, I know it's kind of a basic question, but i'm a beginner in python so I appreciate your reply. – S. Mancera Jun 25 '19 at 21:55
0

In case anyone comes back to this in the Future the reason for the multiple drivers is because you are using the objCtl as the driver rather than a single element.

So if we lay this out this is what a single loop would look like

makeParentCons= mc.parentConstraint('red_ctl', ['red_jt', 'green_jt', 'blue_jt'], mo=True, w=1)

If you wanted to do it to each item in the list you can do as mentioned in the other comment and Zip the two elements or simply get the Value from the objJt using an enumerate function in the loop.

    import maya.cmds as mc
    objCtl= mc.ls('red_ctl', 'green_ctl', 'blue_ctl')
    objJt= mc.ls('red_jt', 'green_jt', 'blue_jt')

    for idx, item in enumerate(objCtl):
        ctl= item
        makeParentCons= mc.parentConstraint(ctl, objJt[idx], mo=True, w=1)