Skip to content

Instantly share code, notes, and snippets.

@nicelifeBS
Created July 28, 2016 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicelifeBS/07173f1ff461a852fdd06f2a713aab73 to your computer and use it in GitHub Desktop.
Save nicelifeBS/07173f1ff461a852fdd06f2a713aab73 to your computer and use it in GitHub Desktop.
modo channel reading API
# @author David Ballesteros
#
# @brief Helper classes to deal with the read and write of Item Channels.
# Based on a ChannelRead class sample code provided by Matt Cox
import lx
class ChannelRead:
def __init__(self, item):
"""
Class to read channel values and action layers at certain time
:param self
:param item The Item from which read a Channel value.
:return: None
:rtype: None
"""
# Get the selection service
self.sel_svc = lx.service.Selection()
# Get a new ChannelRead object
self.chan_read_obj = lx.object.ChannelRead()
# Keep the passed Item
self.item = lx.object.Item(item)
# Get the Scene the Item belongs to
self.scene = self.item.Context()
# Set the Action layer. EDIT by default
self.action_layer = lx.symbol.s_ACTIONLAYER_EDIT
# Assign the ChannelRead object at the actual time
# scene.Channels() can create a ChannelRead Object or a ChannelWrite Object
# In Python always return a ChannelRead Object unless you cast it
self.chan_read_obj = self.scene.Channels (self.action_layer, self.sel_svc.GetTime())
def AtTime(self, time):
""" Obtains the ChannelRead object at certain time """
# @param time float - The time at which obtain the ChannelRead object.
self.chan_read_obj = self.scene.Channels (self.action_layer, time)
def FromEdit(self):
""" Obtains the ChannelRead object from the Edit Action Layer at the actual time """
self.action_layer = lx.symbol.s_ACTIONLAYER_EDIT
self.chan_read_obj = self.scene.Channels (self.action_layer, self.sel_svc.GetTime())
def FromSetup(self):
""" Obtains the ChannelRead object from the Setup Action Layer at the actual time """
self.action_layer = lx.symbol.s_ACTIONLAYER_SETUP
self.chan_read_obj = self.scene.Channels (self.action_layer, self.sel_svc.GetTime())
def GetValue (self, channel):
""" Read the value of a channel by name """
# @param channel String - The name of the Channel to get it's value.
channel_id = None
# Check is passed a string with the name of a channel
if isinstance(channel, str):
channel_id = self.item.ChannelLookup(channel)
# Get the Channel type
chan_type = self.item.ChannelEvalType(channel_id)
# Return the appropiate value depending on the Channel Type
if chan_type == "float":
return self.chan_read_obj.Double(self.item, channel_id)
if chan_type == "integer":
return self.chan_read_obj.Integer(self.item, channel_id)
if chan_type == "string":
return self.chan_read_obj.String(self.item, channel_id)
def GetValueAtTime (self, channel, key = 0.0):
""" Read the value of a channel by name """
# @param channel String - The name of the Channel to get it's value.
channel_id = None
# Check is passed a string with the name of a channel
if isinstance(channel, str):
channel_id = self.item.ChannelLookup(channel)
# Get the Channel type
chan_type = self.item.ChannelEvalType(channel_id)
# Return the appropiate value depending on the Channel Type
if chan_type == "float":
return self.chan_read_obj.Double(self.item, channel_id)
if chan_type == "integer":
return self.chan_read_obj.Integer(self.item, channel_id)
if chan_type == "string":
return self.chan_read_obj.String(self.item, channel_id)
class ChannelWrite:
def __init__(self, item):
""" Class to write channel values and action layers at certain time"""
# @param item Item - The Item from wich read a Channel value.
# Get the selection service
self.sel_svc = lx.service.Selection()
# Get a new ChannelWrite object
self.chan_write_obj = lx.object.ChannelWrite()
# Keep the passed Item
self.item = lx.object.Item(item)
# Get the Scene the Item belongs to
self.scene = self.item.Context()
# Set the Action layer. EDIT by default
self.action_layer = lx.symbol.s_ACTIONLAYER_EDIT
# Assing the ChannelRead object at the actual time
# scene.Channels() can create a ChannelRead Object or a ChannelWrite Object
# In Python always return a ChannelRead Object unles you cast it
self.chan_write_obj = lx.object.ChannelWrite(self.scene.Channels (self.action_layer, self.sel_svc.GetTime()))
def AtTime(self, time):
""" Obtains the ChannelWrite object at certain time """
# @param time float - The time at which obtain the ChannelWrite object.
self.chan_write_obj = lx.object.ChannelWrite(self.scene.Channels (self.action_layer, time))
def FromEdit(self):
""" Obtains the ChannelWrite object from the Edit Action Layer at the actual time """
self.action_layer = lx.symbol.s_ACTIONLAYER_EDIT
self.chan_write_obj = lx.object.ChannelWrite(self.scene.Channels (self.action_layer, self.sel_svc.GetTime()))
def FromSetup(self):
""" Obtains the ChannelWrite object from the Setup Action Layer at the actual time"""
self.action_layer = lx.symbol.s_ACTIONLAYER_SETUP
self.chan_write_obj = lx.object.ChannelWrite(self.scene.Channels (self.action_layer, self.sel_svc.GetTime()))
def SetValue (self, channel, value):
"""
Write the value of a channel by name
:param channel: The name of the Channel to set it's value.
:type channel: string
"""
channel_id = None
# Check is passed a string with the name of a channel
if isinstance(channel, str):
channel_id = self.item.ChannelLookup(channel)
# Get the Channel type
chan_type = self.item.ChannelEvalType(channel_id)
# Write the appropriate value depending on the Channel Type
if chan_type == "float":
self.chan_write_obj.Double(self.item, channel_id, value)
if chan_type == "integer":
self.chan_write_obj.Integer(self.item, channel_id, value)
if chan_type == "string":
self.chan_write_obj.String(self.item, channel_id, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment