I want to make a realtime-streaming chart
here is my code :
import omni.ext
import omni.ui as ui
import threading
from random import uniform
# Any class derived from `omni.ext.IExt` in top level module (defined in `python.modules` of `extension.toml`) will be
# instantiated when extension gets enabled and `on_startup(ext_id)` will be called. Later when extension gets disabled
# on_shutdown() is called.
class ShahnLinePlotExtension(omni.ext.IExt):
#define local variables
def __init__(self):
self.x = 0.0
self.y = 0.0
self.z = 0.0
self.XData = [0.0 for n in range(300)]
self.YData = [0.0 for n in range(300)]
self.ZData = [0.0 for n in range(300)]
self.XPlot = ui.Plot()
self.YPlot = ui.Plot()
self.ZPlot = ui.Plot()
# ext_id is current extension id. It can be used with extension manager to query additional information, like where
# this extension is located on filesystem.
def on_startup(self, ext_id):
self._window = ui.Window("Line Chart", width=600, height=400)
with self._window.frame:
with ui.VStack():
ui.Label("Line Chart 1", alignment=ui.Alignment.CENTER)
self.XPlot = ui.Plot(ui.Type.LINE, -1000.0, 1000.0, *self.XData, width=600, height=100,
alignment=ui.Alignment.CENTER, style={"color": 0xFF0000FF})
ui.Label("Line Chart 2", alignment=ui.Alignment.CENTER)
self.YPlot = ui.Plot(ui.Type.LINE, -2000.0, 2000.0, *self.YData, width=600, height=100,
alignment=ui.Alignment.CENTER, style={"color": 0xFF0000FF})
ui.Label("Line Chart 3", alignment=ui.Alignment.CENTER)
self.ZPlot = ui.Plot(ui.Type.LINE, -10000.0, 10000.0, *self.ZData, width=600, height=100,
alignment=ui.Alignment.CENTER, style={"color": 0xFF0000FF})
self.get_value()
def on_shutdown(self):
print("[shahn.line.plot] shahn line plot shutdown")
def get_value(self):
self.x = uniform(-900, 900)
self.y = uniform(-1900, 1900)
self.z = uniform(-2900, 2900)
self.XData.append(self.x)
self.YData.append(self.y)
self.ZData.append(self.z)
self.XData = self.XData[-300:]
self.YData = self.YData[-300:]
self.ZData = self.ZData[-300:]
self.XPlot.set_data(*self.XData)
self.YPlot.set_data(*self.YData)
self.ZPlot.set_data(*self.ZData)
self._loading_timer = threading.Timer(0.25, self.get_value).start()
I succeeded in executing the realtime streaming chart extension
without the ui freezing using threading,
but When mouse cursor leaves the UI of the extension, chart streaming stops.
I wish it would work even if the mouse cursor goes out of the extension’s ui
Any solution to this problem?
here is the error displayed in the console: