-
Notifications
You must be signed in to change notification settings - Fork 37
Graphics blocks
Peter Corke edited this page May 25, 2026
·
1 revision
This page describes the contract for writing new graphics blocks that inherit from GraphicsBlock.
A graphics block should:
- Inherit from
GraphicsBlock - Call
super().start(simstate)at the top ofstart - Call
super().step(t, inports)at the end ofstep - Usually not override
doneunless it has custom cleanup
After super().start(simstate) returns and graphics are enabled:
-
self.figis valid and ready to use -
self.axis created by the base class for both 2D and 3D blocks - key handling is connected (
qcloses current window,Qcloses all and requests stop) - movie setup is automatic when
movie=...is provided
GraphicsBlock.step() handles:
- backend-specific redraw/flush behavior
- movie frame capture when movie output is enabled
So subclasses should do their drawing updates first, then call super().step(...).
By default, GraphicsBlock creates one 2D axes.
Axes behavior is controlled by the class variable PLOT3D:
-
False(default): create a 2D axes -
True: create a 3D axes (projection='3d')
If a subclass does not define PLOT3D, it inherits the base default (False).
In tiled mode, each graphics block receives a tile slot and GraphicsBlock
creates either a 2D or 3D axes in that slot based on PLOT3D.
class MyPlot(GraphicsBlock):
nin = 1
nout = 0
# Optional; shown here for clarity. Omit to use inherited default.
PLOT3D = False
def start(self, simstate):
super().start(simstate)
if not self._enabled:
return
# fig/ax already prepared by GraphicsBlock
self.line, = self.ax.plot([], [], "k-")
def step(self, t, inports):
if not self._enabled:
return
y = inports[0]
# ... update line data ...
super().step(t, inports)class MyPlot3D(GraphicsBlock):
nin = 1
nout = 0
PLOT3D = True
def start(self, simstate):
super().start(simstate)
if not self._enabled:
return
# fig/ax are prepared by GraphicsBlock
self.ax.set_xlabel("X")
self.ax.set_ylabel("Y")
self.ax.set_zlabel("Z")
def step(self, t, inports):
if not self._enabled:
return
# ... update 3D artists ...
super().step(t, inports)- Do not call
_start_movie()directly in subclasses. - Assigning
self.figis sufficient; movie setup is automatic. - In tiled mode, GraphicsBlock creates 2D or 3D tile axes automatically from
PLOT3D. - Keep drawing logic in the subclass and lifecycle logic in the base class.
Copyright (c) Peter Corke 2020-
- Home
- Control Systems Magazine article
- FAQ
- Changes
- Adding blocks
- Block path
- Connecting blocks
- Subsystems
- Compiling
- Running
- Runtime options
- Discrete-time blocks
- Figures
- Notebook animation
- PID control
- Coding patterns
- Block methods and attributes
- Event handling
- Discrete-time dynamics
- Blocks, wires and plug
- Discrete-time blocks
- Evaluation
- Runtimes and simulator state
- Creating a new block
- Future & related work
Under development on feat/realtime branch