Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- FlyBehaviorTest
- -- scripted plugin for TP 4.0
- -- v0.1
- --username@email.com
- -- WHAT IT DOES:
- tp_unregister FlyBehaviorTest --unregister before you make changes in the plugin, you can test the code on tp runtime
- plugin Geometry FlyBehaviorTest
- name:"FlyBehaviorTest"
- category:"Spider" --defines which sub-category this operator appears in, also based on 'register' category integer at bottom. If blank = "Script"
- --genClassID returnValue:true
- classid:#(1289369524, -2104918710)
- extends:TP_ScriptBase
- invisible:true
- replaceui:true
- version:1
- (
- local triggerDirChange = #(), targetVelocities = #()
- parameters main rollout:params
- (
- dscr1 type:#string ui:dscr1 width:150 height:300
- dscr2 type:#string ui:dscr2 width:150 height:300
- )
- rollout params "Parameters"
- (
- label dscr1 "Adds Musca Behavior" align:#center
- -- - -
- label dscr2 "By Spider" align:#center
- -- - -
- )
- fn tp_color_type =
- (
- 1 --only for coloring in schematic view, 0 == condition, 1 == operator, 2 == helper, 3 == generator, 4 == initiator, 5 == shape
- )
- fn tp_init_inoutputs tp_inout =
- (
- ver = tp_inout.Version()
- if ver == 0 then
- ( --version is 0, that means this is a new creation in the schematicview, a version > 0 let you see which port configuration loaded
- tp_inout.AddInput #TP_PORT_TYPE_PARTICLE "Particle" #TP_PORT_FLAG_NEEDED --0
- tp_inout.AddInput #TP_PORT_TYPE_FLOAT "MinSpeed" #TP_PORT_FLAG_NONE --1
- tp_inout.AddInput #TP_PORT_TYPE_FLOAT "MaxSpeed" #TP_PORT_FLAG_NONE --2
- tp_inout.AddInput #TP_PORT_TYPE_FLOAT "ChangeDirFreq" #TP_PORT_FLAG_NONE --3
- tp_inout.AddInput #TP_PORT_TYPE_POSITION "BoundPos" #TP_PORT_FLAG_NONE --4
- tp_inout.AddInput #TP_PORT_TYPE_FLOAT "BoundSize" #TP_PORT_FLAG_NONE --5
- tp_inout.AddInput #TP_PORT_TYPE_VELOCITY "Velocity" #TP_PORT_FLAG_NEEDED --6
- tp_inout.AddOutput #TP_PORT_TYPE_VELOCITY "Velocity" #TP_PORT_FLAG_NONE --0
- tp_inout.AddOutput #TP_PORT_TYPE_ALIGNMENT "Alignment" #TP_PORT_FLAG_NONE --1
- tp_inout.SetVersion 1 --important, the inoutput version must set to the actual version
- )
- else
- (
- --here check for port conversion, if the version not equal to the actual version
- --use insert, move, remove, rename, to convert the ports to the actual status if you need
- --tp_inout.SetVersion x --importen the inoutput version must set to the actual version
- )
- )
- fn tp_calculate tp_outid tp_inout tp_system =
- (
- if tp_outid >= 0 then
- (
- --if tp_outid >= 0 caller want the value of the output port with this id
- --attention, TP doesn't cache the result value, if the outport has multiple connections this function will be called for every connection
- if tp_inout.InputConnected 0 then
- (
- pid = tp_inout.GetInValue 0 --get the value from the inputport "Particle"
- if (pid != undefined) then
- (
- curVelocity = tp_inout.GetInValue 6 --tp_system.Velocity pid
- --print "curVelocity"
- --print curVelocity
- curAge = tp_system.Age pid
- minSpeed = tp_inout.GetInValue 1
- maxSpeed = tp_inout.GetInValue 2
- changeDirFreq = tp_inout.GetInValue 3
- boundPos = tp_inout.GetInValue 4
- boundSize = tp_inout.GetInValue 5
- if minSpeed == undefined then minSpeed = 5.0
- if maxSpeed == undefined then maxSpeed = 10.0
- if changeDirFreq == undefined then changeDirFreq = 7.0
- if boundPos == undefined then boundPos = [0.0,0.0,0.0]
- if boundSize == undefined then boundSize = 5.0
- velocityOut = [0,0,0] --1.0/4800 TICKS
- --velocityOut = [(random 0.0 1.0)/4800, (random 0.0 1.0)/4800, (random 0.0 1.0)/4800]
- alignmentOut = Matrix3
- if (curAge.frame == 0f) then
- (
- triggerDirChange = #()
- targetVelocities = #()
- triggerDirChange[pid+1] = (curAge.frame as Integer) + (random 1.0 changeDirFreq)
- targetVelocities[pid+1] = [0,0,0]
- )
- if (triggerDirChange[pid+1] != undefined) then
- (
- if (triggerDirChange[pid+1] < curAge.frame) then
- (
- --Change Direction
- --velocityOut = [(random 0.0 1.0)/4800.0, (random 0.0 1.0)/4800.0, (random 0.0 1.0)/4800.0]
- --Change Direction
- targetVelocities[pid+1] = [(random -maxSpeed maxSpeed)/4800, (random -maxSpeed maxSpeed)/4800, (random -maxSpeed maxSpeed)/4800/(random 1.0 10.0)]
- triggerDirChange[pid+1] = curAge + random 1.0 changeDirFreq
- --velocityOut = curVelocity
- ) else (
- interpol = random 0.5 1.0
- lengthInterpolation = (length(curVelocity) + (length(targetVelocities[pid+1]) - length(curVelocity))*interpol)
- curVelocity = normalize(normalize(curVelocity) + (normalize(targetVelocities[pid+1]) - normalize(curVelocity) )*interpol) * lengthInterpolation
- velocityOut = curVelocity
- )
- )
- else
- (
- triggerDirChange[pid+1] = curAge + random 1.0 changeDirFreq
- targetVelocities[pid+1] = [0,0,0]
- velocityOut = curVelocity
- )
- --print "velocityOut"
- --print velocityOut
- case tp_outid of
- (
- 0: velocityOut
- 1: alignmentOut
- )
- )
- else undefined
- )
- else undefined --the result is undefined. This is the signal for the caller that the value cannot be evaluated
- )
- else
- (
- --If tp_outid < 0 then it is a global call. TP enumerates all operators in the MasterDynamic tree and calls there tp_calculate with outid = -1
- --A global call is also called if the operator input is connected to an initiator output. Initiator outputs are marked with a "*".
- --They scan the connected operators and make a calculate call with outid = -1
- --As an example a PPass is called globally and then he actively calls all operators connected to his initiator outputs.
- --Initiator outputs give only a valid value back if the operator actively called his output with ReCalculateOutputs, otherwise undefined.
- --That's the reason why parallel connected initiator operators do not work together: if the first one called active his outputs,
- --the outputs from the second operator are still undefined. The way it works is to make it serial: the first operator calls active the second one
- undefined
- )
- )
- )
- tp_register FlyBehaviorTest 1 --register the plugin and indicate what category type: 0 condition, 1 operator, 2 helper
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement