|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
|
|
|
|
|
Addon Tutorial
|
|
|
|
|
##############
|
|
|
|
|
Add-on Tutorial
|
|
|
|
|
###############
|
|
|
|
|
|
|
|
|
|
************
|
|
|
|
|
Introduction
|
|
|
|
@ -36,6 +36,7 @@ Suggested reading before starting this tutorial.
|
|
|
|
|
To best troubleshoot any error message Python prints while writing scripts you run blender with from a terminal,
|
|
|
|
|
see :ref:`Use The Terminal <use_the_terminal>`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Documentation Links
|
|
|
|
|
===================
|
|
|
|
|
|
|
|
|
@ -46,51 +47,48 @@ While going through the tutorial you may want to look into our reference documen
|
|
|
|
|
- :mod:`bpy.context` api reference. -
|
|
|
|
|
*Handy to have a list of available items your script may operate on.*
|
|
|
|
|
- :class:`bpy.types.Operator`. -
|
|
|
|
|
*The following addons define operators, these docs give details and more examples of operators.*
|
|
|
|
|
*The following add-ons define operators, these docs give details and more examples of operators.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
******
|
|
|
|
|
Addons
|
|
|
|
|
******
|
|
|
|
|
*******
|
|
|
|
|
Add-ons
|
|
|
|
|
*******
|
|
|
|
|
|
|
|
|
|
What is an Add-on?
|
|
|
|
|
==================
|
|
|
|
|
|
|
|
|
|
What is an Addon?
|
|
|
|
|
=================
|
|
|
|
|
|
|
|
|
|
An addon is simply a Python module with some additional requirements so Blender can display it in a list with useful
|
|
|
|
|
An add-on is simply a Python module with some additional requirements so Blender can display it in a list with useful
|
|
|
|
|
information.
|
|
|
|
|
|
|
|
|
|
To give an example, here is the simplest possible addon.
|
|
|
|
|
|
|
|
|
|
To give an example, here is the simplest possible add-on.
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
bl_info = {"name": "My Test Addon", "category": "Object"}
|
|
|
|
|
bl_info = {"name": "My Test Add-on", "category": "Object"}
|
|
|
|
|
def register():
|
|
|
|
|
print("Hello World")
|
|
|
|
|
def unregister():
|
|
|
|
|
print("Goodbye World")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- ``bl_info`` is a dictionary containing addon meta-data such as the title, version and author to be displayed in the
|
|
|
|
|
user preferences addon list.
|
|
|
|
|
- ``register`` is a function which only runs when enabling the addon, this means the module can be loaded without
|
|
|
|
|
activating the addon.
|
|
|
|
|
- ``unregister`` is a function to unload anything setup by ``register``, this is called when the addon is disabled.
|
|
|
|
|
- ``bl_info`` is a dictionary containing add-on metadata such as the title,
|
|
|
|
|
version and author to be displayed in the user preferences add-on list.
|
|
|
|
|
- ``register`` is a function which only runs when enabling the add-on,
|
|
|
|
|
this means the module can be loaded without activating the add-on.
|
|
|
|
|
- ``unregister`` is a function to unload anything setup by ``register``, this is called when the add-on is disabled.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Notice this add-on does not do anything related to Blender, (the :mod:`bpy` module is not imported for example).
|
|
|
|
|
|
|
|
|
|
Notice this addon does not do anything related to Blender, (the :mod:`bpy` module is not imported for example).
|
|
|
|
|
This is a contrived example of an add-on that serves to illustrate the point
|
|
|
|
|
that the base requirements of an add-on are simple.
|
|
|
|
|
|
|
|
|
|
This is a contrived example of an addon that serves to illustrate the point
|
|
|
|
|
that the base requirements of an addon are simple.
|
|
|
|
|
|
|
|
|
|
An addon will typically register operators, panels, menu items etc, but its worth noting that _any_ script can do this,
|
|
|
|
|
An add-on will typically register operators, panels, menu items etc, but its worth noting that _any_ script can do this,
|
|
|
|
|
when executed from the text editor or even the interactive console - there is nothing inherently different about an
|
|
|
|
|
addon that allows it to integrate with Blender, such functionality is just provided by the :mod:`bpy` module for any
|
|
|
|
|
add-on that allows it to integrate with Blender, such functionality is just provided by the :mod:`bpy` module for any
|
|
|
|
|
script to access.
|
|
|
|
|
|
|
|
|
|
So an addon is just a way to encapsulate a Python module in a way a user can easily utilize.
|
|
|
|
|
So an add-on is just a way to encapsulate a Python module in a way a user can easily utilize.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
@ -99,14 +97,14 @@ So an addon is just a way to encapsulate a Python module in a way a user can eas
|
|
|
|
|
Messages will be printed when enabling and disabling.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Your First Addon
|
|
|
|
|
================
|
|
|
|
|
Your First Add-on
|
|
|
|
|
=================
|
|
|
|
|
|
|
|
|
|
The simplest possible addon above was useful as an example but not much else.
|
|
|
|
|
This next addon is simple but shows how to integrate a script into Blender using an ``Operator``
|
|
|
|
|
The simplest possible add-on above is useful as an example but not much else.
|
|
|
|
|
This next add-on is simple but shows how to integrate a script into Blender using an ``Operator``
|
|
|
|
|
which is the typical way to define a tool accessed from menus, buttons and keyboard shortcuts.
|
|
|
|
|
|
|
|
|
|
For the first example we'll make a script that simply moves all objects in a scene.
|
|
|
|
|
For the first example we will make a script that simply moves all objects in a scene.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Write The Script
|
|
|
|
@ -130,13 +128,13 @@ Add the following script to the text editor in Blender.
|
|
|
|
|
:alt: Run Script button
|
|
|
|
|
|
|
|
|
|
Click the Run Script button, all objects in the active scene are moved by 1.0 Blender unit.
|
|
|
|
|
Next we'll make this script into an addon.
|
|
|
|
|
Next we will make this script into an add-on.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Write the Addon (Simple)
|
|
|
|
|
------------------------
|
|
|
|
|
Write the Add-on (Simple)
|
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
|
|
This addon takes the body of the script above, and adds them to an operator's ``execute()`` function.
|
|
|
|
|
This add-on takes the body of the script above, and adds them to an operator's ``execute()`` function.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
@ -173,7 +171,7 @@ This addon takes the body of the script above, and adds them to an operator's ``
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# This allows you to run the script directly from blenders text editor
|
|
|
|
|
# to test the addon without having to install it.
|
|
|
|
|
# to test the add-on without having to install it.
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
register()
|
|
|
|
|
|
|
|
|
@ -206,33 +204,33 @@ Do this by pressing :kbd:`Spacebar` to bring up the operator search dialog and t
|
|
|
|
|
|
|
|
|
|
The objects should move as before.
|
|
|
|
|
|
|
|
|
|
*Keep this addon open in Blender for the next step - Installing.*
|
|
|
|
|
*Keep this add-on open in Blender for the next step - Installing.*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Install The Addon
|
|
|
|
|
-----------------
|
|
|
|
|
Install The Add-on
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
Once you have your addon within in Blender's text editor,
|
|
|
|
|
Once you have your add-on within in Blender's text editor,
|
|
|
|
|
you will want to be able to install it so it can be enabled in the user preferences to load on startup.
|
|
|
|
|
|
|
|
|
|
Even though the addon above is a test, lets go through the steps anyway so you know how to do it for later.
|
|
|
|
|
Even though the add-on above is a test, lets go through the steps anyway so you know how to do it for later.
|
|
|
|
|
|
|
|
|
|
To install the Blender text as an addon you will first have to save it to disk, take care to obey the naming
|
|
|
|
|
To install the Blender text as an add-on you will first have to save it to disk, take care to obey the naming
|
|
|
|
|
restrictions that apply to Python modules and end with a ``.py`` extension.
|
|
|
|
|
|
|
|
|
|
Once the file is on disk, you can install it as you would for an addon downloaded online.
|
|
|
|
|
Once the file is on disk, you can install it as you would for an add-on downloaded online.
|
|
|
|
|
|
|
|
|
|
Open the user :menuselection:`File -> User Preferences`,
|
|
|
|
|
Select the *Addon* section, press *Install Addon...* and select the file.
|
|
|
|
|
Open the user :menuselection:`File --> User Preferences`,
|
|
|
|
|
Select the *Add-on* section, press *Install Add-on...* and select the file.
|
|
|
|
|
|
|
|
|
|
Now the addon will be listed and you can enable it by pressing the check-box,
|
|
|
|
|
Now the add-on will be listed and you can enable it by pressing the check-box,
|
|
|
|
|
if you want it to be enabled on restart, press *Save as Default*.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
The destination of the addon depends on your Blender configuration.
|
|
|
|
|
When installing an addon the source and destination path are printed in the console.
|
|
|
|
|
You can also find addon path locations by running this in the Python console.
|
|
|
|
|
The destination of the add-on depends on your Blender configuration.
|
|
|
|
|
When installing an add-on the source and destination path are printed in the console.
|
|
|
|
|
You can also find add-on path locations by running this in the Python console.
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
@ -243,17 +241,17 @@ if you want it to be enabled on restart, press *Save as Default*.
|
|
|
|
|
`Directory Layout <https://www.blender.org/manual/getting_started/installing_blender/directorylayout.html>`_
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Your Second Addon
|
|
|
|
|
=================
|
|
|
|
|
Your Second Add-on
|
|
|
|
|
==================
|
|
|
|
|
|
|
|
|
|
For our second addon, we will focus on object instancing - this is - to make linked copies of an object in a
|
|
|
|
|
For our second add-on, we will focus on object instancing - this is - to make linked copies of an object in a
|
|
|
|
|
similar way to what you may have seen with the array modifier.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Write The Script
|
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
As before, first we will start with a script, develop it, then convert into an addon.
|
|
|
|
|
As before, first we will start with a script, develop it, then convert into an add-on.
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
@ -324,17 +322,17 @@ allows vectors to be multiplied by numbers and matrices.
|
|
|
|
|
|
|
|
|
|
If you are interested in this area, read into :class:`mathutils.Vector` - there are many handy utility functions
|
|
|
|
|
such as getting the angle between vectors, cross product, dot products
|
|
|
|
|
as well as more advanced functions in :mod:`mathutils.geometry` such as bezier spline interpolation and
|
|
|
|
|
as well as more advanced functions in :mod:`mathutils.geometry` such as Bézier Spline interpolation and
|
|
|
|
|
ray-triangle intersection.
|
|
|
|
|
|
|
|
|
|
For now we'll focus on making this script an addon, but its good to know that this 3D math module is available and
|
|
|
|
|
For now we will focus on making this script an add-on, but its good to know that this 3D math module is available and
|
|
|
|
|
can help you with more advanced functionality later on.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Write the Addon
|
|
|
|
|
---------------
|
|
|
|
|
Write the Add-on
|
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
The first step is to convert the script as-is into an addon.
|
|
|
|
|
The first step is to convert the script as-is into an add-on.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
@ -381,7 +379,7 @@ The first step is to convert the script as-is into an addon.
|
|
|
|
|
register()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Everything here has been covered in the previous steps, you may want to try run the addon still
|
|
|
|
|
Everything here has been covered in the previous steps, you may want to try run the add-on still
|
|
|
|
|
and consider what could be done to make it more useful.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -434,7 +432,7 @@ however the link above includes examples of more advanced property usage.
|
|
|
|
|
Menu Item
|
|
|
|
|
^^^^^^^^^
|
|
|
|
|
|
|
|
|
|
Addons can add to the user interface of existing panels, headers and menus defined in Python.
|
|
|
|
|
Add-ons can add to the user interface of existing panels, headers and menus defined in Python.
|
|
|
|
|
|
|
|
|
|
For this example we'll add to an existing menu.
|
|
|
|
|
|
|
|
|
@ -464,7 +462,7 @@ For docs on extending menus see: :doc:`bpy.types.Menu`.
|
|
|
|
|
Keymap
|
|
|
|
|
^^^^^^
|
|
|
|
|
|
|
|
|
|
In Blender addons have their own key-maps so as not to interfere with Blenders built in key-maps.
|
|
|
|
|
In Blender, add-ons have their own keymaps so as not to interfere with Blenders built in key-maps.
|
|
|
|
|
|
|
|
|
|
In the example below, a new object-mode :class:`bpy.types.KeyMap` is added,
|
|
|
|
|
then a :class:`bpy.types.KeyMapItem` is added to the key-map which references our newly added operator,
|
|
|
|
@ -502,7 +500,7 @@ this allows you to have multiple keys accessing the same operator with different
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
While :kbd:`Ctrl-Shift-Space` isn't a default Blender key shortcut, its hard to make sure addons won't
|
|
|
|
|
While :kbd:`Ctrl-Shift-Space` isn't a default Blender key shortcut, its hard to make sure add-ons won't
|
|
|
|
|
overwrite each others keymaps, At least take care when assigning keys that they don't
|
|
|
|
|
conflict with important functionality within Blender.
|
|
|
|
|
|
|
|
|
@ -606,14 +604,14 @@ After selecting it from the menu, you can choose how many instance of the cube y
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
Directly executing the script multiple times will add the menu each time too.
|
|
|
|
|
While not useful behavior, theres nothing to worry about since addons won't register them selves multiple
|
|
|
|
|
While not useful behavior, theres nothing to worry about since add-ons won't register them selves multiple
|
|
|
|
|
times when enabled through the user preferences.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Conclusions
|
|
|
|
|
===========
|
|
|
|
|
|
|
|
|
|
Addons can encapsulate certain functionality neatly for writing tools to improve your work-flow or for writing utilities
|
|
|
|
|
Add-ons can encapsulate certain functionality neatly for writing tools to improve your work-flow or for writing utilities
|
|
|
|
|
for others to use.
|
|
|
|
|
|
|
|
|
|
While there are limits to what Python can do within Blender, there is certainly a lot that can be achieved without
|
|
|
|
@ -636,7 +634,7 @@ Here are some sites you might like to check on after completing this tutorial.
|
|
|
|
|
*For more background details on Blender/Python integration.*
|
|
|
|
|
- `How to Think Like a Computer Scientist <http://interactivepython.org/courselib/static/thinkcspy/index.html>`_ -
|
|
|
|
|
*Great info for those who are still learning Python.*
|
|
|
|
|
- `Blender Development (Wiki) <http://wiki.blender.org/index.php/Dev:Contents>`_ -
|
|
|
|
|
- `Blender Development (Wiki) <https://wiki.blender.org/index.php/Dev:Contents>`_ -
|
|
|
|
|
*Blender Development, general information and helpful links.*
|
|
|
|
|
- `Blender Artists (Coding Section) <http://blenderartists.org/forum/forumdisplay.php?47-Coding>`_ -
|
|
|
|
|
*forum where people ask Python development questions*
|
|
|
|
|