Friday, November 30, 2012

Basic Operator

This entry covers scripting a basic Operator in Blender.  An operator essentially is an operation which can be performed in/on your project.  Some existing examples are Smooth Shading, or moving objects in the scene. The move operator is one of many ways of integrating custom functionality (or add-ons) into Blender.

For this example, I'll just show a conventional "Hello World" approach.  Below is an example of all the basic code required for a custom operator.



First, note that the class HelloWorldOperator is derived from a Blender base class bpy.types.Operator.  This will give you access to the base members bl_idname and bl_label, as well as the execute function.  In all cases, these fields should be assigned as shown above.

The execute function contains what you want your operator to do when the operator is called.  Note that the operator being called is different from this script being run.

To explain this a bit further, what will be performed if the script is run is the register function will be called.  The last lines of code if __name__ == "__main__": register() is what will be called if the script is run.  This will essentially connect our new operator to the Blender program.

Once this is done, the operator can be accessed through the Blender menu system.  For example, if you were to run the script above, then and only then could you go to the 3D viewport, and hit the [Spacebar], and type in the operator name "Hello World", and then it would be available (below) to run.


Upon running the operator, now the custom code within the operator will be performed.  Since we are just doing a print("Hello World"), the result can only be seen in the output console.

A great link that helped me learn a lot about developing an operator can be found here:  Creating a Blender Addon

No comments:

Post a Comment