trimesh.viewer package

Module contents

viewer

View meshes and scenes via pyglet or inline HTML.

Classes

SceneWidget(scene, **kwargs)

SceneViewer(scene[, smooth, flags, visible, …])

Functions

render_scene(scene[, resolution, visible])

Render a preview of a scene to a PNG.

in_notebook()

Check to see if we are in an IPython or Jypyter notebook.

scene_to_notebook(scene[, height])

Convert a scene to HTML containing embedded geometry and a three.js viewer that will display nicely in an IPython/Jupyter notebook.

scene_to_html(scene)

Return HTML that will render the scene using

class trimesh.viewer.SceneWidget(scene, **kwargs)

Bases: glooey.widget.Widget

Methods

clear()

do_claim()

Return the minimum width and height needed to render this widget.

do_draw()

Draw any shapes or images associated with this widget.

do_regroup()

React to a change in the widget’s pyglet graphics group.

do_undraw()

Delete any shapes or images associated with this widget.

on_mouse_drag(x, y, dx, dy, buttons, modifiers)

React to the mouse being dragged within this widget.

on_mouse_press(x, y, buttons, modifiers)

React when the mouse is pressed on this widget.

on_mouse_scroll(x, y, dx, dy)

React to the mouse scroll wheel being turned.

reset_view()

Attributes

scene_group

clear()
do_claim()

Return the minimum width and height needed to render this widget.

Most widgets need to implement this method. The exception is widgets that have exactly one child. In that case, there’s a reasonable default: claim just enough space for that child. Most composite widgets are covered by this default, because they typically have a container widget as their only child and attach any other widgets they need to that container.

do_draw()

Draw any shapes or images associated with this widget.

This method is called by _draw() after it checks to make sure the widget is attached to the root of the GUI hierarchy and that the rect, group, and batch attributes have all been set.

This method is called both to draw the widget for the first time and to update it subsequently. This usually means that you need to check to see if your graphics objects and resources need to be initialized yet.

do_regroup()

React to a change in the widget’s pyglet graphics group.

In pyglet, groups are used to control layers and OpenGL state. This method is called whenever the widget’s group is changed, for example when the widget is attached to the GUI or moved from one part of the GUI to another.

Only widgets that actually draw things need to implement this method, because groups aren’t used for anything but drawing. Widgets that contain other widgets may need to implement do_regroup_children() to describe how those children should be regrouped.

It’s not always trivial to change the group used to draw something in pyglet. The way to do this depends on what’s being drawn and whether or not it’s been drawn before. The simplest case are high-level APIs like pyglet.sprite.Sprite that allow you to simply change a group attribute. On the other hand, if you’re drawing vertex lists yourself, you need to call the pyglet.graphics.Batch.migrate() method. This method needs to know the OpenGL mode (e.g. GL_QUADS) associated with the vertex list, so you will have to keep track of that.

Keep in mind that this method is called before the widget is drawn and may be called after it’s been undrawn, so any vertex lists created in the draw function may or may not exist yet/anymore. If those vertex lists don’t exist yet, there’s nothing this function needs to do. The _draw() function will be called when the widget’s ready to draw, and at that point the vertex lists should be created with the right group.

do_undraw()

Delete any shapes or images associated with this widget. This method may be called before _draw().

on_mouse_drag(x, y, dx, dy, buttons, modifiers)

React to the mouse being dragged within this widget.

The on_mouse_drag event is propagated to any children that remained under the mouse, an on_mouse_drag_enter event is triggered in any children that just came under the mouse, and an on_mouse_drag_leave event is triggered in any children that were previously under the mouse but no longer are.

on_mouse_press(x, y, buttons, modifiers)

React when the mouse is pressed on this widget.

The on_mouse_press` event is propagated to any children under the mouse, an ``on_mouse_hold event is started, and an on_rollover event is fired to indicate that the widget’s rollover state is now “down”.

on_mouse_scroll(x, y, dx, dy)

React to the mouse scroll wheel being turned.

The on_mouse_scroll event is propagated to any children under the mouse.

reset_view()
property scene_group
class trimesh.viewer.SceneViewer(scene, smooth=True, flags=None, visible=True, resolution=None, start_loop=True, callback=None, callback_period=None, caption=None, fixed=None, offset_lines=True, background=None, window_conf=None, profile=False, **kwargs)

Bases: pyglet.window.Window

Methods

add_geometry(name, geometry, **kwargs)

Add a geometry to the viewer.

cleanup_geometries()

Remove any stored vertex lists that no longer exist in the scene.

hide_geometry(node)

Don’t display the geometry contained at a node on the next draw.

init_gl()

Perform the magic incantations to create an OpenGL scene using pyglet.

on_draw()

Run the actual draw calls.

on_key_press(symbol, modifiers)

Call appropriate functions given key presses.

on_mouse_drag(x, y, dx, dy, buttons, modifiers)

Pan or rotate the view.

on_mouse_press(x, y, buttons, modifiers)

Set the start point of the drag.

on_mouse_scroll(x, y, dx, dy)

Zoom the view.

on_resize(width, height)

Handle resized windows.

reset_view([flags])

Set view to the default view.

save_image(file_obj)

Save the current color buffer to a file object in PNG format.

toggle_axis()

Toggle a rendered XYZ/RGB axis marker:

toggle_culling()

Toggle back face culling.

toggle_fullscreen()

Toggle between fullscreen and windowed mode.

toggle_grid()

Toggle a rendered grid.

toggle_wireframe()

Toggle wireframe mode

unhide_geometry(node)

If a node is hidden remove the flag and show the geometry on the next draw.

update_flags()

Check the view flags, and call required GL functions.

add_geometry(name, geometry, **kwargs)

Add a geometry to the viewer.

Parameters
  • name (hashable) – Name that references geometry

  • geometry (Trimesh, Path2D, Path3D, PointCloud) – Geometry to display in the viewer window

  • ** (kwargs) – Passed to rendering.convert_to_vertexlist

cleanup_geometries()

Remove any stored vertex lists that no longer exist in the scene.

hide_geometry(node)

Don’t display the geometry contained at a node on the next draw.

Parameters

node (str) – Node to not display

init_gl()

Perform the magic incantations to create an OpenGL scene using pyglet.

on_draw()

Run the actual draw calls.

on_key_press(symbol, modifiers)

Call appropriate functions given key presses.

on_mouse_drag(x, y, dx, dy, buttons, modifiers)

Pan or rotate the view.

on_mouse_press(x, y, buttons, modifiers)

Set the start point of the drag.

on_mouse_scroll(x, y, dx, dy)

Zoom the view.

on_resize(width, height)

Handle resized windows.

reset_view(flags=None)

Set view to the default view.

Parameters

flags (None or dict) – If any view key passed override the default e.g. {‘cull’: False}

save_image(file_obj)

Save the current color buffer to a file object in PNG format.

Parameters

file_obj (file name, or file- like object) –

toggle_axis()

Toggle a rendered XYZ/RGB axis marker: off, world frame, every frame

toggle_culling()

Toggle back face culling.

It is on by default but if you are dealing with non- watertight meshes you probably want to be able to see the back sides.

toggle_fullscreen()

Toggle between fullscreen and windowed mode.

toggle_grid()

Toggle a rendered grid.

toggle_wireframe()

Toggle wireframe mode

Good for looking inside meshes, off by default.

unhide_geometry(node)

If a node is hidden remove the flag and show the geometry on the next draw.

Parameters

node (str) – Node to display

update_flags()

Check the view flags, and call required GL functions.

trimesh.viewer.render_scene(scene, resolution=None, visible=True, **kwargs)

Render a preview of a scene to a PNG.

Parameters
  • scene (trimesh.Scene) – Geometry to be rendered

  • resolution ((2,) int or None) – Resolution in pixels, or set from scene.camera

  • kwargs – Passed to SceneViewer

Returns

render – Image in PNG format

Return type

bytes

trimesh.viewer.in_notebook()

Check to see if we are in an IPython or Jypyter notebook.

Returns

in_notebook – Returns True if we are in a notebook

Return type

bool

trimesh.viewer.scene_to_notebook(scene, height=500, **kwargs)

Convert a scene to HTML containing embedded geometry and a three.js viewer that will display nicely in an IPython/Jupyter notebook.

Parameters

scene (trimesh.Scene) – Source geometry

Returns

html – Object containing rendered scene

Return type

IPython.display.HTML

trimesh.viewer.scene_to_html(scene)

Return HTML that will render the scene using GLTF/GLB encoded to base64 loaded by three.js

Parameters

scene (trimesh.Scene) – Source geometry

Returns

html – HTML containing embedded geometry

Return type

str