Tuesday, July 21, 2009

Assignment 03 B - second try

This is what should be our assignment. The error is the same like in the previous script. Here we added more pavilions and a field for each pavilion and we connected the elements.





###############crowd system
###Assignment 03B

#generate random static attractors
#generate random static repulsors
#generate random dynamic principle attractors
#generate random dynamic followers

#


import maya.cmds as cmds
from random import *


class Crowd:
def __init__(self):
"Initialize attributes"
#objects for obstacles
self.collectObjects = []
#number of vehicles
self.vNumber = 0
#boolean for obstacles
self.obst = 0
#boolean for global forces
self.gforces = 0
#dictionary for UI elements
self.UIelements = {}
#set timeline
cmds.playbackOptions(min= 1, max=1000)
#get selected objects
self.collectObjects = cmds.ls(sl=1)
#start UI
self.crowdSystemUI()

def crowdSystemUI(self):
"Assembles and displays the UI for the Crowd System"
self.UIelements["window"] = cmds.window(title="Crowd System",
widthHeight=(300, 200)
)

self.UIelements["form"] = cmds.formLayout()
txt = cmds.text(label="Number of Vehicles")
collection = cmds.radioCollection()
radiob1 = cmds.radioButton(label="10", data=1, changeCommand=self.changeVnumber)
radiob2 = cmds.radioButton(label="20", onCommand="vNumber = 20")
cmds.setParent(upLevel=1)
cmds.setParent(upLevel=1)

txtOB = cmds.text(label="Obstacles")
collection2 = cmds.radioCollection()
radiob3 = cmds.radioButton(label="On", changeCommand=self.changeObst)
radiob4 = cmds.radioButton(label="Off")
cmds.setParent(upLevel=1)
cmds.setParent(upLevel=1)

txtGF = cmds.text( label = "Global Forces")
collection3 = cmds.radioCollection()
radiob5 = cmds.radioButton(label= "On" ,changeCommand =self.changeGforces)
radiob6 = cmds.radioButton(label="Off")
cmds.setParent(upLevel=1)
cmds.setParent(upLevel=1)

cmds.radioCollection(collection, edit=1, select=radiob1)
cmds.radioCollection(collection2, edit=1, select=radiob4)
cmds.radioCollection(collection3, edit=1, select=radiob6)

# Place Vehicle options
form = self.UIelements["form"]
cmds.formLayout(form,
edit=1,
attachForm= [
( txt, "top", 20),
(txt ,"left", 70),
(radiob1,"top", 10),
(radiob1,"left", 20),
(radiob2,"top", 30),
(radiob2,"left", 20)
]
)
# Place environment options
cmds.formLayout(form,
edit=1,
attachForm= [
(txtOB, "top", 80),
(txtOB, "left", 80),
(radiob3, "top", 80),
(radiob3, "left", 150),
(radiob4 ,"top", 80),
(radiob4, "left", 190),
(txtGF, "top", 110),
(txtGF, "left", 63),
(radiob5, "top", 110),
(radiob5, "left", 150),
(radiob6, "top", 110),
(radiob6, "left", 190)
]
)
# Create buttons
button1 = cmds.button(label = "Create")
button2 = cmds.button(label = "Cancel")

# Place buttons in the window
cmds.formLayout(form,
edit=1,
attachForm =[
(button1, "bottom", 10),
(button1, "left", 185),
(button2, "bottom", 10),
(button2, "right", 10)
]
)
# Add the commands to the buttons.
cmds.button(button1, edit=1, command=self.createCrowd )
cmds.button(button2, edit=1, command=self.deleteUI )

cmds.showWindow(self.UIelements["window"])
## UI help functions
def changeVnumber(self, *args):
if args[0] == "true":
self.vNumber = 10
else:
self.vNumber = 20
print "vNumber:",self.vNumber
def changeObst(self, *args):
if args[0] == "true":
self.obst = 1
else:
self.obst = 0
print "Obst:", self.obst
def changeGforces(self, *args):
if args[0] == "true":
self.gforces = 1
else:
self.gforces = 0
print "gforces:", self.gforces
def deleteUI(self, *args):
cmds.deleteUI(self.UIelements["window"])
self.UIelements["window"] = ""

## MAIN METHODS
def createCrowdSolver(self):
"Creates the main rigidSolver for the elements in the Crowd System"
self.crowdSolver = cmds.rigidSolver( create=1,
current=1,
name="crowdSolver",
velocityVectorScale=0.5,
displayVelocity=1
)
cmds.setAttr(self.crowdSolver + ".allowDisconnection", 1)

def createVehicle(self, vType, N):
"Creates one vehicle and add fields and expressions to it."
#first verify if the vehicle is of type follower (F) or leader (L)
#and set the size of the cube according to the type (leaders are bigger! :)
if vType == "P1":
w = 70 #width
h = 70#height
d = 70 #depth
if vType == "P2":
w = 100 #width
h = 100 #height
d = 100 #depth


if vType == "F1":
w = 2 #width
h = 2 #height
d = 2 #depth
if vType == "F2":
w = 2 #width
h = 4 #height
d = 2 #depth
if vType == "F3":
w = 2 #width
h = 6 #height
d = 2 #depth

if vType == "L1":
w = 5 #width
h = 3 #height
d = 5 #depth
if vType == "L2":
w = 5 #width
h = 5 #height
d = 5 #depth
else:
w = 5
h = 7
d = 5

#creates the basic cube
vehicle = cmds.polyCube(w=w,d=d,h=h)

#creates the pavilion
pavilion1 = cmds.polyCube(w=w,d=d,h=h)
pavilion2 = cmds.polyCube(w=w,d=d,h=h)

#create a vehicule force to the agent - force 1
field1=cmds.radial(position=[0,0,0],
magnitude=50,
attenuation = 0.3,
maxDistance = 8.0
)

cmds.parent(field1[0], vehicle[0])
cmds.hide(field1)

if vType=="L1":
Lfield1 = cmds.radial(position=[0,0,0],
magnitude=-1,
attenuation=.2,
maxDistance=50
)

cmds.parent(Lfield1[0], vehicle[0])
cmds.hide(Lfield1)

#create a vehicule force to the agent - force 2
field2=cmds.radial(position=[0,0,0],
magnitude=50,
attenuation = 0.3,
maxDistance = 8.0
)

cmds.parent(field2[0], vehicle[0])
cmds.hide(field2)

if vType=="L2":
Lfield2 = cmds.radial(position=[0,0,0],
magnitude=-1,
attenuation=.2,
maxDistance=50
)

cmds.parent(Lfield2[0], vehicle[0])
cmds.hide(Lfield2)

#create a vehicule force to the agent - force 3
field3=cmds.radial(position=[0,0,0],
magnitude=50,
attenuation = 0.3,
maxDistance = 8.0
)

cmds.parent(field3[0], vehicle[0])
cmds.hide(field3)

if vType=="L3":
Lfield3 = cmds.radial(position=[0,0,0],
magnitude=-1,
attenuation=.2,
maxDistance=50
)

cmds.parent(Lfield3[0], vehicle[0])
cmds.hide(Lfield3)

#create a static force to the agent - force 4
field4=cmds.radial(position=[0,0,0],
magnitude=50,
attenuation = 0.3,
maxDistance = 8.0
)

cmds.parent(field4[0], pavilion1[0])
cmds.hide(field4)

if vType=="P1":
Lfield4 = cmds.radial(position=[0,0,0],
magnitude=-1,
attenuation=.2,
maxDistance=70
)

cmds.parent(Lfield4[0], pavilion1[0])
cmds.hide(Lfield4)

#create a static force to the agent - force 5
field5=cmds.radial(position=[0,0,0],
magnitude=50,
attenuation = 0.3,
maxDistance = 8.0
)

cmds.parent(field5[0], pavilion2[0])
cmds.hide(field5)

if vType=="P2":
Lfield5 = cmds.radial(position=[0,0,0],
magnitude=-1,
attenuation=.2,
maxDistance=100
)

cmds.parent(Lfield5[0], pavilion2[0])
cmds.hide(Lfield5)


#convert vehicle to a rigid body with random placement
rigid = cmds.rigidBody(
vehicle[0],
n="rigidVeh1%s%d" %(vType, N),
active=1,
mass=1,
bounciness=0,
damping=1.5,
position=(uniform(-70,70),uniform(-70,70),0),
impulse=(0,0,0),
standInObject="cube",
solver=self.crowdSolver
)

#convert pavillion1 to a passive rigid body with random placement
rigid = cmds.rigidBody(
pavilion1[0],
n="rigidVeh1%s%d" %(vType, N),
active=0,
mass=1,
bounciness=0,
damping=1.5,
position=(uniform(-70,70),uniform(-70,70),0),
impulse=(0,0,0),
standInObject="cube",
solver=self.crowdSolver
)

#convert pavillion2 to a passive rigid body with random placement
rigid = cmds.rigidBody(
pavilion2[0],
n="rigidVeh1%s%d" %(vType, N),
active=0,
mass=1,
bounciness=0,
damping=1.5,
position=(uniform(-70,70),uniform(-70,70),0),
impulse=(0,0,0),
standInObject="cube",
solver=self.crowdSolver
)

#disconnect the rotation attributes of the vehicle
cmds.disconnectAttr(rigid + "rx.output", vehicle[0]+".rx")
cmds.disconnectAttr(rigid + "ry.output", vehicle[0]+".ry")
cmds.disconnectAttr(rigid + "rz.output", vehicle[0]+".rz")

#add expression for movement
randX = uniform(-3,3)
randY = uniform(-3,3)

expString = "%s.impulseX = sin(time * %f);" % (rigid, randX)
expString += "%s.impulseY = (noise(time)*%f);" % (rigid, randY)
expString += "float $Vel[] = `getAttr %s.velocity`;" % rigid
expString += "%s.rotateX = 0;" % vehicle[0]
expString += "%s.rotateY = 0;" % vehicle[0]
expString += "%s.rotateZ = atan2d( $Vel[0], $Vel[2] );" % vehicle[0]

cmds.expression(s=expString)

if vType == "F1":
return [rigid, field] #returns the name of the function

else:
return[rigid, field, Lfield1]

if vType == "F2":
return [rigid, field] #returns the name of the function

else:
return[rigid, field, Lfield2]

if vType == "F3":
return [rigid, field] #returns the name of the function

else:
return[rigid, field, Lfield3]

def createCrowd(self, *args):
"Method to assemble the whole system"
#creates a rigidSolver to add the vehicle to
self.createCrowdSolver()
#turn on the velocity arrow
cmds.setAttr(self.crowdSolver + ".displayVelocity", 1)
cmds.setAttr(self.crowdSolver + ".scaleVelocity", .5)

#allow disconnections on the rigidSolver
cmds.setAttr(self.crowdSolver + ".allowDisconnection", 1)

###create amount of agents
if self.vNumber == 10:
FNumber = 8
LNumber = 2
elif self.vNumber == 20:
FNumber=18
Lnumber=2


#create empty lists to store followers and leaders
followers1=[]
leaders1=[]
followers2=[]
leaders2=[]
followers3=[]
leaders3=[]
pavilions1=[]
pavilions2=[]

#create the followers
for i in range(FNumber):
if i%3 == 0:
v=self.createVehicle("F1",i)
followers1.append(v)

if i%3 == 1:
v=self.createVehicle("F2",i)
followers2.append(v)

if i%3 == 2:
v=self.createVehicle("F3",i)
followers3.append(v)

#create the leaders
for i in range(LNumber):
if i%3 == 0:
v=self.createVehicle("L1",i)
leaders1.append(v)

if i%3 == 1:
v=self.createVehicle("L2",i)
leaders2.append(v)

if i%3 == 2:
v=self.createVehicle("L3",i)
leaders3.append(v)

#create static attractor
for i in range(2):
if i%2 == 0:
v=self.createVehicle("P1",i)
pavilions1.append(v)

if i%2 == 1:
v=self.createVehicle("P2",i)
pavilions2.append(v)



cmds.polyCube( sx=10, sy=15, sz=5, h=20 )
#result is a 20 units height rectangular box
#with 10 subdivisions along X, 15 along Y and 20 along Z.





#CONNECT THE FIELDS TO THE RIGID BODIES(AGENTS)
#1) Connect the leaders with the followers 1-1

for i in range(LNumber):
if i%3 == 0:
Lfield1=leaders1[i][2]
lfield1=leaders1[i][1]
for j in range (FNumber):
if j%3 == 0:
Frigid1 = followers1[j][0]
#connect the fields
cmds.connectDynamic(Frigid1, fields=Lfields)
cmds.connectDynamic(Frigid1, fields=lfields)

if i%3 == 1:
Lfield2=leaders2[i][2]
lfield2=leaders2[i][1]
for j in range (FNumber):
if j%3 == 1:
Frigid1 = followers2[j][0]
#connect the fields
cmds.connectDynamic(Frigid2, fields=Lfields)
cmds.connectDynamic(Frigid2, fields=lfields)

else:
Lfield3=leaders3[i][2]
lfield3=leaders3[i][1]
for j in range (FNumber):
if j%3 == 2:
Frigid3 = followers3[j][0]
#connect the fields
cmds.connectDynamic(Frigid3, fields=Lfields)
cmds.connectDynamic(Frigid3, fields=lfields)




#2) Connect followers to leaders
for i in range(FNumber):
if i%3 == 0:
Ffield1 = followers1[i][1]
for j in range(LNumber):
if j%3 == 0:
Lrigid1 = leaders1[j][0]
cmds.connectDynamic(Lrigid1, fields=Ffield)


if i%3 == 1:
Ffield2 = followers2[i][1]
for j in range(LNumber):
if j%3 == 1:
Lrigid2 = leaders2[j][0]
cmds.connectDynamic(Lrigid2, fields=Ffield)

else:
Ffield3 = followers3[i][1]
for j in range(LNumber):
if j%3 == 2:
Lrigid3 = leaders3[j][0]
cmds.connectDynamic(Lrigid3, fields=Ffield)




#3) Connect leaders to leaders
for i in range (LNumber):
if i%3 == 0:
Lrigid1 = leaders1[i][0]
Lfield1 = leaders1[i][1]
for j in range(LNumber):
if i == f: continue
if j%3 == 0:
L1rigid = leaders1[j][0]
#l1field = leaders1[j][1]
cmds.connectDynamic(L1rigid, fields=Lfield)

if i%3 == 1:
Lrigid2 = leaders2[i][0]
Lfield2 = leaders2[i][1]
for j in range(LNumber):
if i == f: continue
if j%3 == 1:
L1rigid = leaders2[j][0]
#l1field = leaders1[j][1]
cmds.connectDynamic(L1rigid, fields=Lfield)

if i%3 == 2:
Lrigid3 = leaders3[i][0]
Lfield3 = leaders3[i][1]
for j in range(LNumber):
if i == f: continue
if j%3 == 2:
L1rigid = leaders3[j][0]
#l1field = leaders1[j][1]
cmds.connectDynamic(L1rigid, fields=Lfield)

#4) Connect followers to followers
for i in range(FNumber):
if i%3 == 0:
Ffield1=followers1[i][1]
for j in range(FNumber):
if i==j: continue
if j%3 == 0:
Frigid1 = followers1[j][0]

cmds.connectDynamics(Frigid1, fields=Ffield)
if i%3 == 1:
Ffield2=followers2[i][1]
for j in range(FNumber):
if i==j: continue
if j%3 == 1:
Frigid2 = followers2[j][0]

cmds.connectDynamics(Frigid2, fields=Ffield)
else:
Ffield3=followers3[i][1]
for j in range(FNumber):
if i==j: continue
if j%3 == 2:
Frigid3 = followers3[j][0]

cmds.connectDynamics(Frigid3, fields=Ffield)





#create obstacles if i want
if self.obst:self.collectObstacles()




#disable solver warnings
cmds.cycleCheck(e=0)

#disable solver warnings
cmds.cycleCheck(e=0)

def collectObstacles(self):
"Collects all obstacles"
if len(self.collectObjects) == 0:
#there is nothing selected
return "There is nothing selected!"

for o in self.collectObjects:
cmds.rigidBody( o,
passive = 1,
name=o+"_rb",
bounciness=2.0)

#craete an instance for this class
c=Crowd()

No comments:

Post a Comment