Advertisement
DeaD_EyE

vc

Aug 2nd, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import sys
  4. from collections import OrderedDict
  5. from vcScript import *
  6.  
  7.  
  8. class Button(object):
  9.   """
  10.  switch := button is pressed
  11.  light := light is on
  12.  """
  13.   def __init__(self, name, true_color='light_green', false_color='dark_green'):
  14.     comp = getComponent()
  15.     app = getApplication()
  16.     self.app = app
  17.     self.comp = comp
  18.    
  19.     comp.Material = None
  20.     comp.update()
  21.     app.render()
  22.    
  23.     self.name = name
  24.     self.true_color = app.findMaterial(true_color)
  25.     self.false_color = app.findMaterial(false_color)
  26.     self.node = comp.findNode('Link_' + name)
  27.     self.node.MaterialInheritance = VC_MATERIAL_FORCE_INHERIT_NODE
  28.    
  29.     # print(self.node.Name)
  30.    
  31.     self._option = comp.getProperty('Options::' + name)
  32.     self._input = comp.findBehaviour('Signals::{}_pressed'.format(name))
  33.     self._output = comp.findBehaviour('Signals::{}_light'.format(name))
  34.     if self._output:
  35.       self._output.OnValueChange = self.light_changed
  36.     self._joginfo = comp.findBehaviour('{}_joginfo'.format(name))
  37.     self._joginfo.OnInteract = self.pressed
  38.  
  39.   def light_changed(self, property):
  40.     if self.light:
  41.       self.node.NodeMaterial = self.true_color
  42.     else:
  43.       self.node.NodeMaterial = self.false_color
  44.     self.comp.update()
  45.     self.app.render()
  46.  
  47.   def pressed(self, behaviour, action, data):
  48.     if action == 1:
  49.       self.state = not self.state
  50.       self.switch = self.state
  51.       self.comp.update()
  52.       self.app.render()
  53.  
  54.   @property
  55.   def switch(self):
  56.     return self._input.Value
  57.  
  58.   @switch.setter
  59.   def switch(self, value):
  60.     print('Setting value for switch', self._input.Name, 'to', value)
  61.     # direct write to Value of the property does
  62.     # not work
  63.     # the methdod signal must be used to set the new value
  64.     self._input.signal(value)
  65.  
  66.   @property
  67.   def light(self):
  68.     return self._output.Value
  69.  
  70.   @property
  71.   def state(self):
  72.     return self._option.Value
  73.  
  74.   @state.setter
  75.   def state(self, value):
  76.     self._option.Value = value
  77.  
  78.   def __repr__(self):
  79.     return 'Button("{}")'.format(self.name)
  80.  
  81.   __str__ = __repr__
  82.  
  83.  
  84. class Buttons(object):
  85.  
  86.   def __init__(self, switches, true_color, false_color):
  87.     self._buttons = OrderedDict()
  88.     for sw in switches:
  89.       self._buttons[sw] = Button(sw, true_color, false_color)
  90.  
  91.   def __repr__(self):
  92.     return 'Buttons([{}])'.format(self.switches)
  93.  
  94.   __str__ = __repr__
  95.  
  96.   def __getitem__(self, key):
  97.     return self._buttons[key]
  98.  
  99.   __getattr__ = __getitem__
  100.  
  101.  
  102. if __name__ == '__main__':
  103.   print(sys.version)
  104.   app = getApplication()
  105.   switches = ('Start', 'Stop', 'ACK', 'REF', 'Reset')
  106.   buttons = Buttons(switches, 'light_red', 'dark_red')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement