Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Blender Add-on Cookbook
Blender Add-on Cookbook
Blender Add-on Cookbook
Ebook299 pages51 minutes

Blender Add-on Cookbook

Rating: 0 out of 5 stars

()

Read preview

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.

LanguageEnglish
PublisherMichel Anders
Release dateMar 26, 2017
ISBN9781370025930
Blender Add-on Cookbook
Author

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

Related to Blender Add-on Cookbook

Related ebooks

Software Development & Engineering For You

View More

Related articles

Reviews for Blender Add-on Cookbook

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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 icon

    Who 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 icon

    Code 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 icon

    Conventions 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 icon

    Pythonic 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 icon

    About 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 icon

    Intro

    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 icon

    scene, object, base, object_utils

    explanation icon

    Explanation

    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.png

    class·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

    Enjoying the preview?
    Page 1 of 1