Below is my final code I created using Python, environment created in Photoshop and Maya:
import maya.cmds as cmds
import random as rand
import math
def populationDefine(): #user input sets parameters for creation of population
global p1, p2, p3
print "Before proceeding, define the following parameters regarding your population: "
#determine population size
p1 = raw_input('Is the population large, medium, or small? ')
print p1, "\n"
if p1 == 'large': #assigns value to global variable based on user input
p1 = 0
elif p1 == 'medium':
p1 = 1
elif p1 == 'small':
p1 = 2
else:
print "Incorrect input, please try again."
main()
#determine population type
p2 = raw_input('Is the population of type predator or prey? ')
print p2, "\n"
if p2 == 'predator':
p2 = 0
elif p2 == 'prey':
p2 = 1
else:
print "Incorrect input, please try again."
main()
#determine population speed
p3 = raw_input('Is the population fast or slow? ')
print p3, "\n"
if p3 == 'fast':
p3 = 0
elif p3 == 'slow':
p3 = 1
else:
print "Incorrect input, please try again."
main()
def objectSize(curObj): #determines poly size based on population size
if p1 == 0:
cmds.scale(1.5, 1.5, 1.5, curObj)
elif p1 == 1:
cmds.scale(1, 1, 1, curObj)
elif p1 == 2:
cmds.scale(.5, .5, .5, curObj)
def objectColor(curObj): #could not get this to work, supposed to assign color
if p2 == 0:
cmds.connectAttr(curObj+'.objectColor', 'matRed')
elif p2 == 1:
cmds.connectAttr(curObj+'.objectColor', 'matBlue')
def populationPos(): #places population randomly in environment
x = rand.randrange(1, 19, 1)
z = rand.randrange(1, 19, 1)
curObj = cmds.polySphere(name='population1', radius=.5, subdivisionsX=10, subdivisionsY=10, axis=(0, 1, 0), createUVs=2, constructionHistory=True)
curObj = curObj[0]
objectSize(curObj)
#objectColor(curObj)
print "Name = ", curObj, "\n"
cmds.addAttr(ln = 'size', at = 'enum', en = "large:medium:small")
cmds.setAttr(curObj+'.size', p1)
cmds.addAttr(ln = 'type', at = 'enum', en = "predator:prey")
cmds.setAttr(curObj+'.type', p2)
cmds.addAttr(ln = 'speed', at = 'enum', en = "fast:slow")
cmds.setAttr(curObj+'.speed', p3)
color = cmds.getAttr( "lambert1.color" )
cmds.setAttr( "lambert1.color", color[0][0], color[0][1], color[0][2], type="double3" )
cmds.setAttr(curObj+".translate", x, 0, z)
return curObj
def userInput(curObj): #user input moves object in direction of choice
q = raw_input('In which direction should the population move? N, S, E, or W? To create a new population, type "new". If you are done, type "exit". ')
print q, "\n"
cmds.select(curObj)
if q == 'n' or q == 'N':
if p3 == 0:
cmds.move(2, 0, 0, relative=True)
elif p3 == 1:
cmds.move(1, 0, 0, relative=True)
elif q == 's' or q == 'S':
if p3 == 0:
cmds.move(-2, 0, 0, relative=True)
elif p3 == 1:
cmds.move(-1, 0, 0, relative=True)
elif q == 'e' or q == 'E':
if p3 == 0:
cmds.move(0, 0, 2, relative=True)
elif p3 == 1:
cmds.move(0, 0, 1, relative=True)
elif q == 'w' or q =='W':
if p3 == 0:
cmds.move(0, 0, -2, relative=True)
elif p3 == 1:
cmds.move(0, 0, -1, relative=True)
elif q == 'new':
main()
elif q == 'exit':
print 'OK \n'
return q
def populationDistance(i, j): #checks the distance between 2 populations
global dist
ix = cmds.getAttr(i+'.translateX')
jx = cmds.getAttr(j+'.translateX')
iz = cmds.getAttr(i+'.translateZ')
jz = cmds.getAttr(j+'.translateZ')
dist = math.sqrt( pow(ix-jx,2) + pow(iz-jz,2) )
def populationReact(i, j): #if populations are close together, they will interact
if cmds.getAttr(i+'.type') == 0 and cmds.getAttr(j+'.type') == 1:
if cmds.getAttr(i+'.size') == 0 and cmds.getAttr(j+'.size') == 0:
print "These two populations can coexist!"
if cmds.getAttr(i+'.size') == 1 and cmds.getAttr(j+'.size') == 0:
print "These two populations can coexist!"
if cmds.getAttr(i+'.size') == 2 and cmds.getAttr(j+'.size') == 0:
print "These two populations can coexist!"
if cmds.getAttr(i+'.size') == 2 and cmds.getAttr(j+'.size') == 1:
print "These two populations can coexist!"
else:
cmds.delete(j)
print "These two populations could not coexist!\n\n"
main()
if cmds.getAttr(j+'.type') == 0 and cmds.getAttr(i+'.type') == 1:
if cmds.getAttr(j+'.size') == 0 and cmds.getAttr(i+'.size') == 0:
print "These two populations can coexist!"
if cmds.getAttr(j+'.size') == 1 and cmds.getAttr(i+'.size') == 0:
print "These two populations can coexist!"
if cmds.getAttr(j+'.size') == 2 and cmds.getAttr(i+'.size') == 0:
print "These two populations can coexist!"
if cmds.getAttr(j+'.size') == 2 and cmds.getAttr(i+'.size') == 1:
print "These two populations can coexist!"
else:
cmds.delete(i)
print "These two populations could not coexist!\n\n"
main()
def populationDetect(): #detects populations near current controlled object
cmds.select('population*')
cmds.select('pop*Shape', d=True)
allObj = cmds.ls(sl = True)
cmds.select(d=True)
for i in range(len(allObj)):
temp = allObj[i+1:]
for j in range(len(temp)):
populationDistance(allObj[i], allObj[i+1+j]) #populationDistance called
if dist <= 2:
populationReact(allObj[i], allObj[i+1+j]) #populationReact called
def main(): #Main function
populationDefine()
curObj = populationPos()
popX = cmds.getAttr(curObj+'.translateX')
popZ = cmds.getAttr(curObj+'.translateZ')
q = ''
while popX < 20 and popX > 0 and popZ < 20 and popZ > 0 and q != 'exit' and cmds.objExists(curObj): #user is prompted until one of these things happens
q = userInput(curObj)
populationDetect()
popX = cmds.getAttr(curObj+'.translateX')
popZ = cmds.getAttr(curObj+'.translateZ')

No comments:
Post a Comment