{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Save a Movie Using Glyphs {#movie_glyph_example}\n\nCreate an animated GIF by generating glyphs using `glyph()\n<pyvista.DataSetFilters.glyph>`{.interpreted-text role=\"func\"} using\n`pyvista.Sphere`{.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 sphere glyphs\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "x = np.arange(-10, 10, 1, dtype=float)\ny = np.arange(-10, 10, 1, dtype=float)\nx, y = np.meshgrid(x, y)\nr = np.sqrt(x**2 + y**2)\nz = (np.sin(r) + 1) / 2\n\n# Create and structured surface\ngrid = pv.StructuredGrid(x, y, z)\ngrid.point_data['size'] = z.ravel()\n\n# generate glyphs with varying size\nsphere = pv.Sphere()\nspheres = grid.glyph(scale='size', geom=sphere, orient=False)\n\nspheres.plot(show_scalar_bar=False)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Create the movie\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Create a plotter object and set the scalars to the Z height\nplotter = pv.Plotter(notebook=False)\nplotter.add_mesh(\n    spheres,\n    show_edges=False,\n    show_scalar_bar=False,\n    clim=[0, 1],\n    cmap='bwr',\n)\n\n# Open a gif\nplotter.open_gif(\"glyph_wave.gif\")\n\n# Update Z and write a frame for each updated mesh\nnframe = 30\nfor phase in np.linspace(0, 2 * np.pi, nframe + 1)[:nframe]:\n    z = (np.sin(r + phase) + 1) / 2\n\n    # regenerate spheres\n    grid = pv.StructuredGrid(x, y, z)\n    grid.point_data['size'] = z.ravel()\n    new_spheres = grid.glyph(scale='size', geom=sphere, orient=False)\n\n    spheres.copy_from(new_spheres)\n\n    # Write a frame. This triggers a render.\n    plotter.write_frame()\n\n# Close and finalize the gif\nplotter.close()"
      ]
    }
  ],
  "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
}