PDA

View Full Version : Why does this script choke?


EightBit
10-21-2011, 01:04 AM
Here is a script to create a simple light rig.
It creates a locator which serves as parent to the lites and also has sliders to control the intensity of each lite.
It executes correctly the first time it is run, but if there is an existing rig in the scene, it chokes.
The odd thing is that it chokes with the error that it can't find the intensity of the first lite, even though it can find it's rotations and transforms.
Using the connection editor to make this connection works fine.
Any see what's wrong?
Thanks.
import maya.cmds as cmds
prefix = 'previzLite_'
offset= 5
# create array of lite names
liteNames=['N','NE','E','SE','S','SW','W', 'NW','Top','Bot']
liteSuffix = '_intsty'
liteCtrl = cmds.spaceLocator(n=prefix+'cntrl')[0]
numLites=10

# create array of lite posns
litePosns=[(0,0,-offset), (offset*0.75,0,-offset*0.75), (offset,0,0), (offset*0.75,0,offset*0.75), (0,0,(offset)), (-offset*0.75,0,offset*0.75),(-offset,0,0), (-offset*0.75,0,-offset*0.75), (0,offset,0),(0,(-offset),0)]

#create vector array of Lite Rotns
liteRotations=[(0,180,0), (0,135,0), (0,90,0), (0,45,0), (0,0,0), (0,-45,0), (0,-90,0), (0,-135,0), (-90,0,0), (90,0,0)]

# Create Intensity attrs on controller:
for lite in liteNames:
attrName = lite+liteSuffix
objAttr = '%s.%s' % (liteCtrl,attrName)
if not cmds.objExists (objAttr):
cmds.addAttr(ln = attrName,min = 0, max=5, dv = .5, k = 1)
# print 'attr added'

def pixl_addLites():
c = 0
for lite in liteNames:
newLite=cmds.directionalLight()
# Create Directional Lite
# Return shapeNode name
lightTransf=cmds.listRelatives(newLite,p=1,fullPath=1)
# Get transform. arrayItem[0] will be overwritten.
curLite=cmds.rename(lightTransf[0],prefix+lite)
# Rename transform which will also rename the shapeNode
currentRotn=liteRotations[c]
currentPosn=litePosns[c]
#curLite = cmds.ls(curLite, l=1)[0] # Assign long name
cmds.setAttr((curLite + ".rotate"),currentRotn[0],currentRotn[1],currentRotn[2])
cmds.setAttr((curLite + ".translate"),currentPosn[0],currentPosn[1],currentPosn[2])
cmds.connectAttr (liteCtrl+'.'+lite+liteSuffix, curLite+'Shape.intensity')
cmds.parent(curLite,liteCtrl)
c+=1
cmds.select (liteCtrl)

pixl_addLites()

dbsmith
10-21-2011, 02:28 AM
You cannot just assume your shape name is the same name as the transform with the word 'Shape' appended.

Use something like:

maya.cmds.listRelatives(curLite, shapes=True)


to find the shape names.

EightBit
10-21-2011, 04:36 AM
No doubt that is true, but in this case, a shapeNode with that name does exist, so I don't know why Maya doesn't see it...
At any rate, I implemented your suggestion, and w/some additional code to keep track of objects w/the same name, it now works.
Thanks once again db.
For the record:
def pixl_addLites():
c = 0
for lite in liteNames:
newLite=cmds.directionalLight()
# Create Directional Lite
# Return shapeNode name
lightTransf=cmds.listRelatives(newLite,p=1,fullPath=1)
# Get transform. arrayItem[0] will be overwritten.
curLite=cmds.rename(lightTransf[0],prefix+lite)
# Rename transform which will also rename the shapeNode
currentRotn=liteRotations[c]
currentPosn=litePosns[c]
curLite = cmds.ls(curLite, l=1)[0] # Assign long name so multiple rigs can be created
# print 'curLite:', curLite
cmds.setAttr((curLite + '.rotate'),currentRotn[0],currentRotn[1],currentRotn[2])
cmds.setAttr((curLite + '.translate'),currentPosn[0],currentPosn[1],currentPosn[2])

# Get shapeNode to connect intensity to control:
curLiteShape = cmds.listRelatives(curLite, shapes=1, f = 1)[0] # Get full pathname so multiple rigs can be created
# print 'curLiteShape:',curLiteShape
cmds.connectAttr (liteCtrl+'.'+lite+liteSuffix, curLiteShape+'.intensity')
cmds.parent(curLite,liteCtrl)
c+=1
cmds.select (liteCtrl)

pixl_addLites()

CGTalk Moderation
10-21-2011, 04:36 AM
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.