Fix T100737: OBJ/USD import: imported object has no active material, material has 2 users

Fixes issues in importers written in C++ (T100737):

- Materials had one reference count too much. Affected Collada,
  Alembic, USD, OBJ importers, looks like "since forever".
- Active material index was not properly set on imported meshes.
  Regression since 3.3 (D15145). Affected Alembic, USD, OBJ. Note:
  now it sets the first material as the active one, whereas
  previously the last one was set as active. First one sounds more
  "intuitive" to me.

Reviewed By: Bastien Montagne
Differential Revision: https://developer.blender.org/D15831
This commit is contained in:
2022-09-01 20:38:56 +03:00
parent ad4dcfe227
commit f366d197db
5 changed files with 16 additions and 0 deletions

View File

@@ -26,6 +26,7 @@
#include "BLI_math_geom.h"
#include "BKE_attribute.h"
#include "BKE_lib_id.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_mesh.h"
@@ -93,6 +94,7 @@ static void assign_materials(Main *bmain,
mat_iter = matname_to_material.find(mat_name);
if (mat_iter == matname_to_material.end()) {
assigned_mat = BKE_material_add(bmain, mat_name.c_str());
id_us_min(&assigned_mat->id);
matname_to_material[mat_name] = assigned_mat;
}
else {
@@ -101,6 +103,9 @@ static void assign_materials(Main *bmain,
BKE_object_material_assign_single_obdata(bmain, ob, assigned_mat, mat_index);
}
if (ob->totcol > 0) {
ob->actcol = 1;
}
}
} /* namespace utils */

View File

@@ -743,6 +743,7 @@ bool DocumentImporter::writeMaterial(const COLLADAFW::Material *cmat)
const std::string &str_mat_id = cmat->getName().empty() ? cmat->getOriginalId() :
cmat->getName();
Material *ma = BKE_material_add(bmain, (char *)str_mat_id.c_str());
id_us_min(&ma->id);
this->uid_effect_map[cmat->getInstantiatedEffect()] = ma;
this->uid_material_map[cmat->getUniqueId()] = ma;

View File

@@ -4,6 +4,7 @@
#include "usd_reader_material.h"
#include "BKE_image.h"
#include "BKE_lib_id.h"
#include "BKE_main.h"
#include "BKE_material.h"
#include "BKE_node.h"
@@ -266,6 +267,7 @@ Material *USDMaterialReader::add_material(const pxr::UsdShadeMaterial &usd_mater
/* Create the material. */
Material *mtl = BKE_material_add(bmain_, mtl_name.c_str());
id_us_min(&mtl->id);
/* Get the UsdPreviewSurface shader source for the material,
* if there is one. */

View File

@@ -168,6 +168,9 @@ static void assign_materials(Main *bmain,
std::cout << "WARNING: Couldn't assign material " << it->first << std::endl;
}
}
if (ob->totcol > 0) {
ob->actcol = 1;
}
}
} // namespace utils

View File

@@ -296,6 +296,8 @@ static Material *get_or_create_material(Main *bmain,
const MTLMaterial &mtl = *materials.lookup_or_add(name, std::make_unique<MTLMaterial>());
Material *mat = BKE_material_add(bmain, name.c_str());
id_us_min(&mat->id);
ShaderNodetreeWrap mat_wrap{bmain, mtl, mat, relative_paths};
mat->use_nodes = true;
mat->nodetree = mat_wrap.get_nodetree();
@@ -319,6 +321,9 @@ void MeshFromGeometry::create_materials(Main *bmain,
}
BKE_object_material_assign_single_obdata(bmain, obj, mat, obj->totcol + 1);
}
if (obj->totcol > 0) {
obj->actcol = 1;
}
}
void MeshFromGeometry::create_normals(Mesh *mesh)