Page MenuHome

Geometry: add .attributes in the Python API for Mesh, Hair and Point Cloud
ClosedPublic

Authored by Brecht Van Lommel (brecht) on Jul 3 2020, 7:37 PM.

Details

Summary

This puts all generic float/int/color/string geometry attributes in a new .attributes property. For meshes it provides a more general API for existing attributes, for point clouds attributes will be used as an essential part of particle nodes.

This patch was implemented by @Philipp Oeser (lichtwerk), with further changes by me. It's
still a work in progress, but posting here to show what is going on and for
early feedback.

Ref T76659

Diff Detail

Repository
rB Blender
Branch
temp-geometry-attributes (branched from master)
Build Status
Buildable 8870
Build 8870: arc lint + arc unit

Event Timeline

Brecht Van Lommel (brecht) requested review of this revision.Jul 3 2020, 7:37 PM
Brecht Van Lommel (brecht) created this revision.
Brecht Van Lommel (brecht) planned changes to this revision.Jul 3 2020, 7:37 PM
  • Add FloatColorAttribute and ByteColorAttribute distinction
  • Use CD_MASK_* to make some code more generic
Brecht Van Lommel (brecht) planned changes to this revision.Jul 6 2020, 2:26 PM
/home/jacques/blender-git/blender/source/blender/makesrna/intern/rna_attribute.c: In function ‘RNA_def_attribute’:
/home/jacques/blender-git/blender/source/blender/makesrna/intern/rna_attribute.c:604:3: error: implicit declaration of function ‘rna_def_vertex_color_group’ [-Werror=implicit-function-declaration]
  604 |   rna_def_vertex_color_group(brna);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~

Guess, I can just remove this line for testing.

In rna_attribute.c it says, that the PRIMITIVE domain in a point cloud are the points. However, in attribute.c, the VERTEX domain is used for points in a point cloud. It's probably best to change the description in rna_attribute.c.

I wonder if it would be good to splitup ATT_DOMAIN_PRIMITIVE into ATT_DOMAIN_POLYGON and ATT_DOMAIN_HAIR.

Also I'm not sure about the ATT prefix. Not sure if I've ever seen that as abbreviation for "Attribute". ATTR might work a bit better.

source/blender/blenkernel/BKE_attribute.h
58

Is there any reason for using int domain instead of AttributeDomain domain?

source/blender/blenkernel/intern/attribute.c
105

Might be good to assert that the layer is actually owned by this id.

170

Should return be reachable? Maybe put an BLI_assert(false) here. The same might be good in some other functions.

source/blender/makesdna/DNA_customdata_types.h
216

Should this replace CD_MASK_GENERIC_DATA?

source/blender/makesrna/intern/rna_attribute.c
419

I'm not sure if it is good to force the API user to access byte attributes as floats (at least I had some trouble with this in the past). It's not necessarily something we have to solve right now. Maybe another property like color_bytes could be added to ByteColorAttributeValue in the future that allows scripts to access the bytes directly, without conversion.

source/blender/makesrna/intern/rna_attribute.c
419

The issue really is the color space conversion, it's not possible to encode linear values properly in bytes, but we also don't want to burden scripts with this and have a clean API with only scene linear colors.

The alternative would be to simply remove byte storage altogether, but 4x memory usage is not great, especially if it's per-corner.

Something I would like to add to this API still is a Attribute.raw_data array, that would give a flat array of ints/bytes/floats. So I think we can support it that way, as a more low-level option for those who want to optimize things.

source/blender/makesrna/intern/rna_attribute.c
419

Yes, raw_data access sounds like a good-enough alternative.

Fix uploading the wrong patch.

  • Remove UI part from the patch
  • Various fixes and improvements
Brecht Van Lommel (brecht) retitled this revision from Python API: add unified .attributes for meshes, hair and pointclouds [WIP] to Geometry: add .attributes in the Python API for Mesh, Hair and Point Cloud.Aug 19 2020, 7:31 PM
Brecht Van Lommel (brecht) edited the summary of this revision. (Show Details)

I consider this ready for review now.

The only issue that has not been solved is unique naming across all domains. This is technically quite difficult to implement since our CustomData manipulation all happens per domain throughout mesh modelling tools and the modifier stack. Enforcing this reliably would probably require refactoring custom data storage as a whole. Some other applications (e.g. Houdini) also don't enforce this and have a priority order for domains.

So, I don't think this is something we have to solve now, if at all.

I posted 3 follow up patches as well, for the UI, storage of position and radius, and a spreadsheet view. Besides that still important to make this complete would be to support for rendering in Cycles and Eevee with these attributes, as well as Workbench visualization. And of course particle nodes integration.

LGTM, just some small comments. This can be merged without another round of review from me.

The only issue that has not been solved is unique naming across all domains. This is technically quite difficult to implement since our CustomData manipulation all happens per domain throughout mesh modelling tools and the modifier stack. Enforcing this reliably would probably require refactoring custom data storage as a whole. Some other applications (e.g. Houdini) also don't enforce this and have a priority order for domains.

Ok, I think I can live with that. When we define a priority order for domains, an attribute name alone should still be able to uniquely define an attribute in a given context.

source/blender/blenkernel/BKE_attribute.h
25

#pragma once

68

name -> new_name

source/blender/blenkernel/intern/attribute.c
104

This comment can be removed.

120

Should this say Attribute domain not supported by this id type?

278

Clang Tidy: readability-else-after-return

source/blender/makesrna/intern/rna_attribute.c
46

I wonder if we want to specify how a string property is stored. The max length seems to be 255 currently. We could also leave this unspecified for now or not expose it at all.

Do we actually use string data layers somewhere currently?

Brecht Van Lommel (brecht) marked 10 inline comments as done.Aug 28 2020, 5:02 PM
Brecht Van Lommel (brecht) added inline comments.
source/blender/makesrna/intern/rna_attribute.c
46

It's exposed in the Python API, but it seems to be barely used.

The implementation of this is quite poor, a fixed size of 255 wastes memory, it should really be using string interning.

I wouldn't mind removing this feature entirely, and bring it back later with a better implementation if there is a need for it.

@Campbell Barton (campbellbarton), do you have an opinion on this?

I'd vote for removing string attributes for now, but that can be done separately.

This revision is now accepted and ready to land.Sep 8 2020, 5:27 PM