0
votes

I’m really sorry if I’m asking a question that’s been already answered but I couldn’t find an answer.

I’m writing a code that would allow me to connect a translate of several controllers into a blendWeighted node input channels. The amount of the controllers may vary depending on the selection. I’m struggling with the part where they need to connect to a single blendWeighted node input. Could someone tell me how I could connect every new controller to the next input channel of the blendWeighted node?

I’m sorry if my code is a bit childlike, I’m still learning ^^;

sel = mc.ls(sl=True, fl=True)
drvConnect = []

for i in sel:
    name = i.split('_Crv')[0]
    dGP = mc.createNode('transform', n='%s_Drv'%name, p=i)
    drvConnect.append(dGP)
    sh = mc.listRelatives(i, shapes=True)[0]
    blendX = mc.createNode('blendWeighted', n='%s_X'%name)
    blendY = mc.createNode('blendWeighted', n='%s_Y'%name)
    blendZ = mc.createNode('blendWeighted', n='%s_Z'%name)
    mc.connectAttr(dGP + '.translateX', blendX +'.input')
    mc.connectAttr(dGP + '.translateY', blendY +'.input')
    mc.connectAttr(dGP + '.translateZ', blendZ +'.input')
1

1 Answers

0
votes

I assume you only want to create a single blendWeighted node. If that's the case, consider that the blendWeighted node's input attribute is an array attribute, so if you want to connect multiple items to it you would need to specify the target index.

For example, to connect the three translate outputs of a node to the first three inputs of the blend node you would use something like this:

mc.connectAttr('ctrl.translateX', 'blend.input[0]')
mc.connectAttr('ctrl.translateY', 'blend.input[1]')
mc.connectAttr('ctrl.translateZ', 'blend.input[2]')

(Maya will take care of creating the items in the array)

In our case you could simply keep a counter of the added items while you loop through the selection and the transform components (just a guide - not tested):

sel = mc.ls(sl=True, fl=True)
drvConnect = []

blendNode = mc.createNode('blendWeighted', n='blend_main')

for i in sel:
    name = i.split('_Crv')[0]
    dGP = mc.createNode('transform', n='%s_Drv'%name, p=i)
    drvConnect.append(dGP)

    for comp in ('X', 'Y', 'Z'):
        mc.connectAttr(
            '{}.translate{}'.format(dGP, comp),
            '{}.input[{}]'.format(blendNode, blendIndex))
        blendIndex += 1