PDA

View Full Version : Python nHair batch make many curves Dynamic


dimitarsp
06-03-2012, 11:52 AM
Hi,

I am trying to make follicles for each curve in my list, and connect the the appropriate node connections. I am quite new to python so I am still not 100% solid on the syntax.

import maya.cmds as cmds

curvesInputList = []
curvesInputList = cmds.ls(sl=1)

# --------------- create the necessary nodes
dynamicHair = cmds.createNode ('hairSystem')
nucleus = cmds.createNode ('nucleus')

# ------ connect time node to hair and nucleus node
cmds.connectAttr ('time1.outTime', dynamicHair+'.currentTime')
cmds.connectAttr ('time1.outTime', nucleus+'.currentTime')

# ------- connect nucleus node to hair node
cmds.connectAttr (nucleus+'.startFrame', dynamicHair+'.startFrame')
cmds.connectAttr (nucleus+'.outputObjects[0]', dynamicHair+'.nextState')
cmds.connectAttr(dynamicHair+'.currentState', nucleus+'.inputActive[0]')
cmds.connectAttr(dynamicHair+'.startState', nucleus+'.inputActiveStart[0]')

for i in curvesInputList:
a = cmds.rebuildCurve (i, rt=0, spans=50, ch=0, replaceOriginal=1)
b = cmds.createNode ('follicle')
c = cmds.createNode ('nurbsCurve')

# --------------- connect follicle node to hair node
d = cmds.connectAttr (dynamicHair+'.outputHair[%s]' % (i), b+'.currentPosition')
e = cmds.connectAttr (b+'.outHair', dynamicHair+'.inputHair[%s]' % (i))

# ------------- connect follicle node to input curve
cmds.connectAttr(i+'.local',b+'.startPosition')
cmds.connectAttr(i+'.worldMatrix[0]',b+'.startPositionMatrix')

# ------------- connect follicle node to output curve
cmds.connectAttr(b+'.outCurve', c+'.create')

and I get the following error:

# Error: RuntimeError: file <maya console> line 7: The source attribute 'hairSystemShape2.outputHair[curve1]' cannot be found. #

I know that i need to convert my curvesInputList from the current form - [u'curve1', u'curve2', u'curve3', u'curve4'] to something like [0 1 2 3 4]. So I also tried:

for i in curvesInputList:
a = cmds.rebuildCurve (i, rt=0, spans=50, ch=0, replaceOriginal=1)
b = cmds.createNode ('follicle')
c = cmds.createNode ('nurbsCurve')
for j in range(0,len(curvesInputList)):

# --------------- connect follicle node to hair node
d = cmds.connectAttr (dynamicHair+'.outputHair[%s]' % (j), b+'.currentPosition')
e = cmds.connectAttr (b+'.outHair', dynamicHair+'.inputHair[%s]' % (j))

# ------------- connect follicle node to input curve
cmds.connectAttr(i+'.local',b+'.startPosition')
cmds.connectAttr(i+'.worldMatrix[0]',b+'.startPositionMatrix')

# ------------- connect follicle node to output curve
cmds.connectAttr(b+'.outCurve', c+'.create')

and I get the following error:

# Error: RuntimeError: file <maya console> line 7: 'follicleShape1.currentPosition' already has an incoming connection from 'hairSystemShape1.outputHair[0]'. #

I know this is because of the nested for loop, but I have no idea how to fix it.

Any help would greatly appreciated!
Dimitar

dimitarsp
06-03-2012, 07:40 PM
just found out about the enumerate function :))), which was exactly what i was looking for

#automate the process of assigning nHair to selected curves
import maya.cmds as cmds


myCurve = cmds.curve( p=[(0, 0, 0), (3, 5, 6), (5, 6, 7), (9, 9, 9)] )
curvesInputList = [myCurve]

for i in range(0,5):
dupli_curve= cmds.duplicate(myCurve)
cmds.move(0,2*i, dupli_curve)
curvesInputList.extend(dupli_curve)


dynamicHair = cmds.createNode ('hairSystem')
nucleus = cmds.createNode ('nucleus')

#connect time node to hair and nucleus node
cmds.connectAttr ('time1.outTime', dynamicHair+'.currentTime')
cmds.connectAttr ('time1.outTime', nucleus+'.currentTime')

# connect nucleus node to hair node
cmds.connectAttr (nucleus+'.startFrame', dynamicHair+'.startFrame')
cmds.connectAttr (nucleus+'.outputObjects[0]', dynamicHair+'.nextState')
cmds.connectAttr(dynamicHair+'.currentState', nucleus+'.inputActive[0]')
cmds.connectAttr(dynamicHair+'.startState', nucleus+'.inputActiveStart[0]')



for index, i in enumerate(curvesInputList):
print "This is %s and it is number %s" % (i, index)


for index, i in enumerate(curvesInputList):
cmds.rebuildCurve (i, rt=0, spans=50, ch=0, replaceOriginal=1)
_follicle = cmds.createNode ('follicle')
_nurbsCurve = cmds.createNode ('nurbsCurve')
# connect nodes
cmds.connectAttr (dynamicHair+'.outputHair[%s]' % (index), _follicle+'.currentPosition')
cmds.connectAttr (_follicle+'.outHair', dynamicHair+'.inputHair[%s]' % (index))
# connect follicle node to input curve
cmds.connectAttr(i+'.local',_follicle+'.startPosition')
cmds.connectAttr(i+'.worldMatrix[0]',_follicle+'.startPositionMatrix')
# connect follicle node to output curve
cmds.connectAttr(_follicle+'.outCurve', _nurbsCurve+'.create')

CGTalk Moderation
06-03-2012, 07:40 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.