It doesn't always consider the obstacles as it should. Sometimes the objects are rejected (although they should be deleted) and sometimes they just ignore the obstacles.
#######################################################################################################
##
## Crowd System
## student Andreea Nica
##
## Active agents: predacious & small fishes
## Passive agents: edge & fishing line
##
## Rules:
## - fishes meet the edge - delete fishes
## - fishes meet the fishing line - delete fishes
## - small fish meets predacious - delete small fish
## - small fish meets small fish - scale 0.5
## - predacious meets predacious - duplicate predacious
##
#######################################################################################################
import maya.cmds as cmds
from random import *
def hit(hit, agent):
### define what to do when it some collision is detected
#all transformations are made only on the agent, as the hit object will perform the same queries on this agent
#if it hit the cube, ignore
if hit == "wall_rigid":
return
agentRole = cmds.getAttr(agent + ".role")
hitRole = cmds.getAttr(hit + ".role")
if hitRole == agentRole:
#check if both are bad
if hitRole == "bad":
#then delete - edge rule
cmds.delete('agent')
else:
#if good, increase size
cmds.delete( 'agent')
elif hitRole != agentRole:
if hitRole == "bad":
#means agent role is good (small fish)
cmds.scale(.5,.5,.5, agent, r=1)
elif hitRole == "good":
#means agent was bad (predacious - duplicate)
cmds.duplicate( 'agent')
class Agent:
def __init__(self, solver, role, position=(0,0,0), size=(3,3,3), color=(0,0,0), objType="cube", rigidType="active", bounciness=.6, collide=1):
#set agent atributes
self.bounciness = bounciness
self.collide = collide
self.rigidType = rigidType
self.solver=solver
self.role = role
self.initialScale = size
#create the agent and scale it
self.object = cmds.polyCube(ax=(0,0,1))
cmds.scale(size[0], size[1], size[2])
#apply a color to the agent
#set the object color
#sh = createShader(color, transparency=0, type="lambert")
#applyShaderOnObject(self.object[0], sh)
#create the rigid body
self.rigid = cmds.rigidBody(self.object,
name=self.object[0] + "_rigid",
p=position,
b=self.bounciness,
impulse=(0,0,0),
sio=objType, #stand in object
cc = 1, #contact count
cp = 1, #contact position
cn = 1, #contact name
solver=self.solver)
#add attribute to the rigid body to store the type of agent
cmds.select(self.rigid, r=1)
cmds.addAttr(ln="role", dt="string", keyable=1)
cmds.setAttr(self.rigid + ".role", "good", type="string")
if self.rigidType == "active":
cmds.setAttr(self.rigid + ".active", 1)
else:
cmds.setAttr(self.rigid + ".active", 0)
#apply the expression
self.applyExpression()
def applyExpression(self):
"Function to apply the expression to the agent"
#first disconnect the rotation attributes because we want to controll it via expressions
cmds.disconnectAttr (self.rigid + "ry.output", self.object[0] + ".rotateY")
cmds.disconnectAttr (self.rigid + "rx.output", self.object[0] + ".rotateX")
cmds.disconnectAttr (self.rigid + "rz.output", self.object[0] + ".rotateZ")
# Create expression for VehicleF with a random multiplier added to the expression.
randX = random.uniform(-3,3)
randY = random.uniform(3,-3)
####COMPOSE THE EXPRESSION STRING
#now put the text above in a python string
#the first lines define how the agent will wander
expString = "// Set wander motion of vehicle and add random multipliers.\n"
expString += "%s.impulseX" % self.rigid
expString += " = sin(time * %f);\n" % randX
expString += "%s.impulseY" % self.rigid
expString += " = (noise(time) * %f);\n\n" % randY
#the second part, how it rotates according to the direction it is going (velocity vector)
expString += "// Set orientation of Vehicle according to the velocity direction.\n"
expString += "float $fVel[];\n"
expString += "$fVel = `getAttr %s.velocity`;\n\n" % (self.rigid)
expString += "%s.rotateX = 0;\n" % self.object[0]
expString += "%s.rotateZ = atan2d( $fVel[0], $fVel[1] );\n" % (self.object[0])
expString += "%s.rotateY = 0;\n\n" % self.object[0]
#the third part, checking bumps on other agents
expString += "//checking bumps on other agents\n"
expString += "string $lastHit;\n"
expString += "if (%s.contactCount > 0){\n" % self.rigid
expString += " int $contactCount = `getAttr %s.contactCount`;\n" % self.rigid
expString += " $lastHit = `getAttr %s.contactName[0]`;\n" %self.rigid
expString += ' string $rigid = "%s";\n' % self.rigid
expString += ' python("hit(\'"+$lastHit+"\', \'"+ $rigid +"\')");\n'
expString += "};//endif\n"
self.expString = expString
cmds.expression(s=expString)
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 = 1 #width
h = .5 #height
d = 1 #depth
else:
w = 5
h = 3
d = 5
#creates the basic cube
vehicle = cmds.polyCube(w=w,d=d,h=h)
#create a vehicle force to the agent
field = cmds.radial(position = [0,0,0],
magnitude = 50, #positive magnitude means repoulsion, negative magnitude means atraction
attenuation = 0.3,
maxDistance = 8.0
)
cmds.parent(field[0],vehicle[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
)
#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]
else:
return [rigid, field, Lfield]
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: #vNumber = number of vehicles
FNumber = 8 #followers
LNumber = 2 #leaders
elif self.vNumber ==20:
FNumber =18
LNumber =2
#create empty lists to store followers and leaders
followers = []
leaders = []
#creating the followrs
for i in range(FNumber):
v = self.createVehicle("F",i)
followers.append(v)
#creating the leaders
for i in range(LNumber):
v = self.createVehicle("L",i)
leaders.append(v)
#CONNECT THE FIELDS TO THE RIGID BODIES (AGENTS)
#1) connect the leaders (predacious fishes) to the followers (small fishes)
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=Lfield)
cmds.connectDynamic(Frigid, fields=lfield)
#2) connect followers (small fishes) to leaders (predacious fishes)
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 (predacious fishes) to leaders ((predacious fishes))
for i in range(LNumber):
Lfield = leaders[i][1]
for j in range(LNumber):
if i==j: continue
L1rigid = leaders[j][0]
cmds.connectDynamic(L1rigid, fields=Lfield)
#4) connect followers (small fishes) to followes (small fishes)
for i in range(FNumber):
Ffield = followers[i][1]
for j in range(FNumber):
if i==j: continue
F1rigid = followers[j][0]
cmds.connectDynamic(F1rigid, fields=Ffield)
#create obstacles if I want
if self.obst: self.collectObstacles()
#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 )
### create an instance of this class
c = Crowd()
Friday, August 7, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment