Last active
August 29, 2015 14:02
-
-
Save gregtemp/12987782c0c36d626856 to your computer and use it in GitHub Desktop.
today maya mel python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# master lists version - Boids | |
import maya.cmds as cmds | |
import random | |
from maya.OpenMaya import MVector | |
random.seed(1234) | |
# setup | |
amountOfParticles = 50 | |
r = 10 | |
vr = 10 | |
perceptionDist = 20 | |
perceptionAngle = 0 #0 is perpendicular, 0-1 : front, 0-(-1) : back | |
positionList = list() | |
velocityList = list() | |
### -- FUNCTIONS -- ### | |
def deleteAllCreateAgain (): | |
#list particles and delete | |
# convenience | |
particleList = list() | |
tempList = list() | |
particleList = cmds.ls( 'particleObject*' ) | |
#if there are any, then delete them all | |
if len(particleList) >= 1: | |
cmds.delete(particleList) | |
print ('--- print --- deleted old particleObject') | |
else: | |
print ('--- 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) | |
newVector = (x,y,z) | |
tempList.append(newVector) | |
positionList.append(MVector(*newVector)) | |
x = random.uniform(- r, r) | |
y = random.uniform(- r, r) | |
z = random.uniform(- r, r) | |
newVector = (x,y,z) | |
velocityList.append(MVector(*newVector)) | |
cmds.particle(p=tempList,n='particleObject#') | |
def sendToMaster (pID, myPx, myPy, myPz, myVx, myVy, myVz): | |
positionList[pID] = MVector(myPx, myPy, myPz) | |
velocityList[pID] = MVector(myVx, myVy, myVz) | |
def resetFromMaster (pID): | |
x = random.uniform(- r, r) | |
y = random.uniform(- r, r) | |
z = random.uniform(- r, r) | |
positionList[pID] = MVector(x,y,z) | |
x = random.uniform(- r, r) | |
y = random.uniform(- r, r) | |
z = random.uniform(- r, r) | |
velocityList[pID] = MVector(x,y,z) | |
tempP = positionList[pID] | |
tempV = velocityList[pID] | |
listForMel = [tempP.x, tempP.y, tempP.z, tempV.x, tempV.y, tempV.z] | |
return listForMel | |
def readFromMaster (pID): | |
tempP = positionList[pID] | |
tempV = velocityList[pID] | |
listForMel = [tempP.x, tempP.y, tempP.z, tempV.x, tempV.y, tempV.z] | |
return listForMel | |
def calculateNeighbor(pID): | |
for i in range(0, amountOfParticles): | |
if i == id: | |
continue | |
dist = MVector(positionList[pID]-positionList[i]).length() | |
if dist >= perceptionDiff: | |
continue | |
dotProduct = (velocityList[pID].normal() * velocityList[i].normal()) | |
if dotProduct < perceptionAngle: | |
continue | |
## Do something with neighbors | |
tempP = positionList[pID] | |
tempV = velocityList[pID] | |
listForMel = [tempP.x, tempP.y, tempP.z, tempV.x, tempV.y, tempV.z] | |
return listForMel | |
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]>>;' | |
bigString = expString0 + expString1 + expString2 | |
cmds.dynExpression( 'particleObject*Shape', s=bigString, c=1) | |
def rBDExpression (): | |
expString0 = 'vector $myP = particleObject2Shape.position; vector $myV = particleObject2Shape.velocity; ' | |
expString1 = 'string $sendToMasterPy = "sendToMaster(" + particleObject2Shape.particleId + ", " + $myP.x + ", " + $myP.y + ", " + $myP.z + ", " + $myV.x + ", " + $myV.y + ", " + $myV.z + ")"; python ($sendToMasterPy); ' | |
expString2 = 'string $strForPy = "calculateNeighbor(" + particleObject2Shape.particleId + ")"; float $resultList[] = python($strForPy); vector $posV = <<$resultList[0], $resultList[1], $resultList[2]>>; vector $velV = <<$resultList[3], $resultList[4], $resultList[5]>>; particleObject2Shape.position = $posV; particleObject2Shape.velocity = $velV;' | |
bigString = expString0 + expString1 + expString2 | |
cmds.dynExpression( 'particleObject*Shape', s= bigString, rbd=1) | |
### -- EXEC ORDER -- ### | |
deleteAllCreateAgain() | |
cExpression() | |
rBDExpression() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment