Last active
August 29, 2015 14:01
-
-
Save Gi133/3d33afc4af783cae1ce7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import maya.cmds as cmds | |
import maya.mel as mel | |
### Expression : python("Run("+frame+")"); ### | |
# GLOBAL VARIABLES | |
last_frame_number = 1 | |
framerate = 0 | |
global initialized | |
initialized = False | |
## TIMINGS ## | |
### Set Frame to "auto" if you want it to be automatically calculated based on framerate and time. ### | |
### For the deactivation times, set to -2 if they should never be deactivated and "auto" to automaticall calculate frames, as above. ### | |
triggerActivationTime = 1 # in seconds | |
triggerActivationFrame = "auto" | |
triggerDeActivationTime = -2 | |
triggerDeActivationFrame = -2 | |
exitActivationTime = 3 # in seconds | |
exitActivationFrame = "auto" | |
exitDeActivationTime = -2 | |
exitDeActivationFrame = -2 | |
## END OF TIMINGS ## | |
agentName = "Agent" | |
agentType = "transform" | |
global agentList | |
agentList = None | |
triggerName = "Trigger" | |
triggerType = "transform" | |
triggerRadialFieldName = "RadialField" | |
triggerList = None | |
global triggerRadialFieldList | |
### TRIGGER SETTINGS ### | |
triggerScareValue = 0.3 # magnitude | |
triggerScareFactor = 0.2 # attenuation | |
triggerScareRadius = 15 # max distance | |
exitName = "Exit" | |
exitType = "transform" | |
exitRadialFieldName = "RadialField" | |
exitList = None | |
global exitRadialFieldList | |
### EXIT SETTINGS ### | |
exitSafetyValue = 0.2 # magnitude (get's a negative sign attached to it later so that it pulls the agents towards it) | |
exitSafetyFactor = 0.5 # attenuation | |
exitSafetyVisibility = -1 # max distance (set to -1 to have all agents know about the exit) | |
# this function is used to initialise the simulation | |
def Init(): | |
print ("Custom simulation initialised") | |
# reset last frame counter | |
global last_frame_number,triggerActivationFrame, exitActivationFrame, triggerDeActivationFrame, exitDeActivationFrame, frame_rate | |
last_frame_number = 1 | |
AgentSetup() | |
TriggerSetup() | |
ExitSetup() | |
# Turn activation timers into frame numbers. | |
if triggerActivationFrame == "auto": | |
print("Evaluating triggerActivationFrame!!") | |
print("Trigger Activation Time: " + str(triggerActivationTime)) | |
print("Framerate: " + str(frame_rate)) | |
triggerActivationFrame = triggerActivationTime * frame_rate | |
print("New Trigger Activation Frame: " + str(triggerActivationFrame)) | |
if exitActivationFrame == "auto": | |
exitActivationFrame = exitActivationTime * frame_rate | |
# Turn deactivation timers into frame numbers where applicable. | |
if triggerDeActivationFrame == "auto": | |
triggerDeActivationFrame = triggerDeActivationTime * frame_rate | |
if exitDeActivationFrame == "auto": | |
exitDeActivationTime * frame_rate | |
global exitRadialFieldList, triggerRadialFieldList | |
# Create a list of all radial fields | |
exitRadialFieldList = cmds.ls(exitName + "*" + exitRadialFieldName) | |
triggerRadialFieldList = cmds.ls(triggerName + "*" + triggerRadialFieldName) | |
# this function is called every frame the simulation is run | |
def Run(frame_number): | |
# get the frame rate by using an internal MEL script | |
global frame_rate | |
frame_rate = mel.eval("currentTimeUnitToFPS") | |
# calculate the amount of time in seconds between each frame | |
frame_time = 1.0 / frame_rate | |
# If initialization has not happened yet and the simulation is on the first frame then initialize | |
global initialized | |
if initialized == False: | |
if frame_number == 1: | |
Init() | |
initialized = True | |
# check to see if we have an event to process this frame | |
global last_frame_number | |
if (frame_number - last_frame_number) == 1: | |
print("Trigger Activation Frame: " + str(triggerActivationFrame)) | |
print("Exit Activation Frame: " + str(exitActivationFrame)) | |
print("Trigger DeActivation Frame: " + str(triggerDeActivationFrame)) | |
print("Exit DeActivation Frame: " + str(exitDeActivationFrame)) | |
# Check the timers and activate any fields that need activating. | |
if triggerActivationFrame == frame_number: | |
ActivateTrigger() | |
print("Activating Trigger!!") | |
if exitActivationFrame == frame_number: | |
ActivateExit() | |
print("Activating Exit!!") | |
if triggerDeActivationFrame == frame_number: | |
DeActivateTrigger() | |
print("DeActivating Trigger!!") | |
if exitDeActivationFrame == frame_number: | |
DeActivateExit() | |
print("DeActivating Exit!!") | |
print ("Custom simulation run successfully at frame: "+str(frame_number)) | |
# update the last frame number | |
last_frame_number = frame_number | |
# ADD ANY OF YOUR OWN SCRIPT FUNCTIONS HERE | |
def AgentSetup(): | |
global agentList | |
agentList = cmds.ls(agentName + "*", type = agentType) | |
if len(agentList) != 0: | |
# Set the agents up. | |
for agent in agentList: | |
if len(cmds.ls(agent + "RigidBody", type = "rigidBody")) == 0: | |
cmds.select(agent) | |
cmds.rigidBody(n = agentName+"RigidBody", act = True, b = 0.1) # would be nice if i had the slightest idea how to query for this. | |
else: | |
print("ERROR INITIALIZING AGENT/S, SIZE MISMATCH.") | |
# End of Function | |
def TriggerSetup(): | |
triggerList = cmds.ls(triggerName + "*", type = triggerType) | |
if len(triggerList) > 0: | |
# Setup the trigger/s | |
for trigger in triggerList: | |
if len(cmds.ls(trigger + triggerRadialFieldName, type = "radialField")) == 0: | |
cmds.select(trigger) | |
cmds.radial(trigger, n = trigger + triggerRadialFieldName, magnitude = triggerScareValue, attenuation = triggerScareFactor, maxDistance = triggerScareRadius) | |
else: | |
print("ERROR INITIALIZING TRIGGER/S, SIZE MISMATCH.") | |
# End of Function | |
def ExitSetup(): | |
exitList = cmds.ls(exitName + "*", type = exitType) | |
if len(exitList) > 0: | |
# Setup the exit/s | |
for exit in exitList: | |
if len(cmds.ls(exit + exitRadialFieldName, type = "radialField")) == 0: | |
cmds.select(exit) | |
cmds.radial(exit, n = exit + exitRadialFieldName, magnitude = -exitSafetyValue, attenuation = exitSafetyFactor, maxDistance = exitSafetyVisibility) | |
else: | |
print("ERROR INITIALIZING EXIT/S, SIZE MISMATCH.") | |
# End of Function | |
def HookFields(): | |
global exitRadialFieldList, triggerRadialFieldList, agentList | |
# Hook up the trigger/s and exit/s to the agents. | |
for agent in agentList: | |
agent = cmds.connectDynamic(agent, fields = exitRadialFieldList) # Hook up all agents to all the fields in the combinedList | |
agent = cmds.connectDynamic(agent, fields = triggerRadialFieldList) | |
# End of Function | |
def ActivateTrigger(): | |
global triggerRadialFieldList, agentList | |
for agent in agentList: | |
cmds.select(agent) | |
agent = cmds.connectDynamic(agent, fields = triggerRadialFieldList) | |
# End of function | |
def ActivateExit(): | |
global exitRadialFieldList, agentList | |
for agent in agentList: | |
agent = cmds.connectDynamic(agent, fields = exitRadialFieldList) | |
# End of Function | |
def DeActivateTrigger(): | |
global triggerRadialFieldList, agentList | |
for agent in agentList: | |
cmds.connectDynamic(agent, delete = True, fields = triggerRadialFieldList) | |
# End of Function | |
def DeActivateExit(): | |
global exitRadialFieldList, agentList | |
for agent in agentList: | |
cmds.connectDynamic(agent, delete = True, fields = exitRadialFieldList) | |
# End of Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment