Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Created June 5, 2014 16:25
Show Gist options
  • Save gregtemp/03925d45bea2b6a57a3e to your computer and use it in GitHub Desktop.
Save gregtemp/03925d45bea2b6a57a3e to your computer and use it in GitHub Desktop.
Random instances of first selected object in Maya
# randomInstances.py
import maya.cmds as cmds
import random
#get the same list of "random" numbers each time
random.seed (1234)
result = cmds.ls(orderedSelection = True)
print ('result: %s' % (result))
#get just the shape in order to instance
transformName = result[0]
#create an empty group for our instances
instanceGroupName = cmds.group( empty = True, name=transformName + '_instance_grp#')
for i in range( 0, 50):
#instance initial cube
instanceResult = cmds.instance( transformName, name = transformName + '_instance#')
#add the new instance to our instance list
cmds.parent(instanceResult, instanceGroupName)
#print( 'instanceResult:' + str(instanceResult))
#random location
x = random.uniform ( -10, 10)
y = random.uniform ( 0, 20)
z = random.uniform ( -10, 10)
cmds.move( x, y, z, instanceResult)
#random rotation
xRot = random.uniform( 0, 360)
yRot = random.uniform( 0, 360)
zRot = random.uniform( 0, 360)
cmds.rotate(xRot, yRot, zRot, instanceResult)
#random scale
scalingFactor = random.uniform(0.3, 1.5)
cmds.scale(scalingFactor, scalingFactor, scalingFactor, instanceResult)
#hide initial cube
cmds.hide (transformName)
#set a center pivot for the whole group
cmds.xform (instanceGroupName, centerPivots = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment