Skip to content

Instantly share code, notes, and snippets.

@gregtemp
Last active October 13, 2017 09:11
Show Gist options
  • Save gregtemp/51f1e8150ca37a812c99 to your computer and use it in GitHub Desktop.
Save gregtemp/51f1e8150ca37a812c99 to your computer and use it in GitHub Desktop.
Flocking (a couple different versions... none of which really work) - MAYA mel python
# 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
perceptionDistMin = 2
perceptionAngle = 0 #0 is perpendicular, 0-1 : front, 0-(-1) : back
alignWeight = 1.0
cohesionWeight = 0.7
separationWeight = 0.2
positionList = list()
velocityList = list()
accelList = 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/5.0, r/5.0)
y = random.uniform(- r/5.0, r/5.0)
z = random.uniform(- r/5.0, r/5.0)
newVector = (x,y,z)
velocityList.append(MVector(*newVector))
x = 0
y = 0
z = 0
newVector = (x,y,z)
accelList.append(MVector(*newVector))
cmds.particle(p=tempList,n='particleObject#')
def sendToMaster (pID, myPx, myPy, myPz, myVx, myVy, myVz, myAx, myAy, myAz):
positionList[pID] = MVector(myPx, myPy, myPz)
velocityList[pID] = MVector(myVx, myVy, myVz)
accelList[pID] = MVector(myAx, myAy, myAz)
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/5.0, r/5.0)
y = random.uniform(- r/5.0, r/5.0)
z = random.uniform(- r/5.0, r/5.0)
velocityList[pID] = MVector(x,y,z)
x = 0
y = 0
z = 0
accelList[pID] = MVector(x,y,z)
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 readFromMaster (pID):
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 calculateNeighbor(pID):
pAlignment = MVector()
pCohesion = MVector()
pSeparation = MVector()
for i in range(0, amountOfParticles):
if i == id:
continue
dist = MVector(positionList[pID]-positionList[i])
if dist.length() >= perceptionDiff:
continue
dotProduct = (velocityList[pID].normal() * velocityList[i].normal())
if dotProduct < perceptionAngle:
continue
## now we only have neighbors left
## Do something with neighbors
velDiff = MVector(velocityList[pID]-velocityList[i])
#add up alignment, cohesion and separation
pAlignment += velDiff
pCohesion += dist
if dist.length() < perceptionDistMin:
pSeparation += dist
if pAlignment.length() > 0:
pAlignment.normal()
if pCohesion.length() > 0:
pCohesion.normal()
if pSeparation.length() > 0:
pSeparation.normal()
pAlignment *= alignWeight
pCohesion *= cohesionWeight
pSeparation *= separationWeight
accelList[pID] = pAlignment
accelList[pID] += pCohesion
accelList[pID] -= pSeparation
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 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)
def rBDExpression ():
expString0 = 'vector $myP = particleObject2Shape.position; vector $myV = particleObject2Shape.velocity; vector $myA = particleObject2Shape.acceleration; '
expString1 = 'string $sendToMasterPy = "sendToMaster(" + particleObject2Shape.particleId + ", " + $myP.x + ", " + $myP.y + ", " + $myP.z + ", " + $myV.x + ", " + $myV.y + ", " + $myV.z + ", " + $myA.x + ", " + $myA.y + ", " + $myA.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]>>; vector $accV = <<$resultList[6], $resultList[7], $resultList[8]>>; particleObject2Shape.position = $posV; particleObject2Shape.velocity = $velV; particleObject2Shape.acceleration = $accV;'
bigString = expString0 + expString1 + expString2
cmds.dynExpression( 'particleObject*Shape', s= bigString, rbd=1)
### -- EXEC ORDER -- ###
deleteAllCreateAgain()
cExpression()
rBDExpression()
## THIS ONE WORKS!! i think...
# master lists version - Boids
import maya.cmds as cmds
import random
from maya.OpenMaya import MVector
random.seed(1234)
# setup
amountOfParticles = 100
r = 10
vr = 10
perceptionDist = 5
perceptionDistMin = 0.2
perceptionAngle = 0.5 #0 is perpendicular, 0-1 : front, 0-(-1) : back
alignWeight = 0.0
cohesionWeight = 0.7
separationWeight = 1.5
accelLimit = 100
positionList = list()
velocityList = list()
accelList = 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 = 0
y = 0
z = 0
newVector = (x,y,z)
tempList.append(newVector)
positionList.append(MVector(*newVector))
newVector = (x,y,z)
velocityList.append(MVector(*newVector))
newVector = (x,y,z)
accelList.append(MVector(*newVector))
cmds.particle(p=tempList,n='particleObject#')
def sendToMaster (pID, myPx, myPy, myPz, myVx, myVy, myVz, myAx, myAy, myAz):
positionList[pID] = MVector(myPx, myPy, myPz)
velocityList[pID] = MVector(myVx, myVy, myVz)
accelList[pID] = MVector(myAx, myAy, myAz)
def resetFromMaster (pID):
x = random.uniform(0, 2*r)
y = random.uniform(0, 2*r)
z = random.uniform(2, 2*r)
positionList[pID] = MVector(x,y,z)
x = 2
y = 0
z = 0
velocityList[pID] = MVector(x,y,z)
x = 0
y = 0
z = 0
accelList[pID] = MVector(x,y,z)
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 readFromMaster (pID):
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 calculateNeighbor(pID):
pAlignment = MVector()
pCohesion = MVector()
pSeparation = MVector()
for i in range(0, amountOfParticles):
if i == id:
continue
dist = MVector(positionList[pID]-positionList[i])
if dist.length() >= perceptionDist:
continue
dotProduct = (velocityList[pID].normal() * velocityList[i].normal())
if dotProduct < perceptionAngle:
continue
## now we only have neighbors left
## Do something with neighbors
velDiff = MVector(velocityList[pID]-velocityList[i])
#add up alignment, cohesion and separation
pAlignment += velDiff
pCohesion += dist
if dist.length() < perceptionDistMin:
pSeparation += dist
'''
if pAlignment.length() > 0:
pAlignment.normal()
if pCohesion.length() > 0:
pCohesion.normal()
if pSeparation.length() > 0:
pSeparation.normal()
'''
#pAlignment.normal()
#pCohesion.normal()
#pSeparation.normal()
pAlignment *= alignWeight
pCohesion *= cohesionWeight
pSeparation *= separationWeight
accelList[pID] = pAlignment
accelList[pID] += pCohesion
accelList[pID] -= pSeparation
tempP = positionList[pID]
tempV = velocityList[pID]
tempA = accelList[pID]
listForMel = [tempA.x, tempA.y, tempA.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]>>; particleObject2Shape.acceleration = <<$resultList[6], $resultList[7], $resultList[8]>>;'
bigString = expString0 + expString1 + expString2
cmds.dynExpression( 'particleObject*Shape', s=bigString, c=1)
def rBDExpression ():
expString0 = 'vector $myP = particleObject2Shape.position; vector $myV = particleObject2Shape.velocity; vector $myA = particleObject2Shape.acceleration; '
expString1 = 'string $sendToMasterPy = "sendToMaster(" + particleObject2Shape.particleId + ", " + $myP.x + ", " + $myP.y + ", " + $myP.z + ", " + $myV.x + ", " + $myV.y + ", " + $myV.z + ", " + $myA.x + ", " + $myA.y + ", " + $myA.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]>>; vector $accV = <<$resultList[6], $resultList[7], $resultList[8]>>; particleObject2Shape.position = $posV; particleObject2Shape.velocity = $velV; particleObject2Shape.acceleration = $accV;'
expString2 = 'string $strForPy = "calculateNeighbor(" + particleObject2Shape.particleId + ")"; float $resultList[] = python($strForPy); vector $accV = <<$resultList[0], $resultList[1], $resultList[2]>>; particleObject2Shape.acceleration = $accV;'
bigString = expString0 + expString1 + expString2
cmds.dynExpression( 'particleObject*Shape', s= bigString, rbd=1)
### -- EXEC ORDER -- ###
deleteAllCreateAgain()
cExpression()
rBDExpression()
## THIS ONE WORKS!! i think...
# 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
perceptionDistMin = 1
perceptionAngle = 0.9 #0 is perpendicular, 0-1 : front, 0-(-1) : back
alignWeight = 1.0
cohesionWeight = 0.1
separationWeight = 0.5
accelLimit = 50
positionList = list()
velocityList = list()
accelList = 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 = 2
y = 0
z = 0
newVector = (x,y,z)
velocityList.append(MVector(*newVector))
x = 0
y = 0
z = 0
newVector = (x,y,z)
accelList.append(MVector(*newVector))
cmds.particle(p=tempList,n='particleObject#')
def sendToMaster (pID, myPx, myPy, myPz, myVx, myVy, myVz, myAx, myAy, myAz):
positionList[pID] = MVector(myPx, myPy, myPz)
velocityList[pID] = MVector(myVx, myVy, myVz)
accelList[pID] = MVector(myAx, myAy, myAz)
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 = 2
y = 0
z = 0
velocityList[pID] = MVector(x,y,z)
x = 0
y = 0
z = 0
accelList[pID] = MVector(x,y,z)
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 readFromMaster (pID):
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 calculateNeighbor(pID):
pAlignment = MVector()
pCohesion = MVector()
pSeparation = MVector()
for i in range(0, amountOfParticles):
if i == id:
continue
dist = MVector(positionList[pID]-positionList[i])
if dist.length() >= perceptionDiff:
continue
dotProduct = (velocityList[pID].normal() * velocityList[i].normal())
if dotProduct < perceptionAngle:
continue
## now we only have neighbors left
## Do something with neighbors
velDiff = MVector(velocityList[pID]-velocityList[i])
#add up alignment, cohesion and separation
pAlignment += velDiff
pCohesion += dist
if dist.length() < perceptionDistMin:
pSeparation += dist
'''
if pAlignment.length() > 0:
pAlignment.normal()
if pCohesion.length() > 0:
pCohesion.normal()
if pSeparation.length() > 0:
pSeparation.normal()
'''
#pAlignment.normal()
#pCohesion.normal()
#pSeparation.normal()
pAlignment *= alignWeight
pCohesion *= cohesionWeight
pSeparation *= separationWeight
accelList[pID] = pAlignment
accelList[pID] += pCohesion
accelList[pID] -= pSeparation
if accelList[pID].length() > accelLimit:
accelList[pID] = MVector(0,0,0)
tempP = positionList[pID]
tempV = velocityList[pID]
tempA = accelList[pID]
listForMel = [tempA.x, tempA.y, tempA.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]>>; particleObject2Shape.acceleration = <<$resultList[6], $resultList[7], $resultList[8]>>;'
bigString = expString0 + expString1 + expString2
cmds.dynExpression( 'particleObject*Shape', s=bigString, c=1)
def rBDExpression ():
expString0 = 'vector $myP = particleObject2Shape.position; vector $myV = particleObject2Shape.velocity; vector $myA = particleObject2Shape.acceleration; '
expString1 = 'string $sendToMasterPy = "sendToMaster(" + particleObject2Shape.particleId + ", " + $myP.x + ", " + $myP.y + ", " + $myP.z + ", " + $myV.x + ", " + $myV.y + ", " + $myV.z + ", " + $myA.x + ", " + $myA.y + ", " + $myA.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]>>; vector $accV = <<$resultList[6], $resultList[7], $resultList[8]>>; particleObject2Shape.position = $posV; particleObject2Shape.velocity = $velV; particleObject2Shape.acceleration = $accV;'
expString2 = 'string $strForPy = "calculateNeighbor(" + particleObject2Shape.particleId + ")"; float $resultList[] = python($strForPy); vector $accV = <<$resultList[0], $resultList[1], $resultList[2]>>; particleObject2Shape.acceleration = $accV;'
bigString = expString0 + expString1 + expString2
cmds.dynExpression( 'particleObject*Shape', s= bigString, rbd=1)
### -- EXEC ORDER -- ###
deleteAllCreateAgain()
cExpression()
rBDExpression()
## THIS ONE WORKS!! i think...
# 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
perceptionDistMin = 0.01
perceptionAngle = 0.2 #0 is perpendicular, 0-1 : front, 0-(-1) : back
alignWeight = 1.0
cohesionWeight = 0.5
separationWeight = 0.2
accelLimit = 5
positionList = list()
velocityList = list()
accelList = 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(0, 5)
y = random.uniform(0, 5)
z = random.uniform(0, 5)
newVector = (x,y,z)
velocityList.append(MVector(*newVector))
x = 0
y = 0
z = 0
newVector = (x,y,z)
accelList.append(MVector(*newVector))
cmds.particle(p=tempList,n='particleObject#')
def sendToMaster (pID, myPx, myPy, myPz, myVx, myVy, myVz, myAx, myAy, myAz):
positionList[pID] = MVector(myPx, myPy, myPz)
velocityList[pID] = MVector(myVx, myVy, myVz)
accelList[pID] = MVector(myAx, myAy, myAz)
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(0, 5)
y = random.uniform(0, 5)
z = random.uniform(0, 5)
velocityList[pID] = MVector(x,y,z)
x = 0
y = 0
z = 0
accelList[pID] = MVector(x,y,z)
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 readFromMaster (pID):
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 calculateNeighbor(pID):
pAlignment = MVector()
pCohesion = MVector()
pSeparation = MVector()
for i in range(0, amountOfParticles):
if i == id:
continue
dist = MVector(positionList[pID]-positionList[i])
if dist.length() >= perceptionDiff:
continue
dotProduct = (velocityList[pID].normal() * velocityList[i].normal())
if dotProduct < perceptionAngle:
continue
## now we only have neighbors left
## Do something with neighbors
velDiff = MVector(velocityList[pID]-velocityList[i])
#add up alignment, cohesion and separation
pAlignment += velDiff
pCohesion += dist
if dist.length() < perceptionDistMin:
pSeparation += dist
if pAlignment.length() > 0:
pAlignment.normal()
if pCohesion.length() > 0:
pCohesion.normal()
if pSeparation.length() > 0:
pSeparation.normal()
pAlignment *= alignWeight
pCohesion *= cohesionWeight
pSeparation *= separationWeight
accelList[pID] = pAlignment
accelList[pID] += pCohesion
accelList[pID] -= pSeparation
if accelList[pID].length() > accelLimit:
accelList[pID] = MVector(0,0,0)
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 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)
def rBDExpression ():
expString0 = 'vector $myP = particleObject2Shape.position; vector $myV = particleObject2Shape.velocity; vector $myA = particleObject2Shape.acceleration; '
expString1 = 'string $sendToMasterPy = "sendToMaster(" + particleObject2Shape.particleId + ", " + $myP.x + ", " + $myP.y + ", " + $myP.z + ", " + $myV.x + ", " + $myV.y + ", " + $myV.z + ", " + $myA.x + ", " + $myA.y + ", " + $myA.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]>>; vector $accV = <<$resultList[6], $resultList[7], $resultList[8]>>; particleObject2Shape.position = $posV; particleObject2Shape.velocity = $velV; particleObject2Shape.acceleration = $accV;'
bigString = expString0 + expString1 + expString2
cmds.dynExpression( 'particleObject*Shape', s= bigString, rbd=1)
### -- EXEC ORDER -- ###
deleteAllCreateAgain()
cExpression()
rBDExpression()
@gregtemp
Copy link
Author

gregtemp commented Jun 9, 2014

turns out this is more of a de-flocking script....... fuck.

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