{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Composite Picking {#composite_picking_example}\n\nDemonstrate how to pick individual blocks of a\n`pyvista.MultiBlock`{.interpreted-text role=\"class\"} using\n`pyvista.Plotter.enable_block_picking`{.interpreted-text role=\"func\"}.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n\nimport pyvista as pv"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Create a MultiBlock Dataset\n\nCreate 100 superellipsoids using\n`pyvista.ParametricSuperEllipsoid`{.interpreted-text role=\"func\"}\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "def make_poly():\n    \"\"\"Create a superellipsoid in a random location.\"\"\"\n    poly = pv.ParametricSuperEllipsoid(\n        n1=np.random.default_rng().random(),\n        n2=np.random.default_rng().random() * 2,\n        u_res=50,\n        v_res=50,\n    )\n    poly.points += np.random.default_rng().random(3) * 20\n    return poly\n\n\n# Assemble the multiblock and plot it using the default plotting settings\nblocks = pv.MultiBlock([make_poly() for _ in range(100)])\nblocks.plot()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Enable Block Picking\n\nAdd `blocks` to a `pyvista.Plotter`{.interpreted-text role=\"class\"} and\nenable block picking. For fun, let\\'s also enable physically based\nrendering and set the callback to set the block color to red when the\nblock is clicked and unset the color if the color has already been set\nfor the block.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl = pv.Plotter()\nactor, mapper = pl.add_composite(blocks, color=\"w\", pbr=True, metallic=True)\n\n\ndef callback(index, *args):\n    \"\"\"Change a block to red if color is unset, and back to the actor color if set.\"\"\"\n    if mapper.block_attr[index].color is None:\n        mapper.block_attr[index].color = \"r\"\n    else:\n        mapper.block_attr[index].color = None\n\n\npl.enable_block_picking(callback, side=\"left\")\npl.background_color = \"w\"\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
}