Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Created June 5, 2014 21:23
Show Gist options
  • Save gregtemp/6f59c6c23c4a156f1578 to your computer and use it in GitHub Desktop.
Save gregtemp/6f59c6c23c4a156f1578 to your computer and use it in GitHub Desktop.
Random particle velocity -- with individual particle access (at long last) + (some extra stuff)
# random particle velocity -- with individual particle access (at long last)
import maya.cmds as cmds
import random
random.seed(1234)
amountOfParticles = 500
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')
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)
particleResult = cmds.particle(p=myList,n='particleObject#')
#set all particles to specific velocity
#cmds.setParticleAttr( 'particleObject*', at='velocity', vv=(1, 2, 3) )
#set all particles to random velocity between (-x,-y,-z) and (x,y,z)
#cmds.setParticleAttr( 'particleObject*Shape', at='velocity', rv=(2,2,2))
# doest seem to work
#cmds.select( 'particleObject*Shape.pt[0]')
#cmds.setParticleAttr( vv=(1, 2, 3), at='velocity' )
#cmds.setParticleAttr( 'particleObject*', at='velocity' )
#velExpString = 'if(particleId == %s) velocity = <<%s, %s, %s>>;' %(myParticleId, vx, vy, vz)
#print (velExpString)
#cmds.dynExpression( 'particleObject*Shape', s='velocity = <<rand(5), rand(5), rand(5)>>;', rbd=1 )
#cmds.dynExpression( 'particleObject*Shape', s='if(particleId == 1) velocity = <<0,1,0>>;', rbd=1 )
for myParticleId in range ( 0, amountOfParticles):
vx = random.uniform(- vr, vr)
vy = random.uniform(- vr, vr)
vy = random.uniform(- vr, vr)
velExpString = 'if(particleId == %s) velocity = <<%s, %s, %s>>;' %(myParticleId, vx, vy, vz)
print (velExpString)
cmds.dynExpression( 'particleObject*Shape', s=velExpString, rbd=1 )
@gregtemp
Copy link
Author

gregtemp commented Jun 5, 2014

Just kidding, this only seems to work the first time you press play (WHY???)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment