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
Blender Python API autocomplete modules by Nutti update – added branch for Blender 2.81a.
Last release: 2020.01.11.
Author GitHub: https://github.com/nutti/fake-bpy-module
Only files (without pip installation) https://github.com/Korchy/blender_autocomplete
Working with mesh geometry, it may be necessary to assign each vertex some custom properties or data that must be written to the blend-file when saved, and which must be accessed later.
However, an attempt to assign the necessary data to the vertexes through the creation of custom properties fails. Instead of the custom vertex property, only a tuple with reference to the type of the property is created.
Continue reading “Assigning custom properties to vertexes”
A set of operators is provided in Blender for manipulating with the location of its interface windows.
To split the current window (using the current context) into two in a specified ratio, we need to execute the following operator:
- direction – set the splitting direction (‘HORIZONTAL’ or ‘VERTICAL’)
- factor – percentage ratio of splitting windows
Continue reading “How to split and join Blender interface windows thruough the python API”
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”
The Blender API provides a set of simple property types described in bpy.props (IntProperty, BoolProperty, etc.). But the basic types are not always enough, sometimes we need more complex ones. The Blender API allows to group simple properties to make complex properties.
Let’s consider such complex property creation and make the 3×3 matrix property as an example.
Continue reading “Custom property”
To get current Blender version number through the Python API we need to execute the following command:
# (2, 81, 16)
# '2.81 (sub 16)'
Continue reading “How to get current Blender version number through the Python API”
The Blender 2.80 API provides an ability to draw in a viewport (3D Viewport) using the “gpu” module.
As an example let’s draw a simple coordinate system widget in the center of the scene, consisting of three lines with different colors.
Continue reading “Drawing in Blender viewport”
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”
To correctly set values to an EnumProperty type we need to know all its possible entries.
If we try to set a value to an EnumProperty that is out of its available variants, Blender will throw an error:
TypeError: bpy_struct: item.attr = val: enum “xxx” not found in (‘value_001′, value_002’, …)
xxx – the value we tried to set to an EnumProperty
value_001, value_002, … – possible EnumProperty values
Continue reading “How to get a list with all possible values of an EnumProperty”
To translate cursor to the desired mesh vertex execute the following code:
bpy.context.scene.cursor.location = bpy.context.object.matrix_world @ bpy.context.object.data.vertices[_VERTEX_NUMBER_].co
Attention to the multiplication order – world matrix should be left.
In Blender 2.79 Python API we can add a new object to a scene with:
If we try to do this in Blender 2.80, it throws an error:
‘bpy_prop_collection’ object has no attribute ‘link’
In Blender 2.80 API new objects mast be linked not to the scene, but to the scene collections:
To link a new object to the active scene collection:
To link a new object to the collection by its name:
To move the cursor to the start position of the active strip in Blender VSE:
bpy.context.scene.frame_current = bpy.context.scene.sequence_editor.active_strip.frame_start
To move the cursor to the end position of the active strip:
bpy.context.scene.frame_current = bpy.context.scene.sequence_editor.active_strip.frame_final_end
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”
To get a list of vertices from the vertex group by its name we can use the following code:
[vert for vert in bpy.context.object.data.vertices if bpy.context.object.vertex_groups['vertex_group_name'].index in [i.group for i in vert.groups]]
# [bpy.data.meshes['Cube'].vertices, bpy.data.meshes['Cube'].vertices, ...
Bezier curves are drawing by four points: two main points (p0, p1) and two handle points (p0_hr, p1_hl).
To add an additional point p2 to a curve at a moment t, with t changes from 0 to 1 and represents the ratio of the p2 point location to the total length of the curve, we need to do the following:
Continue reading “Creating points on a Bezier curve”
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”
With the Blender popularity growing, the number of add-ons created for it by third-party developers is growing too. A lot of high-quality professional add-ons are written for Blender now. Over time, the number of add-ons is becoming more and more. And on this wave aggregators appeared – programs and services independently searching for add-ons and allowing Blender users to install add-ons quickly, many at once, and bypassing add-on distribution channels selected by their authors. What caused a negative reaction of add-on developers.
Continue reading “How to protect your add-on from downloading through aggregators”
The following command returns the 3D View areas list with enabled Local View mode:
[area for area in bpy.context.screen.areas if area.type =='VIEW_3D' and area.spaces.local_view]
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”