When you make a button in a custom UI to call the operator with passing the necessary parameters to it, pay attention that only the parameters specified explicitly will be sent to the operator. Other operator parameters will remain with default values.
For example, we need to execute the “transform_apply” operator – applying object transformations with applying just the scale.
If we call the operator as follows:
bl_idname = 'TESTPANEL_PT_panel'
bl_label = 'Testpanel'
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_category = 'Testpanel'
def draw(self, context):
self.layout.operator('context.object.transform_apply', text='Apply Scale').scale = True
But all transformation (scale, rotation, and position) will still be applied to the object.
Continue reading “Setting operator parameters features”
Sometimes, to effectively distribute the hardware load it is necessary to make a render from Blender only on certain video cards of several of them.
To do this we can use a simple script in which specify the numbers of devices to be used for rendering.
Continue reading “Render from console only on the specified GPU devices”
The direction of a Bezier curve, visually indicated by its normals slope, can be checked by the indices of its points.
The Bezier curve points indices always ascending in the curve direction.
So, having two points on the curve we can get the direction through their indices:
bezier_spline = bpy.context.object.data.splines
p0 = bezier_spline.bezier_points
p1 = bezier_spline.bezier_points
p0_index = next(iter([point for point in bezier_spline.bezier_points.items() if point == p0]), None)
p1_index = next(iter([point for point in bezier_spline.bezier_points.items() if point == p1]), None)
direction = 'p0 to p1' if p0_index < p1_index else 'p1 to p0'
# p0 to p1
Sometimes it is necessary to save data for creating a mesh (its vertices and polygon indices arrays) to text, for example, for further use in a script or addon.
We can export the mesh to one of the open formats, for example, to *.obj, but if we need only its vertexes and polygons data, we can use the following simple script:
Continue reading “How to save mesh data to text”
Button click in basically connected with the operator calling in Blender user interface. However, some times actions, that need to be performed when a button is pressed, are quite simple and do not require a separate operator for them. And it makes no sense to fill a registered operators stack with a multitude of specific operators designed to perform one highly specialized function. It would be much more convenient to associate a button press with a separate function call but the Blender API allows to associate buttons only with an operator call.
To solve the problem of creating a separate operator for each button we can use the fact that the operator can be called with the input parameters.
Continue reading “Calling functions by pressing buttons in Blender custom UI”
When making your own custom operators, sometimes it is necessary to pass them certain values – execute operator with parameters.
The passed parameter must be defined as an operator property.
Continue reading “Executing operators with parameters”
The UV-map is directly linked to the mesh through meshloops. We can use that to transfer the selection from the UV-map to the mesh.
To transfer the selection from the UV-map to the mesh, we need to cycle through the mesh polygons, check which meshloops are selected and select the corresponding vertices on the mesh itself.
Continue reading “Transfer selection from the UV-map to mesh”
Code autocomplete greatly simplifies writing scripts or developing add-ons for Blender. One of the best autocomplete modules for today is developed by Nutti. Last updated 20190718.
The project is hosted on the author’s GitHub: https://github.com/nutti/fake-bpy-module
The modules are distributed via pip or as a pre-generated-modules. Author also provides a module generator with which you can assemble autocomplete modules yourself.
When we create a field on the add-on interface panel, the value of which changes something in the node tree, each time the user changes the field value the node tree recompiles. If the user changes the value in that field by holding and moving the mouse, too frequent node tree recompilation will cause Blender to hangs.
This problem can be solved using decorators for deferred updating of the node tree.
Code by Skarn.
Continue reading “Decorators for node tree updation delay”
Class for quick node creation by their type.
Code by Skarn.
def __init__( self
, node_tree : bpy.types.NodeTree
, x : float = -200
, y : float = -200
, delimeter : float = 300
self._x = x
self._y = y
self.tree = node_tree
self.delimeter = delimeter
for node in self.tree.nodes:
def add_node( self
, node_type : str
, node_name : str
, column : int
, row : int
, node_descr : str = ""
node = self.tree.nodes.new(node_type)
node.name = node_name
node.label = node_descr if node_descr else node_name
node.location = (self.delimeter * column + self._x, -self.delimeter * row + self._y)
Continue reading “Class for creating nodes by type”
The easiest way to hide and show rendering objects is to assign animation keys to them. To do this, move the cursor over the eye icon (visibility in the viewport) or camera (visibility when rendering) in the Outliner window, press the “i” key and then manage the created condition in the Graph Editor like the ordinary animation keys.
But this method is not always available. For example, we cannot assign visibility animation keys for collections, Blender will generate errors like:
“hide_viewport” property cannot be animated
“hide_render” property can not be animated
However, using the Blender Python API, we can control the visibility of such objects.
Continue reading “Changing objects visibility in the viewport and while rendering”
Nutti, the author of the “fake-bpy-modules” project, has made the installation of the Blender Python API autocomplete modules through the pip platform. Pip installation is faster and easier, but sometimes we just need to copy the autocomplete modules to our project but now they are not included in the Nutti’s GitHub.
Copies of the autocomplete modules for Blender versions 2.79 and 2.80 can be downloaded directly from here: https://github.com/Korchy/blender_autocomplete
To get the vertex coordinates in the scene global coordinate system when the object’s scale was not applied, we need to multiply the local vertex coordinates by the object world transformation matrix:
object = bpy.data.objects['_MY_OBJECT_']
v_local = object.data.vertices[_VERT_NUMBER_].co # local vertex coordinate
v_global = object.matrix_world @ v_local # global vertex coordinates
To quickly create a color mask by materials, assigned to the scene objects, for post-processing
you can use the following script:
Continue reading “Color mask by scene materials”
When developing add-ons it is often necessary to give an ability to set a number of parameters that affect the whole add-on work to the user. For example, the user can specify a directory for saving/loading files, set some default variables or switch between add-on modes. Of course, the interface for setting such parameters can be placed in the add-on panel, but it is better to place it in a separate add-on preferences panel, which is located in the “Preferences” window under the add-on installation panel.
The main advantage of the add-on preferences is that they don’t reset when Blender restarts. The user does not need to configure the add-on preferences each time, it’s enough to set the necessary parameters once, personalizing the add-on for convenient work.
Let’s create an add-on and define a parameter, placing it in the add-on preferences panel.
Continue reading “Add-on preferences panel”
To find what Python interpreter version is used in current Blender version type the following commands in Python Console window in Blender:
# sys.version_info(major=3, minor=7, micro=0, releaselevel='final', serial=0)
It means that the version of Python used in Blender is 3.7.0.
To make it more readable type the following command:
or with full info:
# 3.7.0 (default, Aug 26 2018, 16:05:01) [MSC v.1900 64 bit (AMD64)]
In the latest version 2.8 of Blender developers have made many changes in API, so all the scripts and add-ons written for earlier Blender versions (2.7 and below) have stopped working. To run your add-ons in the new Blender 2.8, you need to port them – correct their code to work properly with the new Blender API.
To enable your add-on in Blender 2.80 you have to make the following changes in code:
Continue reading “Porting add-on from Blender 2.7 to Blender 2.8”
Blender has its own built-in text editor for writing scripts and add-ons, but it’s much convenient to develop them in external IDEs that provide the user with more features such as autocomplete, syntax highlighting, integration with version control systems and other tools that make development faster and easier.
One of these IDEs is Visual Studio Code from Microsoft. This is a free universal environment that supports development with various programming languages, including the Blender API language – Python.
Continue reading “Using Microsoft Visual Studio Code as external IDE for writing Blender scripts/add-ons”
In general, the “loop” is usually a sequential selection of several points, edges or polygons of a mesh.
However, there is an element in the mesh structure, which is also called a “loop”. It is a combination of one vertex with one edge of the mesh. Let’s try to learn what these “loops” are for.
Continue reading “Learning loops”
When starting Blender from the console it processes all parameters passed through the command line. However, some scripts and add-ons for proper work may require specifying their unique command line arguments. If you specify such additional parameters in the command line, Blender will also try to process them, which is likely to result in an error. Blender provides a special way to exclude such arguments from own processing.
Continue reading “How to pass command line arguments to a Blender python script or add-on”
State switches so-called “radio buttons” are used in the case to limit the choice by one value from several available ones. There are a lot of such buttons in the Blender interface, for example, switching between RGB and BW rendering modes or setting the texture mapping mode. Such buttons can be created in the Blender add-ons interface too.
Let’s create our own radio button switcher.
Continue reading “Creating radio buttons in the Blender add-ons interface”
Single add-on or script can contain several different operators, and not all of them may be registered in the API by the register() function. To verify that the required operator is registered in the Blender API, run the following command:
_operator_bl_idname_ – the text value of the bl_idname operator property.
For example for an operator:
bl_idname = 'test.operator'
bl_label = 'Test operator'
def execute(self, context):
the command will look like this:
To start working every Blender add-on must be registered by setting up the checkbox before add-on name in the User Preferences window – Add-ons page.
To programmatically find out if the required add-on is registered, run the following command:
'add-on_name' in the file bpy.context.user_preferences.addons
add-on_name – the name of the add-on file (without the .py extension) or the name of the add-on package, if it was installed from the package.
Blender add-ons installation and uninstallation can be managed through the python API. Use the following commands in a script:
To deactivate the add-on:
bpy.ops.wm.addon_disable(module = 'add-on name')
To uninstall the add-on:
bpy.ops.wm.addon_remove(module = 'add-on name')
Continue reading “How to install and uninstall Blender add-on via python API”
New mesh Custom Property can be created through the Blender python API by executing the following code:
bpy.data.objects['object_name']['property_name'] = property_value
- object_name – name of the mesh
- property_name – new custom property name
- property_value – value of this new property
After executing this command, the new property will be created and will be available in the Properties window – Object panel – Custom Properties sub-panel.
Like any object custom properties, the created property has a number of parameters that can be accessed by clicking the Edit button. These parameters can also be changed through the API.
Continue reading “Mesh Custom Properties editing through the Blender API”