boinor.render.scene¶
Classes¶
Main window for rendering 3D models using Vispy. |
Module Contents¶
- class boinor.render.scene.MainWindow(camera='turntable', fov=60, bgcolor='black', size=(800, 600))¶
Bases:
vispy.scene.SceneCanvasMain window for rendering 3D models using Vispy.
This class provides a graphical interface for rendering 3D models using the Vispy library. It sets up a scene canvas with interactive capabilities, allowing users to visualize and interact with 3D models. The canvas includes a grid layout and a view with a configurable camera for easy manipulation of the 3D scene.
- Parameters:
camera (str, optional) – Camera type for the view. Options are “turntable”, “arcball”, “fly”, “panzoom”. Default is “turntable”.
fov (float, optional) – Field of view in degrees. Only applicable for 3D cameras. Default is 60.
bgcolor (str or tuple, optional) – Background color of the view. Can be a color name (e.g., “black”, “white”) or an RGBA tuple. Default is “black”.
size (tuple of int, optional) – Window size as (width, height). Default is (800, 600).
- grid¶
- view¶
- set_model(vertices, faces, shading='smooth', color='grey')¶
Set the 3D model to be rendered.
This method takes arrays of vertices and faces, creates a mesh from them, and adds the mesh to the view for rendering.
- Parameters:
vertices (numpy.ndarray) – Array of vertex coordinates.
faces (numpy.ndarray) – Array of face indices.
shading (str, optional) – Shading mode for the mesh. Options are “smooth”, “flat”, or None. Default is “smooth”.
color (str or tuple, optional) – Color of the mesh. Can be a color name (e.g., “grey”, “red”) or an RGBA tuple (e.g., (1.0, 0.5, 0.5, 1.0)). Default is “grey”.
- transforms¶
- property scene¶
The SubScene object that represents the root node of the scene graph to be displayed.
- property central_widget¶
Returns the default widget that occupies the entire area of the canvas.
- property bgcolor¶
- update(node=None)¶
Update the scene
- Parameters:
node (instance of Node) – Not used.
- render(region=None, size=None, bgcolor=None, crop=None, alpha=True)¶
Render the scene to an offscreen buffer and return the image array.
- Parameters:
region (tuple | None) – Specifies the region of the canvas to render. Format is (x, y, w, h). By default, the entire canvas is rendered.
size (tuple | None) – Specifies the size of the image array to return. If no size is given, then the size of the region is used, multiplied by the pixel scaling factor of the canvas (see pixel_scale). This argument allows the scene to be rendered at resolutions different from the native canvas resolution.
bgcolor (instance of Color | None) – The background color to use.
crop (array-like | None) – If specified it determines the pixels read from the framebuffer. In the format (x, y, w, h), relative to the region being rendered.
alpha (bool) – If True (default) produce an RGBA array (h, w, 4). If False, remove the Alpha channel and return the RGB array (h, w, 3). This may be useful if blending of various elements requires a solid background to produce the expected visualization.
- Returns:
image – Numpy array of type ubyte and shape (h, w, 4). Index [0, 0] is the upper-left corner of the rendered region. If
alphaisFalse, then only 3 channels will be returned (RGB).- Return type:
array
- draw_visual(visual, event=None)¶
Draw a visual and its children to the canvas or currently active framebuffer.
- Parameters:
visual (Visual) – The visual to draw
event (None or DrawEvent) – Optionally specifies the original canvas draw event that initiated this draw.
- visual_at(pos)¶
Return the visual at a given position
- Parameters:
pos (tuple) – The position in logical coordinates to query.
- Returns:
visual – The visual at the position, if it exists.
- Return type:
instance of Visual | None
- visuals_at(pos, radius=10)¶
Return a list of visuals within radius pixels of pos.
Visuals are sorted by their proximity to pos.
- push_viewport(viewport)¶
Push a viewport (x, y, w, h) on the stack. Values must be integers relative to the active framebuffer.
- Parameters:
viewport (tuple) – The viewport as (x, y, w, h).
- pop_viewport()¶
Pop a viewport from the stack.
- push_fbo(fbo, offset, csize)¶
Push an FBO on the stack.
This activates the framebuffer and causes subsequent rendering to be written to the framebuffer rather than the canvas’s back buffer. This will also set the canvas viewport to cover the boundaries of the framebuffer.
- pop_fbo()¶
Pop an FBO from the stack.
- property dpi¶
The physical resolution of the canvas in dots per inch.
- events¶
- create_native()¶
Create the native widget if not already done so. If the widget is already created, this function does nothing.
- property context¶
The OpenGL context of the native widget
It gives access to OpenGL functions to call on this canvas object, and to the shared context namespace.
- property app¶
The vispy Application instance on which this Canvas is based.
- property native¶
The native widget object on which this Canvas is based.
- connect(fun)¶
Connect a function to an event
The name of the function should be on_X, with X the name of the event (e.g. ‘on_draw’).
This method is typically used as a decorator on a function definition for an event handler.
- Parameters:
fun (callable) – The function.
- property size¶
The size of canvas/window.
- property physical_size¶
The physical size of the canvas/window, which may differ from the size property on backends that expose HiDPI.
- property pixel_scale¶
The ratio between the number of logical pixels, or ‘points’, and the physical pixels on the device. In most cases this will be 1.0, but on certain backends this will be greater than 1. This should be used as a scaling factor when writing your own visualisations with gloo (make a copy and multiply all your logical pixel values by it). When writing Visuals or SceneGraph visualisations, this value is exposed as TransformSystem.px_scale.
- property fullscreen¶
- property position¶
The position of canvas/window relative to screen.
- property title¶
The title of canvas/window.
- property fps¶
The fps of canvas/window, as the rate that events.draw is emitted.
- set_current(event=None)¶
Make this the active GL canvas
- Parameters:
event (None) – Not used.
- swap_buffers(event=None)¶
Swap GL buffers such that the offscreen buffer becomes visible
- Parameters:
event (None) – Not used.
- show(visible=True, run=False)¶
Show or hide the canvas
- close()¶
Close the canvas
Notes
This will usually destroy the GL context. For Qt, the context (and widget) will be destroyed only if the widget is top-level. To avoid having the widget destroyed (more like standard Qt behavior), consider making the widget a sub-widget.
- measure_fps(window=1, callback='%1.1f FPS')¶
Measure the current FPS
Sets the update window, connects the draw event to update_fps and sets the callback function.
- Parameters:
window (float) – The time-window (in seconds) to calculate FPS. Default 1.0.
callback (function | str) – The function to call with the float FPS value, or the string to be formatted with the fps value and then printed. The default is
'%1.1f FPS'. If callback evaluates to False, the FPS measurement is stopped.
- __repr__()¶
- __enter__()¶
- __exit__(type, value, traceback)¶
- __setattr__(key, value)¶
- freeze()¶
Freeze the object so that only existing properties can be set
- unfreeze()¶
Unfreeze the object so that additional properties can be added