Featured
- Get link
- X
- Other Apps
Embracing Automation with Blender & Python
The Breakdown
This project started as one simple script with a UI panel that was meant to help me & the art team. The thing about embracing automations while modeling is that's you start noticing minor inefficiencies, and think "maybe I can reduce the steps here with a click". Next thing you know, you are are get a few requests & the lines of code grow at an exponential rate. What now? Well, as a solo developer considering sanity & time, building an entire plugin systematically would save me a lot of scrolling & brain power.
The Process
Clean architecture first. The entire plugin should be approached as Legos. I should be able to add or remove an action without breaking anything, focusing the ideas & no longer the system. If the script itself works when executed in Blender, a Class & UI Button for it can be built, followed by adding it into the plugin folder.
Scope, Modularity & Scalability
For me to break down the scope, we need to understand modularity & scalability, which covers the intent moving forward. What is modularity? Modularity in programming is a concept where developers separate program functions into independent pieces. These pieces then act like building blocks, with each block containing all the necessary parts to execute one aspect of functionality. This directly leads into scalability, which refers to the capability of a system, network, or process to handle a growing amount of work or its potential to accommodate growth.
Automated Actions
The entire project is divide into a free and paid version. The paid version builds of the free version, making working with one VS Code workspace, one branch (not recommended), and modules divided into two folders(that plugin folder with the __init__ packaging the entire thing & the experimental/idea folder) easier. If the experimental ideas work as a singular script ran by Blender, they make it into the plugin folder with their respective UI button. The scope focuses on an infinite amount of additions, while still being able to consistently deliver a minimum viable product. A flow free from a house of cards.Some automated actions include:
N-Pole Loops - Let's you find N-Pole all over the model then selects all the loops for them
N-Pole Bevel - Builds on "N-Pole Loops" then bevels them at a fixed value.
Smooth Verts - This let's you completely remove the smooth brush step for more control. You can select the specific vertices you want to smooth, then interpolates all vertices around relative to the smoothed vertex.
Batch Export - This one is a bit self-explanatory, but it let's you export all meshes as individual FBXs to your selected folder. There is an alternative operation that just exports everything into one FBX. Great option for when you want to texture models in bulk.
Understanding the Blender API
Consuming the Blender API was the easiest part, as everything felt very self-explanatory are years of modeling. The challenge was picking the versions of Blender I wanted to develop by casting a wide net with the little to no amount of deprecated functions between the Blender versions. Additionally considering dependencies was also another key factor, although having to enable "Loop Tools" for a particular function had to be an exception, it is a plugin that comes with Blender & can be enabled at any time.import bpyimport bmeshfrom bpy_extras.io_utils import ExportHelperfrom bpy.utils import register_class, unregister_class
bpy: This is Blender’s main Python module, and it gives your script access to nearly everything in Blender. bpy.data holds the scene’s objects, meshes and materials, bpy.context describes what’s currently selected or active, bpy.ops runs the same operators as the UI buttons, and bpy.types has the base classes for building add-ons. It only exists inside Blender’s own Python, so scripts that import it can’t run in a normal Python install.
bmesh: This module edits mesh geometry directly at the vertex, edge and face level. Instead of working through the mesh data Blender stores, you load the mesh into a BMesh, create or delete geometry, then write the result back. It’s the tool for procedural modelling, such as extruding faces, merging vertices or building a mesh from scratch, and it’s faster and more flexible than the equivalent operators. If you create a BMesh outside edit mode, remember to free it when you’re done.
ExportHelper: This is a helper class from bpy_extras.io_utils that you add to your own export operator. It supplies the file path property and opens Blender’s file browser, so users get a normal “Save As” dialog when they use your exporter. You set the file extension and a filter, and the rest is handled for you. This is what you’d use to make a custom export option appear under File > Export.
register_class and unregister_class: These are functions from bpy.utils that add your own classes (operators, panels, property groups, preferences) to Blender, and remove them again. A class only appears in the interface or does anything once it’s registered, which is why add-ons call register_class in their register() function and unregister_class in unregister(). Unregistering matters, because it lets users disable your add-on cleanly.
Seperation of Concerns & Unit/System Testing
Arguably the most important part during this process. I lightly touched on the process, but hammering the practice of SoC & Unit/System Testing was very vital in the tech check verification process. "Divide the entire operation, by ensuring the code works as intended, wrap it in a class then assign the class its button in the UI module, load it in the __init__ file, refresh the plugin in Blender, check if it works as intended with the button & make sure nothing is broken or UI rearranged.Code Examples:
Setting up the __init__ file:
What I Learned
Conclusion
Having started from one script that turned into one giant .py caught up with me fast, as it became difficult to maintain & build upon. Building from a strong foundation & avoiding incurring as much technical debt as possible certainly made my life a whole lot easier later on & wish I had started with solid architecture. Live & learn, I suppose.
Learning Resources
DevOps Fundamentals
Blender 2.93 API Documentation
Software Engineering Principles
GitHub
- Get link
- X
- Other Apps
Popular Posts
Converting GLSL to HLSL For Custom Nodes
- Get link
- X
- Other Apps
3D/Interactive Web Experiences: Blending Game & Software Development Principles
- Get link
- X
- Other Apps
Comments
Post a Comment