{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Picking Meshes {#mesh_picking_example}\n\nThis example demonstrates how to pick meshes using\n`enable_mesh_picking() <pyvista.Plotter.enable_mesh_picking>`{.interpreted-text\nrole=\"func\"}.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import pyvista as pv" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pick either a cube or a sphere using \\\"p\\\"\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "sphere = pv.Sphere(center=(1, 0, 0))\ncube = pv.Cube()\n\npl = pv.Plotter()\npl.add_mesh(sphere, color='r')\npl.add_mesh(cube, color='b')\npl.enable_mesh_picking()\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Deform the mesh after picking\n\nPick to trigger a callback that \\\"shrinks\\\" the mesh each time it\\'s\nselected.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def callback(mesh):\n \"\"\"Shrink the mesh each time it's clicked.\"\"\"\n shrunk = mesh.shrink(0.9)\n mesh.copy_from(shrunk) # make operation \"in-place\" by replacing the original mesh\n\n\npl = pv.Plotter()\npl.add_mesh(sphere, color='r')\npl.add_mesh(cube, color='b')\npl.enable_mesh_picking(callback=callback, show=False)\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pick based on Actors\n\nReturn the picked actor to the callback\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl = pv.Plotter()\npl.add_mesh(pv.Cone(center=(0, 0, 0)), name='Cone')\npl.add_mesh(pv.Cube(center=(1, 0, 0)), name='Cube')\npl.add_mesh(pv.Sphere(center=(1, 1, 0)), name='Sphere')\npl.add_mesh(pv.Cylinder(center=(0, 1, 0)), name='Cylinder')\n\n\ndef reset():\n for a in pl.renderer.actors.values():\n if isinstance(a, pv.Actor):\n a.prop.color = 'lightblue'\n a.prop.show_edges = False\n\n\ndef callback(actor):\n reset()\n actor.prop.color = 'green'\n actor.prop.show_edges = True\n\n\npl.enable_mesh_picking(callback, use_actor=True, show=False)\npl.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }