This is intended as a cleaner way to work with data and attributes on mesh objects in Blender. The goal is to help make importing and working with tabular datasets from outside of Blender as objects inside of blender a whole lot easier.
Not Stable
Active development and refinements are still ongoing. The main functions store_named_attribute and named_attribute are stable, but a lot of the other functionality is still in development. They provide the majority of the useful functionality.
databpy originally started as a submodule inside of Molecular Nodes but has been split off into it’s own package for use by other projects. Some internal code is still quite specific to the use case of Molecular Nodes, but the majority is more general.
Hello World
The ‘Hello World’ example is storing and retrieving data from a mesh objects inside of Blender.
While the bpy API is robust, it is a bit too verbose for what we are after, and there are many particulars that you can’t intuit (you have to store the value for floats, vector for 2 & 3 component vectors, but value for a Quaternion which is a 4-component vector).
import bpyimport numpy as npnp.random.seed(6)new_float_values = np.random.randn(8*3).reshape(-1, 3)obj = bpy.data.objects["Cube"]# create new attribute, then fill with data from a 1D numpy arrayatt = obj.data.attributes.new("test_float", "FLOAT_VECTOR", "POINT")att.data.foreach_set("vector", new_float_values.reshape(-1))# initialise empty array to fill, get data and then reshape to correct dimensionsempty_vector = np.zeros(len(att.data) *3, dtype=float)att.data.foreach_get("vector", empty_vector)empty_vector.reshape((-1, 3))
We can get and set values with single function calls. Data types are inferred from the numpy array data types, but can be manually specified. The point domain is the default domain, but domain can also be specified. See the AttributeDomains for more which domains can be chosen.
import databpy as dbdb.store_named_attribute(obj, new_float_values, "example_values")db.named_attribute(obj, "example_values")
Doing some common data-oriented operations on objects in Blender can be a bit of a pain, so databpy provides a BlenderObject class that wraps mesh objects and provides some convenience functions.
The most useful are the store_named_attribute and named_attribute functions that just work on the mesh object, so you can quickly get and set attributes with bob.
bob = db.BlenderObject(bpy.data.objects["Cube"])bob.store_named_attribute(new_float_values, "example_values")bob.named_attribute("example_values")