A community-maintained Python framework for creating mathematical animations.
Feature plugins (#784)
Jason Villanuevac8c1f68cb35d
diff --git a/docs/source/changelog.rst b/docs/source/changelog.rstindex 2e18c49d2..2347eec92 100644--- a/docs/source/changelog.rst+++ b/docs/source/changelog.rst@@ -39,6 +39,12 @@ v0.1.1Changes since Manim Community release v0.1.0+Plugins+=======++#. Provided a standardized method for plugin discoverability, creation,+ installation, and usage. See the :ref:`documentation <plugins>`.+Fixes=====diff --git a/docs/source/installation.rst b/docs/source/installation.rstindex ec8ace490..47f7b9d6a 100644--- a/docs/source/installation.rst+++ b/docs/source/installation.rst@@ -16,6 +16,7 @@ Installing dependenciesinstallation/linuxinstallation/troubleshootinginstallation/for_dev+ installation/plugins.. _installing-manim:diff --git a/docs/source/installation/plugins.rst b/docs/source/installation/plugins.rstnew file mode 100644index 000000000..27e447ff2--- /dev/null+++ b/docs/source/installation/plugins.rst@@ -0,0 +1,104 @@+.. _plugins:++=======+Plugins+=======++Plugins are features that extend Manim's core functionality. Since Manim is+extensible and not everything belongs in its core, we'll go over how to+install, use, and create your own plugins.++.. note::++ The standard naming convention for plugins is to prefix the plugin with+ ``manim-``. This makes them easy for users to find on package+ repositories such as PyPI.++.. WARNING::++ The plugin feature is new and under active development. Expect updates+ for the best practices on installing, using, and creating plugins; as+ well as new subcommands/flags for ``manim plugins``++Installing Plugins+******************+Plugins can be easily installed via the ``pip``+command:++.. code-block:: bash++ pip install manim-*++After installing a plugin, you may use the ``manim plugins`` command to list+your available plugins, see the following help output:++.. code-block:: bash++ manim plugins -h+ usage: manim plugins -h -l++ Utility command for managing plugins++ optional arguments:+ -h, --help show this help message and exit+ -l, --list Lists all available plugins++ Made with <3 by the manim community devs++You can list plugins as such:++.. code-block:: bash++ manim plugins -l+ Plugins:+ • manim_plugintemplate++Using Plugins in Projects+*************************+Plugins specified in ``plugins/__init__.py`` are imported automatically by+manim's ``__init__.py``. As such, writing:++.. code-block:: python++ from manim import *++in your projects will import any of the plugins imported in+``plugins/__init__.py``.++By default, ``plugins/__init__.py`` is not provided; although, there are+plans to support subcommands that would manage this file. It is especially+useful to create this file for projects that involve usage of the same+plugins. Alternatively, you may manually specify the plugins in your project+scripts.++.. code-block:: python++ import manim_cool_plugin+ # or+ from manim_cool_plugin import feature_x, feature_y, ...++Creating Plugins+****************+Plugins are intended to extend Manim's core functionality. If you aren't sure+whether a feature should be included in Manim's core, feel free to ask over+on the `Discord server <https://discord.gg/mMRrZQW>`_. Visit+`manim-plugintemplate <https://pypi.org/project/manim-plugintemplate/>`_+on PyPI.org which serves as an in-depth tutorial for creating plugins.++.. code-block:: bash++ pip install manim-plugintemplate++The only requirement of manim plugins is that they specify an entry point+with the group, ``"manim.plugins"``. This allows Manim to discover plugins+available in the user's environment. Everything regarding the plugin's+directory structure, build system, and naming are completely up to your+discretion as an author. The aforementioned template plugin, is only a model+using Poetry since this is the build system Manim uses. The plugin's `entry+point <https://packaging.python.org/specifications/entry-points/>`_ can be+specified in poetry as:++.. code-block:: toml++ [tool.poetry.plugins."manim.plugins"]+ "name" = "object_reference"\ No newline at end of filediff --git a/manim/__init__.py b/manim/__init__.pyindex df7322a69..fd34b97db 100644--- a/manim/__init__.py+++ b/manim/__init__.py@@ -86,3 +86,5 @@ from .utils.space_ops import *from .utils.strings import *from .utils.tex import *from .utils.tex_templates import *++from .plugins import *diff --git a/manim/__main__.py b/manim/__main__.pyindex 160fc4806..55858f033 100644--- a/manim/__main__.py+++ b/manim/__main__.py@@ -62,6 +62,14 @@ def main():else:logger.error("No subcommand provided; Exiting...")+ elif args.cmd == "plugins":+ from manim.plugins import plugins_flags++ if args.list:+ plugins_flags.list_plugins()+ elif not args.list:+ logger.error("No flag provided; Exiting...")+# elif args.cmd == "some_other_cmd":# something_else_here()diff --git a/manim/_config/main_utils.py b/manim/_config/main_utils.pyindex a977e50e6..970fc6004 100644--- a/manim/_config/main_utils.py+++ b/manim/_config/main_utils.py@@ -31,7 +31,8 @@ def _find_subcommand(args):"""subcmd = args[1]if subcmd in [- "cfg"+ "cfg",+ "plugins",# , 'init',]:return subcmd@@ -106,6 +107,8 @@ def parse_args(args):subcmd = _find_subcommand(args)if subcmd == "cfg":return _parse_args_cfg_subcmd(args)+ elif subcmd == "plugins":+ return _parse_args_plugins(args)# elif subcmd == some_other_future_subcmd:# return _parse_args_some_other_subcmd(args)elif subcmd is None:@@ -150,6 +153,26 @@ def _parse_args_cfg_subcmd(args):return parsed+def _parse_args_plugins(args):+ """Parse arguments of the form 'manim plugins <args>'."""+ parser = argparse.ArgumentParser(+ description="Utility command for managing plugins",+ prog="manim plugins",+ epilog="Made with <3 by the manim community devs",+ usage=("%(prog)s -h -l"),+ )++ parser.add_argument(+ "-l",+ "--list",+ action="store_true",+ help="Lists all available plugins",+ )+ parsed = parser.parse_args(args[2:])+ parsed.cmd = "plugins"+ return parsed++def _parse_args_no_subcmd(args):"""Parse arguments of the form 'manim <args>', when no command is present."""parser = argparse.ArgumentParser(@@ -157,7 +180,7 @@ def _parse_args_no_subcmd(args):prog="manim",usage=("%(prog)s file [flags] [scene [scene ...]]\n"- " %(prog)s {cfg,init} [opts]"+ " %(prog)s {cfg,init,plugins} [opts]\n"),epilog="Made with <3 by the manim community devs",)diff --git a/manim/plugins/.gitignore b/manim/plugins/.gitignorenew file mode 100644index 000000000..93f5256cd--- /dev/null+++ b/manim/plugins/.gitignore@@ -0,0 +1 @@+__init__.py\ No newline at end of filediff --git a/manim/plugins/plugins_flags.py b/manim/plugins/plugins_flags.pynew file mode 100644index 000000000..5905b3016--- /dev/null+++ b/manim/plugins/plugins_flags.py@@ -0,0 +1,29 @@+"""+plugins_flags.py+------------++Plugin Managing Utility.+"""++import pathlib+import pkg_resources++from manim import console++__all__ = ["list_plugins", "update"]+++def get_plugins():+ plugins = {+ entry_point.name: entry_point.load()+ for entry_point in pkg_resources.iter_entry_points("manim.plugins")+ }+ return plugins+++def list_plugins():+ console.print("[green bold]Plugins:[/green bold]", justify="left")++ plugins = get_plugins()+ for plugin in plugins:+ console.print(f" • {plugin}")