Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Created June 5, 2014 16:19
Show Gist options
  • Save gregtemp/67e61f13d3ac6c77eef9 to your computer and use it in GitHub Desktop.
Save gregtemp/67e61f13d3ac6c77eef9 to your computer and use it in GitHub Desktop.
Random cubes in Maya
# randomCubes.py
import maya.cmds as cmds
import random
#get the same list of "random" numbers each time
random.seed (1234)
''' <- this is how you comment out large chunks
#list all cubes called myCube in the scene
cubeList = cmds.ls( 'myCube*' )
#if there are any, then delete them all
if len(cubeList) > 1:
cmds.delete(cubeList)
'''
#make a new polyCube, name it myCube# (# means whatever number is next). save it as "result"
result = cmds.polyCube (w=1, h=1, d=1, name='myCube#')
#print ('result:' + str(result))
#result contains the cube (named myCube1) and the cube shape (named polyCube1)
#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