Small works Machine process comp ex.

Below you find a short example how to make a component compatible with the works machine process command. The only thing the component needs is one String signal called task.

Then we wait for that signal to be triggered, we check the values, and use up the process time, then we signal back that the process is done. The dummy delay could of course be replaced with some nice servo action or similar.

The dictionary “compsInWorld” contains the names of all comps in the world as keys, and the value is in turn a dict with the components behaviors names as keys and the actual behavior as value. In this way we can quickly find the corresponding component and TaskDone signal.

from vcScript import *

comp = getComponent()

def OnSignal( signal ):
global queue
if signal.Name == ‘task’ and signal.Value:
queue.append(signal.Value)

def OnStart():
global compsInWorld
compsInWorld = {}
for x in getApplication().Components:
behavs = [(b.Name, b) for b in x.Behaviours]
compsInWorld[x.Name] = dict(behavs)

def OnRun():
global queue

queue = []
while True:
condition(lambda: queue)
task = queue.pop()
#task ex = 5.0,WorksProcess #4,123
vals = task.split(",")
processtime = float(vals[0])
worksProcessComponent = vals[1]
#In this example we are expecting the machine command to be of type int
machineCommand = int(vals[2]) if type(eval(vals[2])) == int else 1

if machineCommand == 1:
  pass
  #do something
else:
  #Do something else 
  pass
delay(processtime)

taskDone = compsInWorld[worksProcessComponent]["TaskDone"]
taskDone.signal(task)