Blender Add-on Cookbook
()
About this ebook
A cookbook for Blender add-on developers who want to go one step further and want to add a professional touch to their creations or want to add functionality that isn't so straight forward to implement.
This book offers more than 30 examples of practical issues you may encounter when developing Blender add-ons. It gives you practical solutions with fully documented code samples, offers insight and advice based on years of developing add-ons and is fully illustrated.
Each recipe also comes with links to relevant reference sites and Blender API sections, and each code snippet comes with a small example add-on that can be downloaded from GitHub so you can simply test the given examples.
Michel Anders
Although a Blender user for over ten years, I have to admit that I am an enthusiastic but (very) mediocre artist at best. I discovered however that I really enjoyed helping people out with programming related questions and a couple of years ago when Packt Publishing was looking for authors on the BlenderArtists/Python forum I stepped in. So far this has resulted in several books in print: Blender 2.49 Scripting, ISBN 9781849510400 Published by Packt Publishing in April 2010. Python 3 Web Development, ISBN 9781849513746 Published by Packt Publishing in May 2011. Recently I switched to self publishing and my third book: 'Open Shading Language for Blender', distributed by Smashwords, was the first result. My latest book is called 'Creating add-ons for Blender' I maintain a blog on Blender related things, 'Small Blender Things' (blog.michelanders.nl) and I keep an eye on the coding forums at http://www.blenderartists.org/forum/ where you can also contact me via private message if you like, my nickname there is 'varkenvarken'. Recently I started offering some Blender add-ons on BlenderMarket, the first one called WeightLifter, a vertex group tool. I live in a small converted farm in the southeast of the Netherlands where we raise goats for a hobby. We also keep a few chickens and the general management of the farm is left to our cats. This arrangement leaves me with with enough time to write the occasional book.
Read more from Michel Anders
Creating add-ons for Blender Rating: 5 out of 5 stars5/5Open Shading Language for Blender Rating: 0 out of 5 stars0 ratings
Related to Blender Add-on Cookbook
Related ebooks
Blender 3D Incredible Machines Rating: 0 out of 5 stars0 ratingsExtending Unity with Editor Scripting Rating: 0 out of 5 stars0 ratingsBlender Compositing and Post Processing Rating: 0 out of 5 stars0 ratingsBlender Cycles: Lighting and Rendering Cookbook Rating: 0 out of 5 stars0 ratingsBlender 2.6 Cycles:Materials and Textures Cookbook Rating: 0 out of 5 stars0 ratingsBlender Cycles: Materials and Textures Cookbook - Third Edition Rating: 0 out of 5 stars0 ratingsBlender 3D By Example Rating: 4 out of 5 stars4/5Blender 2.5 Character Animation Cookbook Rating: 4 out of 5 stars4/5Blender Game Engine: Beginner’s Guide Rating: 3 out of 5 stars3/5Blender 2.5 Materials and Textures Cookbook Rating: 4 out of 5 stars4/5Blender 3D: Characters, Machines, and Scenes for Artists Rating: 0 out of 5 stars0 ratingsBlender 3D Basics Beginner's Guide Second Edition Rating: 5 out of 5 stars5/5Building a Game with Unity and Blender Rating: 0 out of 5 stars0 ratingsPapervision3D Essentials Rating: 0 out of 5 stars0 ratingsMEL Scripting for Maya Animators Rating: 0 out of 5 stars0 ratingsUnreal Engine Physics Essentials Rating: 1 out of 5 stars1/5Building Levels in Unity Rating: 0 out of 5 stars0 ratingsUnreal Development Kit Game Design Cookbook Rating: 0 out of 5 stars0 ratings3D Game Design with Unreal Engine 4 and Blender Rating: 0 out of 5 stars0 ratingsBlender 3D Cookbook Rating: 5 out of 5 stars5/5Blender For Dummies Rating: 0 out of 5 stars0 ratingsTexturing and Modeling: A Procedural Approach Rating: 0 out of 5 stars0 ratingsUnreal Engine Game Development Cookbook Rating: 0 out of 5 stars0 ratingsBlender 3D 2.49 Incredible Machines Rating: 0 out of 5 stars0 ratingsBlender 3D: Designing Objects Rating: 0 out of 5 stars0 ratingsGetting Started with General and Scanline Materials in 3ds Max 2017 Rating: 5 out of 5 stars5/5Google SketchUp for Game Design: Beginner's Guide Rating: 5 out of 5 stars5/5iClone 4.31 3D Animation Beginner's Guide Rating: 0 out of 5 stars0 ratings
Software Development & Engineering For You
Hand Lettering on the iPad with Procreate: Ideas and Lessons for Modern and Vintage Lettering Rating: 4 out of 5 stars4/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Level Up! The Guide to Great Video Game Design Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python For Dummies Rating: 4 out of 5 stars4/5Beginning Programming For Dummies Rating: 4 out of 5 stars4/5Lua Game Development Cookbook Rating: 0 out of 5 stars0 ratingsSQL For Dummies Rating: 0 out of 5 stars0 ratingsThe Essential Persona Lifecycle: Your Guide to Building and Using Personas Rating: 4 out of 5 stars4/5Managing Humans: Biting and Humorous Tales of a Software Engineering Manager Rating: 4 out of 5 stars4/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsiPhone Application Development For Dummies Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsTiny Python Projects: Learn coding and testing with puzzles and games Rating: 5 out of 5 stars5/5The Inmates Are Running the Asylum (Review and Analysis of Cooper's Book) Rating: 4 out of 5 stars4/5How Do I Do That In InDesign? Rating: 5 out of 5 stars5/5Diary of a Software Craftsman Rating: 5 out of 5 stars5/5Git Essentials Rating: 4 out of 5 stars4/5Agile: What You Need to Know About Agile Project Management, the Kanban Process, Lean Thinking, and Scrum Rating: 5 out of 5 stars5/5Wordpress 2023 A Beginners Guide : Design Your Own Website With WordPress 2023 Rating: 0 out of 5 stars0 ratingsAdobe Illustrator CC For Dummies Rating: 5 out of 5 stars5/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5How Do I Do That in Photoshop?: The Quickest Ways to Do the Things You Want to Do, Right Now! Rating: 4 out of 5 stars4/5Photoshop For Beginners: Learn Adobe Photoshop cs5 Basics With Tutorials Rating: 0 out of 5 stars0 ratingsAndroid App Development For Dummies Rating: 0 out of 5 stars0 ratingsBeginning C++ Programming Rating: 3 out of 5 stars3/5
Reviews for Blender Add-on Cookbook
0 ratings0 reviews
Book preview
Blender Add-on Cookbook - Michel Anders
Blender Add-on Cookbook
By Michel Anders
Copyright 2017 Michel Anders
Smashwords Edition
Smashwords Edition, License Notes
This ebook is licensed for your personal enjoyment only. This ebook may not be re-sold or given away to other people. If you would like to share this book with another person, please purchase an additional copy for each recipient. If you’re reading this book and did not purchase it, or it was not purchased for your use only, then please return to blendermarket.com and purchase your own copy. Thank you for respecting the hard work of this author
Introduction
In the last couple of year I have created numerous add-ons and while researching and developing these add-ons I encountered all sorts of different issues that where not always very straightforward to solve.
Blender has of course extensive documentation on its Python API and a helpful community as well but sometimes it nevertheless took quite some time to figure something out.
So I decided to document these issues and bundle then into this book, where you will find quite a collection of solutions to commonly occurring issues. Some are obvious once you see and understand them, others showcase subtleties that often prove to cause trouble in unexpected places if you are not aware of them.
I have done my best to provide not just solutions but accompany them with detailed explanations as well. Also, where appropriate I have included references to all sorts of documentation, often the most relevant spot in the Blender Python API docs, just to save you from an endless hunt for the right information. An index of terms is also provided.
Information
refs iconWho is this book for?
This book is for add-on developers who want to go one step further and add a professional touch to their creations or want to add functionality that isn't so straightforward to implement.
You should have a good understanding of Python and have some knowledge of writing Blender add-ons already. This is a cookbook in the sense that it provides small pieces of code that are aimed at solving a particular problem but if you want a more general introduction you could consider my book Creating add-ons for Blender.
code iconCode availability
The code in the book is presented as code snippets that will not run on their own. Typically just the execute() function of an operator is shown. However, to spare you the effort of typing over many lines of code, each recipe refers to a file that you can download and that implements a small but complete add-on that uses the code shown in the snippet, so you can test it out immediately.
All code examples are licensed under the GPL and available from this GitHub repository:
https://github.com/varkenvarken/blenderaddon-cookbook.
Each recipe contains individual references to the relevant code but you can also download all code a a single .zip file by clicking on the 'Clone or download' button and selecting 'Download as .zip':
All code is tested against the latest version of Blender (2.78a at time of writing)
refs iconConventions used in this book
Code snippets are shown as follows
def·myfunction(x): ····y·=·x·*·x ····return·y
Function or class names used in the running text are shown in bold: myfunction().
refs iconPythonic code
The recipes in this book focus on illustrating concepts relevant to writing Blender add-ons. We do strive to keep the code readable and Pythonic but not at all costs. Sometimes the limitations inherent in formating source code in books force us to sacrifice the recommendation in pep-8. Also, although we are aware of the distinctions, we use the terms function and method interchangeably. And as for values passed to functions we follow the Klingon definition.
refs iconAbout the author
Although a Blender user for over ten years, I have to admit that I am an enthusiastic but (very) mediocre artist at best. I discovered however that I really enjoyed helping people out with programming related questions and a couple of years ago when Packt Publishing was looking for authors on the BlenderArtists/Python forum I stepped in.
So far this has resulted in several books in print:
Blender 2.49 Scripting, ISBN 9781849510400, Published by Packt Publishing in April 2010.
Python 3 Web Development, ISBN 9781849513746, Published by Packt Publishing in May 2011.
Recently I switched to self publishing and my third book: 'Open Shading Language for Blender', distributed by Smashwords, major retailers and Blender Market, was the first result.
My latest book is called 'Creating add-ons for Blender' which gives a broader and more gentle introduction to Blender add-ons than this book.
I maintain a blog on Blender related things, 'Small Blender Things' (blog.michelanders.nl) and I keep an eye on the coding forums at http://www.blenderartists.org/forum/ where you can also contact me via private message if you like, my nickname there is 'varkenvarken'.
I also offer some Blender add-ons on BlenderMarket, the first one was called WeightLifter, a vertex group tool, but now accompanied by SpaceTree Pro, a environment aware tree modeler, and IDMapper, a tool to create ID-maps based on smart heuristics. If you like you can have a look at my Blender Market store to see what's on offer.
I live in a small converted farm in the southeast of the Netherlands where we raise goats for a hobby. We also keep a few chickens and the general management of the farm is left to our cats. This arrangement leaves me with with enough time to write the occasional book.
Adding an object to a scene
intro iconIntro
Many add-ons add an object to a scene and this functionality is so common that the Blender Python API provides us with several useful building blocks in the object_utils module to make your life easier.
In this recipe we will use the AddObjectHelper class and object_data_add() function to add a simple mesh object to the scene with a minimum amount of code. We will also take a look at some of the implementation details of these helper functions to gain a better insight in what is needed to add an object to a scene.
tags iconscene, object, base, object_utils
explanation iconExplanation
The provided extra functionality can be found in the bpy_extras.object_utils module and from here we explicitly import the AddObjectHelper class and the object_data_add() function
import·bpy from·bpy_extras.object_utils·import·AddObjectHelper from·bpy_extras.object_utils·import·object_data_add
By deriving our DummyOpObject class not just from Operator but also from the AddObjectHelper mix-in our operator will be automatically extended with properties to translate, move and align the new object, as is shown in the image below
screenshot-addobject.pngclass·DummyOpObject(bpy.types.Operator,·AddObjectHelper): ····bl_idname·=·'mesh.dummyopobject' ····bl_label·=·'Add·Object' ····bl_options·=·{'REGISTER',·'UNDO'}
The execute() method can now create a Mesh data object in the usual manner
····def·execute(self,·context): ········sqrt2·=·1.4142135623730951 ········verts·=·[[-1,-1,0],[1,-1,0],[1,1,0],[-1,1,0], ································[0,0,-sqrt2],[0,0,sqrt2]] ········faces·=·[[0,4,1],[1,4,2],[2,4,3],[3,4,0], ························[0,1,5],[1,2,5],[2,3,5],[3,0,5]] ········mesh·=·bpy.data.meshes.new(name=New·Object·Mesh
) ········mesh.from_pydata(verts,·[],·faces)
This Mesh data object can then be passed to the object_data_add() helper function that will take care of creating a new object with the mesh data, add it to the current scene and translate, rotate and align the object as specified in the properties
········object_data_add(context,·mesh,·operator=self) ········return·{FINISHED
}
If we have a look at how the object_data_add() function is defined inside the object_utils.py module we can understand what is needed to add an object to a scene
def·object_data_add(context,·obdata,·operator=None, ·····························use_active_layer=True,·name=None):
The function performs some additional actions like rotating and translating the new object as specified by the properties, which we don't show here, but in essence it deselects all objects in the scene and then creates a new object based on an optional name and the object data we have given it
... ····for·ob·in·scene.objects: ········ob.select·=·False ····if·name·is·None: ········name·=·Object
·if·obdata·is·None·else·obdata.name ····obj_new·=·bpy.data.objects.new(name,·obdata)
Blender can define more than one scene in a file and an object in Blender will not be present in a scene before it is linked. Such a link is called a base or base object and is what is returned by the link() method
····base·=·scene.objects.link(obj_new) ····base.select·=·True ... ····return·base
Of course you can implement all those steps in you own code as well, but if you want to add an object and what to offer additional functionality like rotation and alignment without any extra effort, the AddObjectHelper class and object_data_add() function are there to help you to simplify you code.
code icon