Created
June 11, 2014 21:35
-
-
Save gregtemp/8705991fd7f27718d8ff to your computer and use it in GitHub Desktop.
newFlockingAttempt python maya mel
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
import maya.cmds as cmds | |
import maya.OpenMaya as om | |
import random | |
random.seed (1234) | |
amountOfParticles = 10 | |
dist = 10 | |
ease = 1 | |
positionList = list() | |
velocityList = list() | |
tvelList = list() | |
def resetFromMaster(pID): | |
x = random.uniform(10,20) | |
y = random.uniform(10,20) | |
z = random.uniform(10,20) | |
newVector = (x,y,z) | |
if (len(positionList) <= pID): | |
positionList.append(om.MVector(*newVector)) | |
else: | |
positionList[pID] = om.MVector(*newVector) | |
newVector = (random.uniform(1,3),random.uniform(1,3),random.uniform(1,3)) | |
if (len(velocityList) <= pID): | |
velocityList.append(om.MVector(*newVector)) | |
tvelList.append(om.MVector(*newVector)) | |
else: | |
velocityList[pID] = om.MVector(*newVector) | |
tvelList[pID] = om.MVector(*newVector) | |
tempP = positionList[pID] | |
tempV = velocityList[pID] | |
listForMel = [tempP.x, tempP.y, tempP.z, tempV.x, tempV.y, tempV.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 updateLists(pID, px, py, pz, vx, vy, vz): | |
positionList[pID] = om.MVector(px, py, pz) | |
velocityList[pID] = om.MVector(vx, vy, vz) | |
def updatePosition(pID): | |
tempPos = (positionList[pID].x, positionList[pID].y, positionList[pID].z) | |
return tempPos | |
def updateVelocity(pID): | |
tempVel = (velocityList[pID].x, velocityList[pID].y, velocityList[pID].z) | |
return tempVel | |
def computeNeighbors(pID, neighborList): | |
myVel = velocityList[pID] | |
for i in range (0, len(neigborList)): | |
v2 = neighborList[i] | |
v1 = myVel | |
v1.rotateTo(v2) | |
tvelList[pID] = v1 | |
tvelList[pID] = totalVector | |
def findNeighbors(pID): | |
tempList = list() | |
for i in range(0, amountOfParticles): | |
if i == pID: | |
continue | |
if (velocityList[i].angle(velocityList[pID]) < 0.2): | |
continue | |
if (om.MVector(positionList[i]-positionList[pID]).length() > dist): | |
continue | |
tempList.append(velocityList[pID]) | |
if (len(tempList) > 0): | |
tvelList[pID].rotateTo(tempList[0]) | |
#computeNeighbors(pID, tempList) | |
def easing(): | |
#print('easing happening') | |
for i in range (0,amountOfParticles): | |
tv = tvelList[i] | |
v = velocityList[i] | |
if (abs(tv.x-v.x)>1): | |
v.x += (tv.x - v.x) * ease | |
if (abs(tv.y-v.y)>1): | |
v.y += (tv.y - v.y) * ease | |
if (abs(tv.z-v.z)>1): | |
v.z += (tv.z - v.z) * ease | |
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( 'particleObject2Shape', s=bigString, c=1) | |
def rBDExpression (): | |
# expString0 = 'vector $posV = particleObject2Shape.position;' | |
# expString1 = 'vector $velV = particleObject2Shape.position;' | |
# expString2 = 'string $strForPy2 = "updateLists(" + particleObject2Shape.particleId + "," + $posV.x + "," + $posV.y + "," + $posV.z + "," + $velV.x + "," + $velV.y + "," + $velV.z + ")";' | |
# expString2 = 'python($strForPy2); ' | |
expString0 = 'string $getPos = "updatePosition(" + particleObject2Shape.particleId + ")"; float $posArray[] = python($getPos); particleObject2Shape.position = ($posArray[0], $posArray[1], $posArray[2]);' | |
expString1 = 'string $getVel = "updateVelocity(" + particleObject2Shape.particleId + ")"; float $velArray[] = python($getVel); particleObject2Shape.velocity = ($velArray[0], $velArray[1], $velArray[2]);' | |
expString2 = 'string $strForPy3 = "findNeighbors(" + particleObject2Shape.particleId + ")"; python($strForPy3); ' | |
expString3 = 'if(particleObject2Shape.particleId == 0) python("easing()");' | |
bigString = expString0 + expString1 + expString2 + expString3 | |
cmds.dynExpression( 'particleObject2Shape', s=bigString, rbd=1) | |
createParticles() | |
cExpression() | |
rBDExpression() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this doesnt work yet