Tuesday, July 21, 2009

Assignment 03B - first try

In this script we tried to add pavilions(by script) and obstacles(manually in maya). It seams to be something wrong when we define the pavilions. Pavilions are static rigid bodies. The only difference between vehicles and pavilions is the varriable active, which is 0 for pavilions. It is a wrong way to define pavilions like that?


#######################################################################################################
##
## CROWD SYSTEM
## Adapted from original idea by Mark R. Wilkins and Chris Kazmier
##
## Modified: 22.06.09 by Daniel da Rocha
##
## Last Modified: 21.07.09 by Alexandra Virlan and Mircea Mogan
##
#######################################################################################################


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 == "F":
w = 2 #width
h = 2.5 #height
d = 2 #depth

if vType == "P":
w = 100 #width
h = 100 #height
d = 100 #depth

else:
w = 5
h = 3.5
d = 5

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

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

cmds.parent(field[0], vehicle[0])
cmds.parent(field[0], pavilion[0])
cmds.hide(field)

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

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

#convert it 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
)

rigid = cmds.rigidBody(
pavilion[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 == "F":
return [rigid, field] #returns the name of the function

if vType == "L":
return[rigid, field, Lfield]

else:
return[rigid, field, Pfield]

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
followers=[]
leaders=[]
pavilions=[]
#create the followers
for i in range(FNumber):
v=self.createVehicle("F",i)
followers.append(v)

#create the leaders
for i in range(LNumber):
v=self.createVehicle("L",i)
leaders.append(v)

create pavilion
for i in range(1):
v=self.createVehicle("P",i)
pavilions.append(v)



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

for i in range(LNumber):
Lfield=leaders[i][2]
lfield=leaders[i][1]
for j in range (FNumber):
Frigid = followers[j][0]
#connect the fields
cmds.connectDynamic(Frigid, fields=Lfields)
cmds.connectDynamic(Frigid, fields=lfields)


#2) Connect followers to leaders
for i in range(FNumber):
Ffield = followers[i][1]
for j in range(LNumber):
Lrigid = leaders[j][0]
cmds.connectDynamic(Lrigid, fields=Ffield)

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

#4) Connect followers to followers
for i in range(FNumber):
Ffield=followers[i][1]
for j in range(FNumber):
if i==j: continue
Frigid = followers[j][0]

cmds.connectDynamics(Frigid, fields=Ffield)

#5) Connect the pavilion with the leaders

for i in range(1):
Pfield=pavilions[i][2]
Lfield=pavilions[i][1]

for j in range (LNumber):
Frigid = leaders[j][0]
#connect the fields
cmds.connectDynamic(Frigid, fields=Pfields)
cmds.connectDynamic(Frigid, fields=Lfields)


#6) Connect leaders to pavilions
for i in range(LNumber):
Lfield = leaders[i][1]
for j in range(1):
Frigid = pavilions[j][0]
cmds.connectDynamic(Prigid, fields=Lfield)

#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