trimesh.viewer package

Submodules

trimesh.viewer.notebook module

notebook.py

Render trimesh.Scene objects in HTML and jupyter notebooks using three.js

trimesh.viewer.notebook.in_notebook()

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

Returns:
in_notebook : bool

Returns True if we are in a notebook

trimesh.viewer.notebook.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 : str

HTML containing embedded geometry

trimesh.viewer.notebook.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 : IPython.display.HTML

Object containing rendered scene

trimesh.viewer.windowed module

windowed.py

Provides a pyglet- based windowed viewer to preview Trimesh, Scene, PointCloud, and Path objects.

Works on all major platforms: Windows, Linux, and OSX.

class trimesh.viewer.windowed.SceneViewer(scene, smooth=True, flags=None, visible=True, resolution=(640, 480), start_loop=True, callback=None, callback_period=None, **kwargs)

Bases: pyglet.window.Window

Attributes:
CURSOR_DEFAULT
WINDOW_STYLE_DEFAULT
caption

The window caption (title).

config

A GL config describing the context of this window.

context

The OpenGL context attached to this window.

display

The display this window belongs to.

fullscreen

True if the window is currently fullscreen.

height

The height of the window, in pixels.

resizeable

True if the window is resizable.

screen

The screen this window is fullscreen in.

style

The window style; one of the WINDOW_STYLE_* constants.

visible

True if the window is currently visible.

vsync

True if buffer flips are synchronised to the screen’s vertical retrace.

width

The width of the window, in pixels.

Methods

activate() Attempt to restore keyboard focus to the window.
add_geometry(name, geometry, **kwargs) Add a geometry to the viewer.
clear() Clear the window.
close() Close the window.
dispatch_events() Poll the operating system event queue for new events and call attached event handlers.
draw_mouse_cursor() Draw the custom mouse cursor.
event(*args) Function decorator for an event handler.
flip() Swap the OpenGL front and back buffers.
get_location() Return the current position of the window.
get_size() Return the current size of the window.
get_system_mouse_cursor(name) Obtain a system mouse cursor.
get_viewport_size() Return the size in actual pixels of the viewport.
init_gl() Perform the magic incantations to create an OpenGL scene.
maximize() Maximize the window.
minimize() Minimize the window.
on_activate() The window was activated.
on_close() The user attempted to close the window.
on_context_lost() The window’s GL context was lost.
on_context_state_lost() The state of the window’s GL context was lost.
on_deactivate() The window was deactivated.
on_draw() The window contents must be redrawn.
on_expose() A portion of the window needs to be redrawn.
on_hide() The window was hidden.
on_key_press(symbol, modifiers) A key on the keyboard was pressed (and held down).
on_key_release(modifiers) A key on the keyboard was released.
on_mouse_drag(x, y, dx, dy, buttons, modifiers) The mouse was moved with one or more mouse buttons pressed.
on_mouse_enter(y) The mouse was moved into the window.
on_mouse_leave(y) The mouse was moved outside of the window.
on_mouse_motion(y, dx, dy) The mouse was moved with no buttons held down.
on_mouse_press(x, y, buttons, modifiers) A mouse button was pressed (and held down).
on_mouse_release(y, button, modifiers) A mouse button was released.
on_mouse_scroll(x, y, dx, dy) The mouse wheel was scrolled.
on_move(y) The window was moved.
on_resize(width, height) The window was resized.
on_show() The window was shown.
on_text() The user input some text.
on_text_motion() The user moved the text input cursor.
on_text_motion_select() The user moved the text input cursor while extending the selection.
pop_handlers() Pop the top level of event handlers off the stack.
push_handlers(*args, **kwargs) Push a level onto the top of the handler stack, then attach zero or more event handlers.
register_event_type(name) Register an event type with the dispatcher.
remove_handler(name, handler) Remove a single event handler.
remove_handlers(*args, **kwargs) Remove event handlers from the event stack.
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.
set_caption(caption) Set the window’s caption.
set_exclusive_keyboard([exclusive]) Prevent the user from switching away from this window using keyboard accelerators.
set_exclusive_mouse([exclusive]) Hide the mouse cursor and direct all mouse events to this window.
set_fullscreen([fullscreen, screen, mode, …]) Toggle to or from fullscreen.
set_handler(name, handler) Attach a single event handler.
set_handlers(*args, **kwargs) Attach one or more event handlers to the top level of the handler stack.
set_icon(*images) Set the window icon.
set_location(x, y) Set the position of the window.
set_maximum_size(width, height) Set the maximum size of the window.
set_minimum_size(width, height) Set the minimum size of the window.
set_mouse_cursor([cursor]) Change the appearance of the mouse cursor.
set_mouse_platform_visible([platform_visible]) Set the platform-drawn mouse cursor visibility.
set_mouse_visible([visible]) Show or hide the mouse cursor.
set_size(width, height) Resize the window.
set_visible([visible]) Show or hide the window.
set_vsync(vsync) Enable or disable vertical sync control.
switch_to() Make this window the current OpenGL rendering context.
toggle_axis() Toggle a rendered XYZ/RGB axis marker on, world frame, or every frame.
toggle_culling() Toggle backface culling on or off.
toggle_fullscreen() Toggle between fullscreen and windowed mode.
toggle_wireframe() Toggle unfilled wireframe mode on or off, good for looking inside meshes.
update_flags() Check the view flags and call what is needed with gl to handle it correctly.
dispatch_event  
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

init_gl()

Perform the magic incantations to create an OpenGL scene.

on_draw()

The window contents must be redrawn.

The EventLoop will dispatch this event when the window should be redrawn. This will happen during idle time after any window events and after any scheduled functions were called.

The window will already have the GL context, so there is no need to call switch_to. The window’s flip method will be called after this event, so your event handler should not.

You should make no assumptions about the window contents when this event is triggered; a resize or expose event may have invalidated the framebuffer since the last time it was drawn.

New in version 1.1.

Event:
on_key_press(symbol, modifiers)

A key on the keyboard was pressed (and held down).

In pyglet 1.0 the default handler sets has_exit to True if the ESC key is pressed.

In pyglet 1.1 the default handler dispatches the on_close() event if the ESC key is pressed.

Parameters:
symbol : int

The key symbol pressed.

modifiers : int

Bitwise combination of the key modifiers active.

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

The mouse was moved with one or more mouse buttons pressed.

This event will continue to be fired even if the mouse leaves the window, so long as the drag buttons are continuously held down.

Parameters:
x : int

Distance in pixels from the left edge of the window.

y : int

Distance in pixels from the bottom edge of the window.

dx : int

Relative X position from the previous mouse position.

dy : int

Relative Y position from the previous mouse position.

buttons : int

Bitwise combination of the mouse buttons currently pressed.

modifiers : int

Bitwise combination of any keyboard modifiers currently active.

Event:
on_mouse_press(x, y, buttons, modifiers)

A mouse button was pressed (and held down).

Parameters:
x : int

Distance in pixels from the left edge of the window.

y : int

Distance in pixels from the bottom edge of the window.

button : int

The mouse button that was pressed.

modifiers : int

Bitwise combination of any keyboard modifiers currently active.

Event:
on_mouse_scroll(x, y, dx, dy)

The mouse wheel was scrolled.

Note that most mice have only a vertical scroll wheel, so scroll_x is usually 0. An exception to this is the Apple Mighty Mouse, which has a mouse ball in place of the wheel which allows both scroll_x and scroll_y movement.

Parameters:
x : int

Distance in pixels from the left edge of the window.

y : int

Distance in pixels from the bottom edge of the window.

scroll_x : int

Number of “clicks” towards the right (left if negative).

scroll_y : int

Number of “clicks” upwards (downwards if negative).

Event:
on_resize(width, height)

The window was resized.

The window will have the GL context when this event is dispatched; there is no need to call switch_to in this handler.

Parameters:
width : int

The new width of the window, in pixels.

height : int

The new height of the window, in pixels.

Event:
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 on, world frame, or every frame. Off by default.

toggle_culling()

Toggle backface culling on or off. 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_wireframe()

Toggle unfilled wireframe mode on or off, good for looking inside meshes. Off by default.

update_flags()

Check the view flags and call what is needed with gl to handle it correctly.

trimesh.viewer.windowed.geometry_hash(geometry)

Get an MD5 for a geometry object

Parameters:
geometry : object
Returns:
MD5 : str
trimesh.viewer.windowed.render_scene(scene, resolution=(1080, 1080), visible=True, **kwargs)

Render a preview of a scene to a PNG.

Parameters:
scene : trimesh.Scene

Geometry to be rendered

resolution : (2,) int

Resolution in pixels

kwargs : **

Passed to SceneViewer

Returns:
render : bytes

Image in PNG format

trimesh.viewer.windowed.view_to_transform(view)

Given a dictionary containing view parameters, calculate a transformation matrix.

Module contents

viewer

View meshes and scenes via pyglet or inline HTML.