.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/introductory/orbit_camera.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_introductory_orbit_camera.py: Orbit Camera ============ Example showing orbit camera controller. Press 's' to save the state, and press 'l' to load the last saved state. .. GENERATED FROM PYTHON SOURCE LINES 10-72 .. image-sg:: /_gallery/introductory/images/sphx_glr_orbit_camera_001.webp :alt: orbit camera :srcset: /_gallery/introductory/images/sphx_glr_orbit_camera_001.webp :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Imageio: 'chelsea.png' was not found on your computer; downloading it now. Try 1. Download from https://github.com/imageio/imageio-binaries/raw/master/images/chelsea.png (216 kB) Downloading: 8192/221294 bytes (3.7%)221294/221294 bytes (100.0%) Done File saved as /home/docs/.imageio/images/chelsea.png. | .. code-block:: Python import imageio.v3 as iio from rendercanvas.auto import RenderCanvas, loop import pygfx as gfx import numpy as np import pylinalg as la canvas = RenderCanvas() renderer = gfx.renderers.WgpuRenderer(canvas) scene = gfx.Scene() scene.add(gfx.AxesHelper(size=250)) im = iio.imread("imageio:chelsea.png") tex = gfx.Texture(im, dim=2) material = gfx.MeshBasicMaterial(map=tex, side="front") geometry = gfx.box_geometry(100, 100, 100) cubes = [gfx.Mesh(geometry, material) for i in range(8)] for i, cube in enumerate(cubes): cube.local.position = (350 - i * 100, 150, 0) scene.add(cube) dark_gray = np.array((169, 167, 168, 255)) / 255 light_gray = np.array((100, 100, 100, 255)) / 255 background = gfx.Background.from_color(light_gray, dark_gray) scene.add(background) camera = gfx.PerspectiveCamera(70, 16 / 9) camera.show_object(scene) controller = gfx.OrbitController(camera, register_events=renderer) state = {} def on_key_down(event): global state if event.key == "s": state = camera.get_state() elif event.key == "l": camera.set_state(state) elif event.key == "r": camera.show_object(scene) renderer.add_event_handler(on_key_down, "key_down") def animate(): for i, cube in enumerate(cubes): rot = la.quat_from_euler((0.005 * i, 0.01 * i), order="XY") cube.local.rotation = la.quat_mul(rot, cube.local.rotation) renderer.render(scene, camera) canvas.request_draw() if __name__ == "__main__": canvas.request_draw(animate) loop.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.928 seconds) .. _sphx_glr_download__gallery_introductory_orbit_camera.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: orbit_camera.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: orbit_camera.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: orbit_camera.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_ .. only:: html Interactive example ------------------- Try this example in your browser using Pyodide. Might not work with all examples and all devices. Check the output and your browser's console for details. .. raw:: html