Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Last active August 29, 2015 14:02
Show Gist options
  • Save gregtemp/64c255e111e976fb9bc1 to your computer and use it in GitHub Desktop.
Save gregtemp/64c255e111e976fb9bc1 to your computer and use it in GitHub Desktop.
Random particle velocity in Maya-- (the right way) && (only on creation)
# random particle velocity -- (the right way)
import maya.cmds as cmds
import random
random.seed(1234)
# setup
amountOfParticles = 50
r = 10
vr = 4
myList = list()
#list particles and delete
# convenience
particleList = cmds.ls( 'particleObject*' )
#if there are any, then delete them all
if len(particleList) >= 1:
cmds.delete(particleList)
print ('deleted old particleObject')
else:
print ('no old particleObjects')
# make all particles and add em
for i in range( 0, amountOfParticles):
x = random.uniform(- r, r)
y = random.uniform(- r, r)
z = random.uniform(- r, r)
newPosition = (x,y,z)
myList.append(newPosition)
cmds.particle(p=myList,n='particleObject#')
velCExpString = 'float $myRange = %s; float $vx = rand(-$myRange, $myRange); float $vy = rand(-$myRange, $myRange); float $vz = rand(-$myRange, $myRange); particleObject2Shape.velocity = <<$vx, $vy, $vz>>;' %(vr)
#just a little check to see if im doing it right
print (velCExpString)
#the dynExp iterates through each particle... so you dont have to.
cmds.dynExpression( 'particleObject*Shape', s=velCExpString, c=1 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment