[Maya Python] Automatic Actions When a Specific Event Occurs

Submitted by Benoit on Sat, 04/24/2010 - 17:37

To automatically execute an action when a specific event occurs use the scriptJob command

Example:

#===========================================================================
# This script automatically scales selected objects
#===========================================================================
 
import pymel as pm 
 
# Clear the scene
pm.newFile(force=1)
 
# Create some objects in the scene
nbSpheres = 10;
 
for i in range(nbSpheres):
    mySphere = pm.polySphere()
    mySphere[0].translateX.set(i)
 
# Create the event handler to scale the objects
eventHandler = pm.scriptJob(e = ["SelectionChanged","ScaleObject()"])
 
def ScaleObject():
 
    objs = pm.ls(type = "transform", sl=True)
 
    for obj in objs:
        obj.scaleX.set(0.5)
        obj.scaleY.set(2)
        obj.scaleZ.set(0.5)

Best regards,
Benoît