Hi guys,
So I'm trying to write a script where I can scale the light exposure ( or intensity) based on the boundingbox of selected object when created. So what I have is a JSON file with position, scale, rotation and basic light presets in it.
The code I'm sharing now goes into the JSON file, reads in what I need and starts creating the lights by setting the attributes from the JSON file to the created lights. Then I scale the whole light setup ( which is in a group) until it matches the selected object. The thing is, I want to keep a constant light exposure so If I have 2 objects, one really small and one really big, I'd like to have the same light on both models. With the current code my light gets waaay too dimmed when I apply the script on a big object.
I tried using a piece of code I found on the internet which works with vectors, but since I don't really understand how it works I can't do really much with it.
I'm trying to figure out how to calculate this but I really have no idea since I don't really use advanced calculations in my codes.
If you guys could help me with that I would be very grateful !
Thanks in advance !
Also the piece of code I used from the internet is this one, you'll find the rest of my code below that.
v1 = [bbox[0], bbox[1], bbox[2]]
v2 = [bbox[3], bbox[4], bbox[5]]
v3 = [v2[0]- v1[0], v2[1]-v1[1], v2[2]-v1[2]]
vLength = math.sqrt(v3[0]**2 + v3[1]**2 + v3[2]**2)
#returns int that cannot be smaller than vLength
rounded = math.ceil(vLength)
scaleFactor = rounded/7.0
I used the last variable in the setAttr command for exposure
Whole code:
def lightSetup(jsonData, setup):
#lightData = need group read in from JSon
lightData = jsonData[setup]
sel = cmds.ls(sl=True, l=True)
if len(sel) == 0:
print('Select an object.')
else:
if cmds.objExists('SetuplightGroup'):
cmds.delete('SetuplightGroup')
groupingSetupLight = cmds.group( n = 'SetuplightGroup', em = True)
bbox = cmds.exactWorldBoundingBox(sel)
objectWidth = (bbox[3] - bbox[0])
objectHeight = (bbox[4] - bbox[1])
objectDepth = (bbox[5] - bbox[2])
positionX = bbox[0] + (objectWidth/2)
positionY = bbox[1] + (objectHeight/2)
positionZ = bbox[2] + (objectDepth/2)
counter = 0
v1 = [bbox[0], bbox[1], bbox[2]]
v2 = [bbox[3], bbox[4], bbox[5]]
v3 = [v2[0]- v1[0], v2[1]-v1[1], v2[2]-v1[2]]
vLength = math.sqrt(v3[0]**2 + v3[1]**2 + v3[2]**2)
rounded = math.ceil(vLength)
scaleFactor = rounded/7.0
#exposureScale = 0
#For each element in my lightData ( group from read in JSON) do following code
for lightElement in lightData:
counter += 1
createLight = cmds.shadingNode('aiAreaLight', asLight=True)
cmds.setAttr(createLight + ".tx", lightElement['xPos'])
cmds.setAttr(createLight + ".ty", lightElement['yPos'])
cmds.setAttr(createLight + ".tz", lightElement['zPos'])
cmds.setAttr(createLight + ".rx", lightElement['xRot'])
cmds.setAttr(createLight + ".ry", lightElement['yRot'])
cmds.setAttr(createLight + ".rz", lightElement['zRot'])
cmds.setAttr(createLight + ".sx", lightElement['xScale'])
cmds.setAttr(createLight + ".sy", lightElement['yScale'])
cmds.setAttr(createLight + ".sz", lightElement['zScale'])
cmds.setAttr(createLight + ".color", lightElement['r'], lightElement['g'], lightElement['b'], type="double3")
cmds.setAttr(createLight + ".exposure", lightElement['exposure'],scaleFactor)
cmds.setAttr(createLight + ".intensity", lightElement['intensity'])
cmds.setAttr(createLight + ".aiSpread", lightElement['spread'])
cmds.parent(createLight, groupingSetupLight)
cmds.matchTransform(groupingSetupLight, sel, pos = True, rot = True)
cmds.select(groupingSetupLight)
cmds.scale(objectWidth/4,objectWidth/4,objectWidth/4)
cmds.select(sel)