Cleanup: Alembic, simplify material assignment code
Refactor material assignment code such that: - `build_mat_map()` just returns the built map (instead of relying on modifying a map passed as parameter), - `LISTBASE_FOREACH` is used to loop over a `ListBase` (instead of a hand-crafted for-loop), - just `return` when not enough material slots can be created (instead of setting a boolean to false, then doing some useless work, then checking the boolean), - reorder some code for clarity, and - rename `mat_map` to `matname_to_material` so that the semantics are clearer. No functional changes.
This commit is contained in:
parent
c32a5ce17c
commit
047819e728
|
@ -33,6 +33,7 @@
|
||||||
#include "DNA_object_types.h"
|
#include "DNA_object_types.h"
|
||||||
|
|
||||||
#include "BLI_compiler_compat.h"
|
#include "BLI_compiler_compat.h"
|
||||||
|
#include "BLI_listbase.h"
|
||||||
#include "BLI_math_geom.h"
|
#include "BLI_math_geom.h"
|
||||||
|
|
||||||
#include "BKE_main.h"
|
#include "BKE_main.h"
|
||||||
|
@ -70,55 +71,44 @@ namespace blender::io::alembic {
|
||||||
/* Some helpers for mesh generation */
|
/* Some helpers for mesh generation */
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
|
||||||
static void build_mat_map(const Main *bmain, std::map<std::string, Material *> &mat_map)
|
static std::map<std::string, Material *> build_material_map(const Main *bmain)
|
||||||
{
|
{
|
||||||
Material *material = static_cast<Material *>(bmain->materials.first);
|
std::map<std::string, Material *> mat_map;
|
||||||
|
LISTBASE_FOREACH (Material *, material, &bmain->materials) {
|
||||||
for (; material; material = static_cast<Material *>(material->id.next)) {
|
|
||||||
mat_map[material->id.name + 2] = material;
|
mat_map[material->id.name + 2] = material;
|
||||||
}
|
}
|
||||||
|
return mat_map;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void assign_materials(Main *bmain,
|
static void assign_materials(Main *bmain,
|
||||||
Object *ob,
|
Object *ob,
|
||||||
const std::map<std::string, int> &mat_index_map)
|
const std::map<std::string, int> &mat_index_map)
|
||||||
{
|
{
|
||||||
bool can_assign = true;
|
std::map<std::string, int>::const_iterator it;
|
||||||
std::map<std::string, int>::const_iterator it = mat_index_map.begin();
|
for (it = mat_index_map.begin(); it != mat_index_map.end(); ++it) {
|
||||||
|
|
||||||
int matcount = 0;
|
|
||||||
for (; it != mat_index_map.end(); ++it, matcount++) {
|
|
||||||
if (!BKE_object_material_slot_add(bmain, ob)) {
|
if (!BKE_object_material_slot_add(bmain, ob)) {
|
||||||
can_assign = false;
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO(kevin): use global map? */
|
std::map<std::string, Material *> matname_to_material = build_material_map(bmain);
|
||||||
std::map<std::string, Material *> mat_map;
|
|
||||||
build_mat_map(bmain, mat_map);
|
|
||||||
|
|
||||||
std::map<std::string, Material *>::iterator mat_iter;
|
std::map<std::string, Material *>::iterator mat_iter;
|
||||||
|
|
||||||
if (can_assign) {
|
for (it = mat_index_map.begin(); it != mat_index_map.end(); ++it) {
|
||||||
it = mat_index_map.begin();
|
const std::string mat_name = it->first;
|
||||||
|
const int mat_index = it->second;
|
||||||
|
|
||||||
for (; it != mat_index_map.end(); ++it) {
|
Material *assigned_mat;
|
||||||
std::string mat_name = it->first;
|
mat_iter = matname_to_material.find(mat_name);
|
||||||
mat_iter = mat_map.find(mat_name);
|
if (mat_iter == matname_to_material.end()) {
|
||||||
|
assigned_mat = BKE_material_add(bmain, mat_name.c_str());
|
||||||
Material *assigned_mat;
|
matname_to_material[mat_name] = assigned_mat;
|
||||||
|
|
||||||
if (mat_iter == mat_map.end()) {
|
|
||||||
assigned_mat = BKE_material_add(bmain, mat_name.c_str());
|
|
||||||
mat_map[mat_name] = assigned_mat;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
assigned_mat = mat_iter->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
BKE_object_material_assign(bmain, ob, assigned_mat, it->second, BKE_MAT_ASSIGN_OBDATA);
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
assigned_mat = mat_iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
BKE_object_material_assign(bmain, ob, assigned_mat, mat_index, BKE_MAT_ASSIGN_OBDATA);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue