Expressing a plusMinusAverage node's input index as a variable

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

 
Thread Tools Display Modes
  06 June 2018
Expressing a plusMinusAverage node's input index as a variable

HI all, more than likely a simple question here but I've been trying to get this to run in a script and I'm new to python

I've gotten the script to do a large amount of what I want but I'm stuck trying to get multiple inputs into a plus Minus Average node

the problem comes in expressing a variable index for the input3D[i]


#loop through selected joints to create the necessary node chain
#for each joint, set attributes and connect them correctly
#connect the multiInput PMA to each joint
joints = cmds.ls(selection=True)
for x in range(0, len(joints)):
jointName = 'spine_FK_jnt_' + str(x + 1)
joints[x] = cmds.rename(joints[x], (jointName))
cmds.shadingNode('plusMinusAverage', asUtility=True, name=jointName + '_multiInput_PMA')
cmds.connectAttr(jointName + '_multiInput_PMA.output3D.output3Dx', jointName + '.rx', f=True)
cmds.connectAttr(jointName + '_multiInput_PMA.output3D.output3Dy', jointName + '.ry', f=True)
cmds.connectAttr(jointName + '_multiInput_PMA.output3D.output3Dz', jointName + '.rz', f=True)

#make a list of the controllers we're using
FK_controls = []
controls = cmds.ls(type='nurbsCurve')
for ctrl in controls:
if "spine_FK_ctrl" in ctrl:
FK_controls.append(ctrl)
cmds.connectAttr(ctrl + '_MDI_' + str(x + 1) + '.outputX', jointName + '_multiInput_PMA.input3D[0].input3Dx')
cmds.connectAttr(ctrl + '_MDI_' + str(x + 1) + '.outputY', jointName + '_multiInput_PMA.input3D[0].input3Dy')
cmds.connectAttr(ctrl + '_MDI_' + str(x + 1) + '.outputZ', jointName + '_multiInput_PMA.input3D[0].input3Dz')

this is the code I have so far to do it




the image attached shows what the node graph should look like when the script runs

p.s. ignore the name of the MDI node, it's just using the name of the shapes of the controllers rather than the name of the transform

any help would be greatly appreciated! thank you!
 
  06 June 2018
Can you tell us what exactly is the problem? Do you get an errormessage? It seems you do not use input3D[i] but only input3d[0].
__________________
www.renderwiki.com - www.openmaya.net
 
  06 June 2018
the problem is that for the input index of the 'multiInput_PMA' nodes I can't express that without hard coding it
I haven't been able to find references to anyone doing something like this and I can't see it in the maya documentation

I'm trying to get this to work:

cmds.connectAttr('blahblah' + str(i + 1) + 'MDI.outputX', 'multInput_PMA.input3D[insertVariableHere].input3Dx')
cmds.connectAttr('blahblah' + str(i + 1) + 'MDI.outputY', 'multInput_PMA.input3D[insertVariableHere].input3Dy')
cmds.connectAttr('blahblah' + str(i + 1) + 'MDI.outputZ', 'multInput_PMA.input3D[insertVariableHere].input3Dz')

instead of:

cmds.connectAttr('blahblah_1_MDI.outputX', 'multInput_PMA.input3D[0].input3Dx')
cmds.connectAttr('blahblah_1_MDI.outputY', 'multInput_PMA.input3D[0].input3Dy')
cmds.connectAttr('blahblah_1_MDI.outputZ', 'multInput_PMA.input3D[0].input3Dz')
cmds.connectAttr('blahblah_2_MDI.outputX', 'multInput_PMA.input3D[1].input3Dx')
cmds.connectAttr('blahblah_2_MDI.outputY', 'multInput_PMA.input3D[1].input3Dy')
cmds.connectAttr('blahblah_2_MDI.outputZ', 'multInput_PMA.input3D[1].input3Dz')
cmds.connectAttr('blahblah_3_MDI.outputX', 'multInput_PMA.input3D[2].input3Dx')
cmds.connectAttr('blahblah_3_MDI.outputY', 'multInput_PMA.input3D[2].input3Dy')
cmds.connectAttr('blahblah_3_MDI.outputZ', 'multInput_PMA.input3D[2].input3Dz')
cmds.connectAttr('blahblah_4_MDI.outputX', 'multInput_PMA.input3D[3].input3Dx')
cmds.connectAttr('blahblah_4_MDI.outputY', 'multInput_PMA.input3D[3].input3Dy')
cmds.connectAttr('blahblah_4_MDI.outputZ', 'multInput_PMA.input3D[3].input3Dz')


and that doesn't work anyway.

I'm mostly getting syntax errors because it doesn't recognize the variable as an input index

Last edited by Envoy376 : 06 June 2018 at 03:46 AM. Reason: clarifying the message
 
  06 June 2018
Except in very specific cases, you almost always want to be using pymel rather than cmds. (The only time to use cmds is specific cases where pymel is slow, or when working around bugs.) It's much cleaner and more robust. Here's a general example of how to do this with pm:


import pymel.core as pm

joints = pm.ls(selection=True)
for idx, joint in enumerate(joints):
 joint_name = 'spine_FK_jnt_%i' % (idx + 1)
 joint.rename(joint_name)
 add_node = pm.shadingNode('plusMinusAverage', asUtility=True, name='%s_multiInput_PMA' % joint_name)
 add_node.output3D.connect(joint.r, f=True)
 
 for ctrl in pm.ls(type='nurbsCurve'):
 node = pm.PyNode(ctrl.nodeName() + '_MDI_%i' % (idx + 1))
 node.outputX.connect(add_node.input3D[0].input3Dx)
 node.outputY.connect(add_node.input3D[0].input3Dy)
 node.outputZ.connect(add_node.input3D[0].input3Dz)


You can connect either the whole input or the individual channels. I prefer to connect the whole input unless I have a reason not to, since it's just fewer connections.

If you're creating the "MDI" node in the same code, you don't need to look it up by name either. Just keep a reference to the PyNode, like add_node above, which will be much more reliable than searching by node name.
 
  06 June 2018
could I possibly get an example of this in cmds? I get that this is the better way to do things but adapting it at this point to fit into the rest of the script that's already written in cmds is a bit much to wrap my head around at this point.

I really appreciate the answer and I'm trying to apply it as best I can, and I completely plan to learn pymel as well

but for this exact script, this is an assessment that's due very soon and this is the part I'm stuck on.

aside from that I've tried this and gotten this error


# Error: AttributeError: file <maya console> line 1: 'unicode' object has no attribute 'input3D' #


when inputting the script

joints = cmds.ls(selection=True)
for x in range(0, len(joints)):
    jointName = 'spine_FK_jnt_' + str(x + 1)
    joints[x] = cmds.rename(joints[x], (jointName))
    multiInput = cmds.shadingNode('plusMinusAverage', asUtility=True, name=jointName + '_multiInput_PMA')
    cmds.connectAttr(multiInput + '.output3D.output3Dx', jointName + '.rx', f=True)
    cmds.connectAttr(multiInput + '.output3D.output3Dy', jointName + '.ry', f=True)
    cmds.connectAttr(multiInput + '.output3D.output3Dz', jointName + '.rz', f=True)
            
    #make a list of the controllers we're using
    FK_controls = []
    controls = cmds.ls(type='transform', ln=True)    
    for ctrl in controls:
        if "spine_FK_ctrl" in ctrl:
            FK_controls.append(ctrl)            
            #create the PMA, RMV and MDI nodes for each controller per joint
            PMA = cmds.shadingNode('plusMinusAverage', asUtility=True, name=ctrl + '_PMA_' + str(x + 1))
            cmds.setAttr(PMA + '.input3D[0].input3Dx', (x))
            cmds.setAttr(PMA + '.input3D[0].input3Dy', (x + 2))
            RMV = cmds.shadingNode('remapValue', asUtility=True, name=ctrl + '_RMV_' + str(x + 1))
            cmds.setAttr(RMV + '.value[1].value_Position', 1)
            cmds.setAttr(RMV + '.value[1].value_Interp', 1)
            cmds.setAttr(RMV + '.value[1].value_FloatValue', 0)
            cmds.setAttr(RMV + '.value[2].value_Position', 0.5)
            cmds.setAttr(RMV + '.value[2].value_Interp', 1)
            cmds.setAttr(RMV + '.value[2].value_FloatValue', 1)
            node = cmds.shadingNode('multiplyDivide', asUtility=True, name=ctrl + '_MDI_' + str(x + 1))
            MDI = pm.PyNode(node)
            #connect the PMA to the RMV nodes
            cmds.connectAttr(PMA + '.output3D.output3Dx', RMV + '.inputMin', f=True)
            cmds.connectAttr(PMA + '.output3D.output3Dy', RMV + '.inputMax', f=True)
            #connect the RMV to the MDI nodes
            cmds.connectAttr(RMV + '.outValue', MDI + '.input2X', f=True)
            cmds.connectAttr(RMV + '.outValue', MDI + '.input2Y', f=True)
            cmds.connectAttr(RMV + '.outValue', MDI + '.input2Z', f=True)
            
            #connect MDI nodes to multiInput nodes
            #cmds.connectAttr(MDI + '.output', multiInput + '.input3D[0]'
            MDI.outputX.connect(multiInput.input3D[0].input3Dx)

it might be a bit of a mess but I've been mixing and matching between pymel and python a bit
I've incrementally tested the code and it only errors once I get to the final two lines (same thing in cmds as pymel I think)

Last edited by Envoy376 : 06 June 2018 at 12:12 PM.
 
reply share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 04:37 PM.


Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.