Executing operators with parameters
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.
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.
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.
To get a list of vertices from the vertex group by its name we can use the following code:
1 2 |
[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[0], bpy.data.meshes['Cube'].vertices[1], ... |
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.
Class for quick node creation by their type.
Code by Skarn.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class NodeTreeBuilder: 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 self.purge_tree() def purge_tree(self): for node in self.tree.nodes: self.tree.nodes.remove(node) 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) return node |
The following command returns the 3D View areas list with enabled Local View mode:
1 2 3 |
[area for area in bpy.context.screen.areas if area.type =='VIEW_3D' and area.spaces[0].local_view] # [bpy.data.screens['Layout']...Area] |
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
or
“hide_render” property can not be animated
However, using the Blender Python API, we can control the visibility of such objects.
Changing objects visibility in the viewport and while renderingRead More »
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:
1 2 3 |
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 |