Page MenuHome

Support changing materials during evaluation.
ClosedPublic

Authored by Jacques Lucke (JacquesLucke) on May 12 2021, 11:55 AM.

Details

Summary

The goal of this patch is to allow that the evaluated geometry of an object has different materials from the original geometry. We need that for geometry nodes. For some more context see T88196 and T86737. I extracted this patch out of D11155 so that it can be reviewed separately.

The main thing that changes for render engines is that the number of material slots on the object and on the geometry might not match anymore.
For original data, the slot counts are still equal, but not for evaluated data.

The "ground truth" for the number of materials is the number of materials on the geometry now. This is important in the current design, because Eevee needs to know the number of materials just based on the mesh in mesh_render_mat_len_get (and similarly in other places).

To lookup the actual materials, I added Material *BKE_object_material_get_eval(Object *ob, short act) which is only meant to be used on evaluated objects. For convenience there is also BKE_object_material_count_eval.
Both functions are exposed through rna, so that Cycles can access the materials the same way.

In a few places I had to add a special case for mesh edit mode to get it to work properly.

Diff Detail

Repository
rB Blender

Event Timeline

Jacques Lucke (JacquesLucke) requested review of this revision.May 12 2021, 11:55 AM
Jacques Lucke (JacquesLucke) created this revision.
Brecht Van Lommel (brecht) requested changes to this revision.May 17 2021, 1:30 PM

I imagined that on evaluated objects, the regular material slot API would change behavior. Not to add a separate API for this, which add-on developers will forget to use.

Any reason a new API is needed?

This revision now requires changes to proceed.May 17 2021, 1:30 PM

@Brecht Van Lommel (brecht) are you only talking about the rna part or the rest as well?

As for the material_slots rna property the issue is mostly that Object.totcol and Object.mat does not contain the correct number of materials that are used by the geometry. Therefore, the iterator does not work in its current state. I'll check if I can refactor the iterator in a way so that it just works.

Yes, you'll need to replace totcol in RNA with a function that computes the number of slots.

  • Merge branch 'master' into refactor-material-handling
  • cleanup material slots array
  • remove old api

Just adding a function to replace totcol was not enough. I changed the iterator so that it uses CountIterator in CollectionPropertyIterator that you added a year ago for volume grids. Iterating over the indices seems to work better for this iterator.
Before, it would always get current material slot index by computing ptr.data - ob->mat which seems even more hacky when the object does not even have materials itself and ob->mat is null.

I'd potentially push this even further and hide this behind the BKE_object_material_* API. But that would require changes in more Blender code, to replace totcol usage. This seems like a step in the right direction.

This revision is now accepted and ready to land.May 17 2021, 7:56 PM