Some Blender operators can only be called from the workspace for which they are intended.
If you call from the Python console, for example, an operator intended only for working in the 3D Viewport area, the operator will not be executed:
Python
1
2
3
bpy.ops.wm.toolbar()
# {'CANCELLED'}
or it will fail with an error message about the incorrect context:
Python
1
2
3
bpy.ops.view3d.background_image_add()
# RuntimeError: Operator bpy.ops.view3d.background_image_add.poll() failed, context is incorrect
However, we can still execute operators from a non-native working area. To do this, we can pass the first implicit parameter to any operator – a pointer to the context of the area in which this operator should be executed. This parameter commonly named the “overridden context”.
To switch the active tool from the T-panel in the 3D viewport window, we need to call the appropriate operator and pass the “idname” of the required tool in its “name” parameter.
For example, to enable the “Select Circle” selection toll, we need to call:
Modules used in Blender scripts and add-ons are located in the same directory as the script or add-on, or in the Blender installation directory by the “blender_version\python\lib\” path. However, it is possible to load a module located elsewhere on the hard drive.
To do this, we need to load the module by its absolute path.
To accurately align the bone of one armature with the bone of another armature, we need to correlate the global armature matrices and the local pose-bone matrices.
Most often, for quick simplified calculations with the object’s geometry, their Bounding Boxes are used – the minimum parallelepiped into which this object is inscribed. But sometimes the Bounding Sphere – the minimum sphere into which an object can be inscribed – can provide greater accuracy and simplify the calculations. While the location and size of the object’s Bounding Box is available in Blender at once, the Bounding Sphere we need to calculate manually
Let’s write a function that, based on the object list, returns the coordinates of the center of their Bounding Sphere and its radius.
An attempt to invert the transformation matrix with the inverted() method can be failed if, for example, some constraint affects the object for which the initial matrix is calculated.
In this case, Blender will generate an error:
ValueError: Matrix.invert(ed): matrix does not have an inverse