Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Last active August 29, 2015 14:02
Show Gist options
  • Save gregtemp/f37c8e6723ca06fff2c4 to your computer and use it in GitHub Desktop.
Save gregtemp/f37c8e6723ca06fff2c4 to your computer and use it in GitHub Desktop.
Today2 maya python mel
# random particle velocity -- (the right way)
# maybe try making a list that cross checks before and after dynamics? is that a bad idea?
import maya.cmds as cmds
import random
from maya.OpenMaya import MVector
random.seed(1234)
# setup
amountOfParticles = 50
r = 10
vr = 10
myList = list()
pointList = list()
# functions
def vectorCompHandler (x, y, z, id):
pointList[id] = MVector(x,y,z)
for i in range(0, amountOfParticles):
dist = MVector(pointList[id]-pointList[i]).length()
if dist > 10:
#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)
pointList.append(MVector(0,0,0))
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 ('---print--- velCExpString= ' + velCExpString)
#the dynExp iterates through each particle... so you dont have to.
cmds.dynExpression( 'particleObject*Shape', s=velCExpString, c=1 )
passRBDExpString = 'vector $myv = particleObject2Shape.position; int $pId = particleObject2Shape.particleId; string $myPy = "vectorCompHandler(" + $myv.x + "," + $myv.y + "," + $myv.z + "," + $pId + ")"; python($myPy);'
print ('---print--- passRBDExpString= ' + passRBDExpString)
cmds.dynExpression( 'particleObject*Shape', s= passRBDExpString, rbd=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment