Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- FlyBehaviorTest
  2. -- scripted plugin for TP 4.0
  3. -- v0.1
  4. --username@email.com
  5.  
  6.  
  7. -- WHAT IT DOES:
  8.  
  9.  
  10.  
  11. tp_unregister FlyBehaviorTest       --unregister before you make changes in the plugin, you can test the code on tp runtime
  12.  
  13. plugin  Geometry FlyBehaviorTest
  14. name:"FlyBehaviorTest"
  15. category:"Spider"           --defines which sub-category this operator appears in, also based on 'register' category integer at bottom.  If blank = "Script"
  16. --genClassID returnValue:true
  17. classid:#(1289369524, -2104918710)
  18. extends:TP_ScriptBase
  19. invisible:true
  20. replaceui:true
  21. version:1
  22. (  
  23.     local triggerDirChange = #(), targetVelocities = #()
  24.    
  25.     parameters main rollout:params
  26.     (
  27.         dscr1 type:#string ui:dscr1 width:150 height:300
  28.         dscr2 type:#string ui:dscr2 width:150 height:300
  29.     )
  30.  
  31.     rollout params "Parameters"
  32.     (
  33.         label dscr1 "Adds Musca Behavior" align:#center
  34.         --              -                                           -
  35.         label dscr2 "By Spider" align:#center
  36.         --              -                                           -
  37.     )
  38.    
  39.     fn tp_color_type =
  40.     (
  41.         1   --only for coloring in schematic view, 0 == condition, 1 == operator, 2 == helper, 3 == generator, 4 == initiator, 5 == shape
  42.     )
  43.    
  44.     fn tp_init_inoutputs tp_inout =
  45.     (
  46.         ver = tp_inout.Version()
  47.                
  48.         if ver == 0 then
  49.         (   --version is 0, that means this is a new creation in the schematicview, a version > 0 let you see which port configuration loaded
  50.            
  51.             tp_inout.AddInput   #TP_PORT_TYPE_PARTICLE  "Particle"          #TP_PORT_FLAG_NEEDED        --0
  52.            
  53.             tp_inout.AddInput   #TP_PORT_TYPE_FLOAT "MinSpeed"              #TP_PORT_FLAG_NONE          --1
  54.            
  55.             tp_inout.AddInput   #TP_PORT_TYPE_FLOAT "MaxSpeed"              #TP_PORT_FLAG_NONE          --2
  56.            
  57.             tp_inout.AddInput   #TP_PORT_TYPE_FLOAT "ChangeDirFreq"             #TP_PORT_FLAG_NONE          --3
  58.            
  59.             tp_inout.AddInput   #TP_PORT_TYPE_POSITION  "BoundPos"              #TP_PORT_FLAG_NONE          --4
  60.            
  61.             tp_inout.AddInput   #TP_PORT_TYPE_FLOAT "BoundSize"             #TP_PORT_FLAG_NONE          --5
  62.            
  63.             tp_inout.AddInput   #TP_PORT_TYPE_VELOCITY    "Velocity"            #TP_PORT_FLAG_NEEDED        --6
  64.            
  65.             tp_inout.AddOutput  #TP_PORT_TYPE_VELOCITY      "Velocity"              #TP_PORT_FLAG_NONE          --0
  66.            
  67.             tp_inout.AddOutput  #TP_PORT_TYPE_ALIGNMENT     "Alignment"             #TP_PORT_FLAG_NONE          --1
  68.            
  69.             tp_inout.SetVersion 1  --important, the inoutput version must set to the actual version
  70.         )
  71.         else
  72.         (
  73.             --here check for port conversion, if the version not equal to the actual version
  74.             --use insert, move, remove, rename, to convert the ports to the actual status if you need
  75.            
  76.             --tp_inout.SetVersion x  --importen the inoutput version must set to the actual version
  77.         )
  78.     )
  79.    
  80.     fn tp_calculate tp_outid tp_inout tp_system =
  81.     (
  82.         if tp_outid >= 0 then
  83.         (
  84.             --if tp_outid >= 0 caller want the value of the output port with this id
  85.             --attention, TP doesn't cache the result value, if the outport has multiple connections this function will be called for every connection
  86.        
  87.             if tp_inout.InputConnected 0 then
  88.             (
  89.                 pid = tp_inout.GetInValue 0     --get the value from the inputport "Particle"
  90.                
  91.                
  92.                 if (pid != undefined) then
  93.                 (
  94.                     curVelocity = tp_inout.GetInValue 6 --tp_system.Velocity pid
  95.                    
  96.                     --print "curVelocity"
  97.                     --print curVelocity
  98.                    
  99.                     curAge = tp_system.Age pid
  100.                    
  101.                     minSpeed = tp_inout.GetInValue 1
  102.                
  103.                     maxSpeed = tp_inout.GetInValue 2
  104.        
  105.                     changeDirFreq = tp_inout.GetInValue 3
  106.                
  107.                     boundPos = tp_inout.GetInValue 4
  108.                
  109.                     boundSize = tp_inout.GetInValue 5
  110.                    
  111.                     if minSpeed == undefined then minSpeed = 5.0
  112.                    
  113.                     if maxSpeed == undefined then maxSpeed = 10.0
  114.                
  115.                     if changeDirFreq == undefined then changeDirFreq = 7.0
  116.                
  117.                     if boundPos == undefined then boundPos = [0.0,0.0,0.0]
  118.                    
  119.                     if boundSize == undefined then boundSize = 5.0
  120.                        
  121.  
  122.                     velocityOut = [0,0,0] --1.0/4800 TICKS
  123.                    
  124.                    
  125.                     --velocityOut =  [(random 0.0 1.0)/4800, (random 0.0 1.0)/4800, (random 0.0 1.0)/4800]
  126.                    
  127.                     alignmentOut = Matrix3
  128.                    
  129.                     if (curAge.frame == 0f) then
  130.                     (
  131.                         triggerDirChange = #()
  132.                         targetVelocities = #()
  133.                         triggerDirChange[pid+1]  = (curAge.frame as Integer) + (random 1.0 changeDirFreq)
  134.                         targetVelocities[pid+1] = [0,0,0]
  135.                     )
  136.  
  137.                    
  138.                     if (triggerDirChange[pid+1] != undefined) then
  139.                     (
  140.  
  141.                         if (triggerDirChange[pid+1] < curAge.frame) then
  142.                         (
  143.                             --Change Direction
  144.                             --velocityOut = [(random 0.0 1.0)/4800.0, (random 0.0 1.0)/4800.0, (random 0.0 1.0)/4800.0]
  145.                             --Change Direction
  146.                             targetVelocities[pid+1] = [(random -maxSpeed maxSpeed)/4800, (random -maxSpeed maxSpeed)/4800, (random -maxSpeed maxSpeed)/4800/(random 1.0 10.0)]
  147.  
  148.                        
  149.                             triggerDirChange[pid+1] = curAge + random 1.0 changeDirFreq
  150.                            
  151.                             --velocityOut = curVelocity
  152.                         ) else (
  153.                             interpol = random 0.5 1.0
  154.                             lengthInterpolation = (length(curVelocity) + (length(targetVelocities[pid+1]) - length(curVelocity))*interpol)
  155.                             curVelocity = normalize(normalize(curVelocity) + (normalize(targetVelocities[pid+1]) - normalize(curVelocity) )*interpol) * lengthInterpolation
  156.                            
  157.                             velocityOut = curVelocity
  158.                            
  159.                         )
  160.                        
  161.  
  162.                     )
  163.                     else
  164.                     (
  165.  
  166.                         triggerDirChange[pid+1] = curAge + random 1.0 changeDirFreq
  167.                         targetVelocities[pid+1] = [0,0,0]
  168.                         velocityOut = curVelocity
  169.                     )
  170.                    
  171.                     --print "velocityOut"
  172.                     --print velocityOut
  173.                     case tp_outid of
  174.                     (
  175.                         0: velocityOut
  176.                    
  177.                         1: alignmentOut
  178.                    
  179.                     )
  180.                 )
  181.                 else undefined
  182.                
  183.             )
  184.             else undefined  --the result is undefined.  This is the signal for the caller that the value cannot be evaluated
  185.         )
  186.         else
  187.         (
  188.             --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
  189.             --A global call is also called if the operator input is connected to an initiator output.  Initiator outputs are marked with a "*".
  190.             --They scan the connected operators and make a calculate call with outid = -1
  191.             --As an example a PPass is called globally and then he actively calls all operators connected to his initiator outputs.
  192.             --Initiator outputs give only a valid value back if the operator actively called his output with ReCalculateOutputs, otherwise undefined.
  193.             --That's the reason why parallel connected initiator operators do not work together:  if the first one called active his outputs,
  194.             --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
  195.             undefined
  196.         )
  197.     )
  198. )
  199. 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