Monday, November 12, 2012

Python Macros

This entry covers the creation of Python Macros.  These are basically the same as python scripts, however these are much simpler scripts that encapsulate actions.    In this example we'll show how you can combine the action of adding a Sub-Surface Modifier along with the Smooth Shader operation.  An example macro can be seen below.



Before you can get started, you'll need to create a text data block by clicking New in your Text Editor view window.  The very first line (as show above) needs to be import bpy so that you can access all the Blender API functions you'll be referencing.


There are two ways that you can attain Blender actions for your macro (aside from learning the blender API).  The first of these two is to perform the action, then get the code from the info window.  This can be shown by dragging the window edge of the info panel down.  Any line in this view can be right-clicked to select, then [Ctrl+C] can be clicked to copy the text.  In your text editor view you can [Ctrl+V] to paste.

The second approach to getting actions is by hovering the mouse over an action user-interface element.  In this case, I'll hover over the Smooth button. Again you can now use [Ctrl+C] to copy the Python code, and past into your text editor view.

Now, after copying those two actions into the text editor, your code should look very similar to the first image in this entry.  If you add a monkey, and click Run Script [Alt+P], both the Sub-Surf and the Smooth Shader should be applied to your monkey instantly.

If you so desire, you can save your script in the Text Editor view by clicking Text > Save As, and giving the file a .py extension.

No comments:

Post a Comment