Basic Example🔗

The example below loads a .tox file, advances it for a fixed number of frames via an on_frame callback, then unloads it to cleanly release resources. The same script is available in the repository at examples/01_load_comp_basic.py.

 1import touchpy as tp
 2
 3# create a class that inherits from tp.Comp
 4# inheriting from tp.Comp is not required but it is the recommended way to interface
 5# with a component
 6
 7class MyComp (tp.Comp):
 8	def __init__(self):
 9		
10		# call the parent class constructor to initialize the component
11		super().__init__()
12
13		# create a frame counter
14		self.frame = 0
15
16		# set the on_frame_callback to the on_frame method
17		self.set_on_frame_callback(self.on_frame)
18
19	# define the on_frame method that will be called on every frame
20	# the info argument is user data that can be passed to the callback
21	# in this case it is an empty dictionary
22	def on_frame(self):
23
24		# stop running the component after 1200 frames (20 seconds at 60 fps)
25		if self.frame == 1200:
26			self.stop()
27			return
28
29		# start_next_frame() is called to advance the component to the next frame
30		self.start_next_frame()
31		self.frame += 1
32
33# create an instance of the MyCompBasic class
34comp = MyComp()
35
36# load the tox file into the component
37comp.load('TopChopDatIO.tox') 
38
39# start the component, this will block until self.stop() is called when not running
40# the component in async mode
41comp.start()
42
43# unload the component to cleanly free up resources
44comp.unload()
45
46
47
48
49
50