Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Created June 10, 2014 20:02
Show Gist options
  • Save gregtemp/3575a9065655c7fe2719 to your computer and use it in GitHub Desktop.
Save gregtemp/3575a9065655c7fe2719 to your computer and use it in GitHub Desktop.
Basic random particles - MAYA python/mel
import maya.cmds as cmds
import maya.OpenMaya as om
import random
random.seed (1234)
amountOfParticles = 5000
positionList = list()
velocityList = list()
accelList = list()
def resetFromMaster(pID):
x = random.uniform(0,10)
y = random.uniform(0,10)
z = random.uniform(0,10)
newVector = (x,y,z)
if (len(positionList) <= pID):
positionList.append(om.MVector(*newVector))
else:
positionList[pID] = om.MVector(*newVector)
newVector = (0,0,2)
if (len(velocityList) <= pID):
velocityList.append(om.MVector(*newVector))
else:
velocityList[pID] = om.MVector(*newVector)
newVector = (0,0,0)
if (len(accelList) <= pID):
accelList.append(om.MVector(*newVector))
else:
accelList[pID] = om.MVector(*newVector)
tempP = positionList[pID]
tempV = velocityList[pID]
tempA = accelList[pID]
listForMel = [tempP.x, tempP.y, tempP.z, tempV.x, tempV.y, tempV.z, tempA.x, tempA.y, tempA.z]
return listForMel
def createParticles():
tempList = list()
for i in range( 0, amountOfParticles):
resetFromMaster(i)
x = positionList[i].x
y = positionList[i].y
z = positionList[i].z
newVector = (x,y,z)
tempList.append(newVector)
cmds.particle(p=tempList,n='particleObject#')
def cExpression ():
expString0 = 'string $strForPy1 = "resetFromMaster(" + particleObject2Shape.particleId + ")";'
expString1 = 'float $resultList[] = python($strForPy1);'
expString2 = 'particleObject2Shape.position = <<$resultList[0], $resultList[1], $resultList[2]>>; particleObject2Shape.velocity = <<$resultList[3], $resultList[4], $resultList[5]>>; particleObject2Shape.acceleration = <<$resultList[6], $resultList[7], $resultList[8]>>;'
bigString = expString0 + expString1 + expString2
cmds.dynExpression( 'particleObject*Shape', s=bigString, c=1)
createParticles()
cExpression()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment