Attribute Node: refactor lookup to remove duplication between engines.

Currently lookup of Object and Instancer attributes is completely
duplicated between Cycles, Eevee and Eevee Next. This is bad design,
so this patch aims to deduplicate it by introducing a common API
in blenkernel.

In case of Cycles this requires certain hacks, but according to
Brecht it is planned to be rewritten later for more direct access
to internal Blender data anyway.

Differential Revision: https://developer.blender.org/D16117
This commit is contained in:
2022-09-30 18:54:26 +03:00
parent 42eda155df
commit 554afabf75
10 changed files with 155 additions and 215 deletions

View File

@@ -64,6 +64,14 @@ typedef struct DupliObject {
unsigned int random_id;
} DupliObject;
/** Look up the RGBA value of a uniform shader attribute.
* \return true if the attribute was found; if not, r_value is also set to zero. */
bool BKE_object_dupli_find_rgba_attribute(struct Object *ob,
struct DupliObject *dupli,
struct Object *dupli_parent,
const char *name,
float r_value[4]);
#ifdef __cplusplus
}
#endif

View File

@@ -58,6 +58,9 @@
#include "BLI_hash.h"
#include "NOD_geometry_nodes_log.hh"
#include "RNA_access.h"
#include "RNA_path.h"
#include "RNA_types.h"
using blender::Array;
using blender::float3;
@@ -1711,3 +1714,120 @@ void free_object_duplilist(ListBase *lb)
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Uniform attribute lookup
* \{ */
/** Lookup an arbitrary RNA property and convert it to RGBA if possible. */
static bool find_rna_property_rgba(PointerRNA *id_ptr, const char *name, float r_data[4])
{
if (id_ptr->data == nullptr) {
return false;
}
/* First, check custom properties. */
IDProperty *group = RNA_struct_idprops(id_ptr, false);
PropertyRNA *prop = nullptr;
if (group && group->type == IDP_GROUP) {
prop = (PropertyRNA *)IDP_GetPropertyFromGroup(group, name);
}
/* If not found, do full path lookup. */
PointerRNA ptr;
if (prop != nullptr) {
ptr = *id_ptr;
}
else if (!RNA_path_resolve(id_ptr, name, &ptr, &prop)) {
return false;
}
if (prop == nullptr) {
return false;
}
/* Convert the value to RGBA if possible. */
PropertyType type = RNA_property_type(prop);
int array_len = RNA_property_array_length(&ptr, prop);
if (array_len == 0) {
float value;
if (type == PROP_FLOAT) {
value = RNA_property_float_get(&ptr, prop);
}
else if (type == PROP_INT) {
value = static_cast<float>(RNA_property_int_get(&ptr, prop));
}
else if (type == PROP_BOOLEAN) {
value = RNA_property_boolean_get(&ptr, prop) ? 1.0f : 0.0f;
}
else {
return false;
}
copy_v4_fl4(r_data, value, value, value, 1);
return true;
}
if (type == PROP_FLOAT && array_len <= 4) {
copy_v4_fl4(r_data, 0, 0, 0, 1);
RNA_property_float_get_array(&ptr, prop, r_data);
return true;
}
if (type == PROP_INT && array_len <= 4) {
int tmp[4] = {0, 0, 0, 1};
RNA_property_int_get_array(&ptr, prop, tmp);
for (int i = 0; i < 4; i++) {
r_data[i] = static_cast<float>(tmp[i]);
}
return true;
}
return false;
}
static bool find_rna_property_rgba(ID *id, const char *name, float r_data[4])
{
PointerRNA ptr;
RNA_id_pointer_create(id, &ptr);
return find_rna_property_rgba(&ptr, name, r_data);
}
bool BKE_object_dupli_find_rgba_attribute(
Object *ob, DupliObject *dupli, Object *dupli_parent, const char *name, float r_value[4])
{
/* Check the dupli particle system. */
if (dupli && dupli->particle_system) {
ParticleSettings *settings = dupli->particle_system->part;
if (find_rna_property_rgba(&settings->id, name, r_value)) {
return true;
}
}
/* Check the dupli parent object. */
if (dupli_parent && find_rna_property_rgba(&dupli_parent->id, name, r_value)) {
return true;
}
/* Check the main object. */
if (ob) {
if (find_rna_property_rgba(&ob->id, name, r_value)) {
return true;
}
/* Check the main object data (e.g. mesh). */
if (ob->data && find_rna_property_rgba((ID *)ob->data, name, r_value)) {
return true;
}
}
copy_v4_fl(r_value, 0.0f);
return false;
}
/** \} */