2. Add an object to your plot
Full example code: examples/visualization/visualization_step_2.py
As a first object, we will add a simple plain cube to our plot. For any surface we want to add, we need the vertices and how these vertices are connected in elements.
# We define a cube as example data
vertices = np.array([
[0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0],
[0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 1.0],
[1.0, 1.0, 1.0], [0.0, 1.0, 1.0]
])
# Sides of the cube
quads = np.array([
[0, 3, 2, 1], [4, 5, 6, 7], [0, 1, 5, 4],
[1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7]
])
With our cube defined, we can simply create a Surface object and add it to our plot.
To add objects to a plot we use the add_object method to add a single object or or add_objects for a list of objects. We call this method on the renderer of the plot.
For a plot with multiple subplots, we specify which subplot we want to add the object to, starting at index 0.
* For a single dimension of subplots, the index gives the row or column of the subplot.
* For two dimensions of subplots, the first index gives the column, the second index the row of the subplot. [0][0] being top-left.
from dgmr.visualization.renderer import Plot, Subplots
from dgmr.visualization import SurfaceBuilder
# Create our cube object
cube = SurfaceBuilder(vertices, quads)
# Single plot
plot = Plot(640, 480)
plot.renderer.add_object(cube)
plot.start()
# 1D Subplots
subplots = Subplots(640, 480, ncols=2, nrows=1)
subplots.renderers[0].add_object(cube)
subplots.renderers[1].add_object(cube)
subplots.start()
# 2D Subplots
subplot = Subplots(nrows=2, ncols=2)
subplot.renderers[0][1].add_object(my_object)
You can find this example and run it yourself at examples/visualization_step2.py
← Previous: Create an empty plot or subplot Next: Set properties of an object →