{ "cells": [ { "cell_type": "markdown", "id": "3d571a3d", "metadata": {}, "source": [ "# PyGfx picking example\n", "\n", "**Note that this example depends on pygfx (`pip install -U pygfx`).**\n", "\n", "An example demonstrating pickable points." ] }, { "cell_type": "code", "execution_count": 1, "id": "25eb8174", "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "230eee3142eb4aaea283797ec340240f", "version_major": 2, "version_minor": 0 }, "text/plain": [ "RFBOutputContext()" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "8297e2f5538643dc8082c5133392dabb", "version_major": 2, "version_minor": 0 }, "text/html": [ "
snapshot
" ], "text/plain": [ "PickingWgpuCanvas()" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "import pygfx as gfx\n", "from wgpu.gui.jupyter import WgpuCanvas\n", "\n", "\n", "class PickingWgpuCanvas(WgpuCanvas):\n", " def handle_event(self, event):\n", " super().handle_event(event)\n", " # Get a dict with info about the clicked location\n", " if event[\"event_type\"] == \"pointer_down\":\n", " xy = event[\"x\"], event[\"y\"]\n", " info = renderer.get_pick_info(xy)\n", " wobject = info[\"world_object\"]\n", " # If a point was clicked ..\n", " if wobject and \"vertex_index\" in info:\n", " i = round(info[\"vertex_index\"])\n", " geometry.positions.data[i, 1] *= -1\n", " geometry.positions.update_range(i)\n", " canvas.request_draw()\n", "\n", "\n", "canvas = PickingWgpuCanvas()\n", "renderer = gfx.renderers.WgpuRenderer(canvas)\n", "scene = gfx.Scene()\n", "\n", "xx = np.linspace(-50, 50, 10)\n", "yy = np.random.uniform(20, 50, 10)\n", "geometry = gfx.Geometry(positions=[(x, y, 0) for x, y in zip(xx, yy)])\n", "if True: # Set to False to try this for a line\n", " ob = gfx.Points(geometry, gfx.PointsMaterial(color=(0, 1, 1, 1), size=16))\n", "else:\n", " ob = gfx.Line(geometry, gfx.LineMaterial(color=(0, 1, 1, 1), thickness=10))\n", "scene.add(ob)\n", "\n", "camera = gfx.OrthographicCamera(120, 120)\n", "\n", "canvas.request_draw(lambda: renderer.render(scene, camera))\n", "canvas" ] }, { "cell_type": "code", "execution_count": null, "id": "21419674", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.9" } }, "nbformat": 4, "nbformat_minor": 5 }