The camera angle in Blender can be set both in millimeters – the Focal Length parameter, and in degrees – the Field of View parameter.
To get the Field of View in degrees from the Focal Length value in millimeters, we can use the following formula:
Continue reading “How to get camera FOV in degrees from focal length in mm”
We can access vertex groups for the active mesh through its “vertex_groups” structure.
bpy . context . active_object . vertex_groups
Continue reading “How to create a Vertex Group and add vertices to it with the Blender Python API”
To make a new UV with the Python API and set coordinates to its points, we need:
First – create a new UV with the desired name:
new_uv = bpy . context . active_object . data . uv_layers . new ( name = 'NewUV' )
Next, to specify the coordinates of its points, we need to cycle through all the “loops” of the mesh:
Continue reading “How to create a new mesh UV with the Blender Python API”
You may need to get a list of coordinates of the mesh’s UV, for example, when exporting a mesh from Blender’s internal format to any external formats.
A list of the mesh UV-s can be got by accessing the “uv_layers” structure:
bpy . context . object . data . uv_layers [ : ]
# [bpy.data.meshes['Cube'].uv_layers["UVMap"], bpy.data.meshes['Cube'].uv_layers["UVMap.001"]]
Continue reading “How to get a list of UV coordinates”
To completely remove the object from the scene through the Blender Python API do the following:
Open the “Text Editor” window.
Continue reading “How to delete object from scene through the Blender Python API”
We can get a list of all available shader nodes by their type.
To start, add any object to the scene, for example, a cube, create material for it, and delete all nodes. Here, in this material, we will display all the shader nodes available in Blender.
Continue reading “How to show all available nodes”
To add an operator to the area header, we can use the “append” and “prepend” functions specifying in its parameters the drawing function of the operator call button.
For example, to add an operator of adding the default cube to the header of the viewport (3D View) area, we need to define the operator drawing function:
def cube_add_fnc ( self , context ) : self . layout . operator ( 'mesh.primitive_cube_add' , text = '' , icon = 'MESH_CUBE' )
and add it to the window header:
bpy . types . VIEW3D_HT_header . prepend ( cube_add_fnc )
The operator button will appear in the header of the viewport window.
Now we can view a list of all the functions that add operators to the area header by the “_draw_funcs” property of the “draw” method.
bpy . types . VIEW3D_HT_header . draw . _draw _funcs # [<function cube_add_fnc at 0x000000000FC8B8B8>, <function VIEW3D_HT_header.draw at 0x000000000F9C0678>, <function draw_pause at 0x0000000011633A68>]
Note that the “_draw_funcs” property would be defined only if custom operators have been added to the area header. If custom operators were not added to the header, an error will be thrown:
AttributeError: ‘function’ object has no attribute ‘_draw_funcs’
To create and add a camera to the scene with the Blender Python API we need to do the following:
1. Create a camera data-block
camera_data = bpy . data . cameras . new ( name = 'Camera' )
2. Create an object and link it with the camera date-block, we created
camera_object = bpy . data . objects . new ( 'Camera' , camera_data )
3. Add created camera-object to the scene
bpy . context . scene . collection . objects . link ( camera_object )
A new camera will be created in the current scene in its main collection.
camera_data = bpy . data . cameras . new ( name = 'Camera' ) camera_object = bpy . data . objects . new ( 'Camera' , camera_data ) bpy . context . scene . collection . objects . link ( camera_object )
The Blender Python API provides the ability to append or link objects from another *.blend file to the current scene using the “append” command.
To append an object (for example – a mesh) from another .blend file, we need to do the following:
Continue reading “How to append an object from another blend file to the scene using the Blender Python API”
Blender Python API autocomplete modules: fake-bpy-modules by Nutti update – added branch for Blender 2.90.
Last release: 2020.08.12.
Only files (without pip installation)
In scripts and add-ons for importing-exporting files with formats that are not supported in Blender by default, we must give the user an ability to select files using the “FileBrowser” interface.
To open the file browser window, and after the user selects the necessary file, to return the path to it, we need to use the “ImportHelper” and the “ExportHelper” classes.
Continue reading “Using the FileBrowser interface window to opening and saving files”
To add custom mesh to the scene through the Blender Python API we need to do the following:
Open the “Text Editor” window.
Continue reading “How to create mesh through the Blender Python API”
For add-ons developing, sometimes it is necessary to use packages that are not included in the core set of the Blender Python interpreter. Most often, the dependencies you need are included in the Python Package Index (PyPI) and can be installed to Blender through the Pip package management system.
The Pip Package Manager is already installed in Blender. However, Blender does not allow us to install the desired packages in a common way with the “pip install package_name” command.
Continue reading “How to install required packages to the Blender Python with pip”
Blender Python API autocomplete modules by Nutti update – added branch for Blender 2.83.
Last release: 2020.07.24.
Only files (without pip installation)
An example of using the NodeTree Source addon for creating a separate addon that builds complex node trees itself.
Blender 3D add-on for converting material nodes into python source code and storing it in the library.
Continue reading “Blender add-on: NodeTree Source”
For objects rotation, we can use the “rotation_euler” property. This property is a list of three items, each of them corresponds to the rotation angle around the X, Y, and Z-axis. The otation_euler contains the rotation angle around the X-axis, rotation_euler – around the Y-axis, and rotation_euler – around the Z-axis. To rotate an object we must set a rotation angle to the appropriate field of the property.
For example, to rotate an active object around the X-axis to the 90 degrees angle we must execute the following command:
import math bpy . context . active_object . rotation_euler [ 0 ] = math . radians ( 90 )
math.radians is used to convert degrees to radians.
The flat/smooth shading mode is regulated through the “use_smooth” property of each polygon of the mesh.
To enable smooth shading we need to set the “use_smooth” property of each mesh polygon to “True”.
For active object:
bpy . context . object . data . polygons . foreach_set ( 'use_smooth' , [ True ] * len ( bpy . context . object . data . polygons ) )
To enable flat shading – set the “use_smooth” property of each polygon to “False”.
bpy . context . object . data . polygons . foreach_set ( 'use_smooth' , [ False ] * len ( bpy . context . object . data . polygons ) )
To make new shading mode visible – force update mesh data:
bpy . context . object . data . update ( )
To switch to the view from the active camera we can execute the following command:
bpy . ops . view3d . view_camera ( )
This operator works in the toggle mode, so the next executing of the same command returns the view to the previous state.
Continue reading “Switching to the view from camera throug the python API”
To switch from a global coordinate system to a local coordinate system of an object, we need to multiply the global coordinates by the inverted matrix of an object:
object_matrix_inverted = copy . copy ( bpy . context . object . matrix_world ) object_matrix_inverted . invert ( ) cursor_location_local = object_matrix _inverted @ bpy . context . scene . cursor . location
To switch from the local coordinate system of the object to the global coordinate system, we need to multiply the local coordinates by the matrix of an object:
object_matrix = copy . copy ( bpy . context . object . matrix_world ) vertex_0 = bpy . context . object . data . vertices [ 0 ] . co vertex_0_global = object _matrix @ vertex_0
In Blender 2.8x the undo/redo operation system (ctrl+z / ctrl+shift+z) has been completely rebuilt. One of the results of this API change is when the undo operation is performed, all objects in the scene are fully recreated – the current object is destroyed, and a completely new object is created instead. Pointers to the old objects (before the undo operation) now point to invalid objects that can no longer be used.
Continue reading “Objects pointers brokes if undo/redo operation is used”
In Blender 2.81 and earlier, you could see the possible code completion by pressing “ctrl + space” in the python console. This key-shot shows nothing in Blender 2.82.
Code autocompletion in the python console was not removed in 2.82, it was moved to the “
Interface elements in custom user panels often do not correspond to each other in size. As a result – the overall panel layout does not look beautiful. As an example, let’s create a custom panel and place an operator button and an input text field on it.
Can we make it more accurate?
Continue reading “User interface elements alignment by columns”
To get a list of collections with all internal collections, we can use the following function:
def collections ( collection , col_list ) : col_list . append ( collection ) for sub_collection in collection . children : collections ( sub_collection , col_list )
Continue reading “How to get all collectons list”
Some times we need to make a render of the scene with the single material, for example, for clay or wireframe renders. Blender has the global materials override option for the Cycles render engine in the “View Layer Properties” – “Override” but not for the EEVEE render engine.
Quick materials override for the EEVEE rendering engine can be made with a small script, as shown in the following video:
Script and video by
Continue reading “Global materials override for the Blender EEVEE render engine”