6
$\begingroup$

I'm trying to determine if it's possible to define custom attributes associated with vertices or edges of a mesh and then assign values to them manually as part of the mesh creation process, much like how edges can be (un)marked as sharp in edit mode, or vertices in groups can be assigned weights.

When initially skimming the user guide for 3.2 as a new user, it sounded like custom attributes provided exactly the ability I was looking for. But as best as I can tell, these named custom attributes exist only as locations to which geometry nodes can write procedurally calculated intermediate values. E.g., the Spreadsheet editor type displays any added custom attributes for a mesh and their values, and has separate views for "evaluated" and "original," but there seems to be no way in this or any other interface to assign values to the starting mesh.

Are custom attributes exclusively derived / procedural state, or is there a way (including through the Python API) to assign custom attribute values to mesh elements as part of the authoring process, similar to built-in properties like edge sharp/smooth? Are there any other Blender features suitable for assigning custom data manually to vertices / edges / etc. of a mesh?

$\endgroup$

1 Answer 1

7
$\begingroup$

Custom attributes can be set through the python API. For example, to add a float attribute to each vertex of the default cube:

mesh = bpy.context.collection.objects["Cube"].data
attribute = mesh.attributes.new(name="new attribute", type="FLOAT", domain="POINT")
attribute_values = [i for i in range(len(mesh.vertices))]
attribute.data.foreach_set("value", attribute_values)

You can access the attribute (or any other existing attribute) using its name, be careful when an attribute with the same name exists, Blender will choose a different name for the attribute, for example "new attribute.001"

mesh.attributes['new attribute']

in general it is a good idea to try not to rely on names for accessing properties

See docs for more details on the options

enter image description here

$\endgroup$
3
  • 1
    $\begingroup$ Has something changed in the API, or something like that? (Testing with 3.4 here) This does not seem to work for me, the foreach_set always fail, giving the error: RuntimeError: internal error setting the array, with this message: Error: Array length mismatch (expected 0, got 68) The attribute data is always a collection of 0 elements, and I can't use foreach_set to add values in it. $\endgroup$
    – JulienH
    Nov 2, 2022 at 14:59
  • 3
    $\begingroup$ Answering my own question above: The attribute data array will stay empty if you are in EDIT mode, so, don't forget to go in object mode before doign anything related to attributes. $\endgroup$
    – JulienH
    Nov 2, 2022 at 15:10
  • 1
    $\begingroup$ I found that the active attribute might change when going between object and edit mode. I think the position attribute throws it off $\endgroup$ Jul 20 at 0:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .