#based on the MEL procedure "createAnnotation" from Alias
#create annotation for each light in the scene
#if the lights' name has been changed, user need to re-excute the script to update the annotation names
import maya.cmds as cmds
def ling_addLights_annotation():
#get all the light from the scene
lightShapeList = cmds.ls(typ='light')
lightList = cmds.listRelatives(lightShapeList , p=1)
for i in range(len(lightList)):
cmds.select(lightList[i],r=1)
relatives = cmds.listRelatives(lightList[i])
#if it has annotation already, just update the light name when necessary
if(len(relatives)>1) :
#compare the name of the light and the annotation
annotationTrans = cmds.listRelatives(relatives[1])
annotationShape = cmds.listRelatives(annotationTrans[1])
annotationName = cmds.getAttr(annotationShape[0]+'.text')
if(lightList[i] != annotationName ):
cmds.setAttr(annotationShape[0]+'.text', lightList[i], type='string')
#else: create the annotation
else:
ling_createAnnotation(lightList[i])
cmds.select(clear=1)
def ling_createAnnotation(annotation):
#get the object to annotate
object= cmds.ls(sl=1)
#make sure it's a valid transform node
checkIfTransform= cmds.ls(object[0],sl=1, typ= 'transform')
if (1 == len(checkIfTransform)):
#carry on if it is and calculate the offset for the annotation
#based on the object's bounding box
bbox = cmds.xform(object[0], q=1, ws=1, bb=1 )
x = abs(bbox[3] - bbox[0])
y = abs(bbox[4] - bbox[1])
z = abs(bbox[5] - bbox[2])
#create a locator for easier manipulation
locator = cmds.spaceLocator(n= "ling_light_annotationLocator#" );
cmds.xform( ws=1 ,t =( (bbox[0]+(x/2)) , (bbox[1]+(y/2)), (bbox[2]+(z/2))) )
cmds.parent(locator, object[0])
x=1
y=3
z=2
#sort the sides of the bounding box from lowest to highest so
#that the annotation has a more predictable and consistent offset
xyz = sorted([x,y,z])
#add annotation
cmds.select(locator,r=1)
annotationNode = cmds.annotate(locator, tx = annotation, p= ( (bbox[0]+xyz[2]) , (bbox[1]+xyz[2]) ,(bbox[2]+xyz[2]) ) )
#get parent transform of annotation
transform = cmds.listRelatives(annotationNode,p=1)
#parent annotation to locator
cmds.parent(transform[0], locator)
cmds.select(annotationNode,r=1)
else:
print("error: Please select the transform node")
ling_addLights_annotation()