Fix T96470 new obj exporter writing material groups

The logic in the code was _completely different_ from the documentation
and what the python exporter in 3.0 did. The new code assumed that
"export material groups" meant "append material name to the object name",
and was only ever kicking in when the "export object groups" option was
also checked. But the proper behavior (as in 3.0 exporter & the online docs),
is to emit g objectname_materialname before each usemtl line. Which is something entirely else.

Cherry picked from b9123b806f (D14349) with minor conflict fixes.
This commit is contained in:
Aras Pranckevicius 2022-03-20 08:59:16 -04:00 committed by Aras Pranckevicius
parent 9ed566efb6
commit 1781c6002a
Notes: blender-bot 2023-02-14 08:33:26 +01:00
Referenced by issue #96470, New .obj exporter: Material groups not being written
Referenced by issue #96241, 3.1: Potential candidates for corrective releases
5 changed files with 21 additions and 34 deletions

View File

@ -359,7 +359,7 @@ void WM_OT_obj_export(struct wmOperatorType *ot)
"export_material_groups",
false,
"Export Material Groups",
"Append mesh name and material name to object name, separated by a '_'");
"Generate an OBJ group for each part of a geometry using a different material");
RNA_def_boolean(
ot->srna,
"export_vertex_groups",

View File

@ -178,31 +178,13 @@ void OBJWriter::write_mtllib_name(const StringRefNull mtl_filepath) const
file_handler_->write<eOBJSyntaxElement::mtllib>(mtl_file_name);
}
void OBJWriter::write_object_group(const OBJMesh &obj_mesh_data) const
{
/* "o object_name" is not mandatory. A valid .OBJ file may contain neither
* "o name" nor "g group_name". */
BLI_assert(export_params_.export_object_groups);
if (!export_params_.export_object_groups) {
return;
}
const std::string object_name = obj_mesh_data.get_object_name();
const char *object_mesh_name = obj_mesh_data.get_object_mesh_name();
const char *object_material_name = obj_mesh_data.get_object_material_name(0);
if (export_params_.export_materials && export_params_.export_material_groups &&
object_material_name) {
file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name +
"_" + object_material_name);
return;
}
file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name);
}
void OBJWriter::write_object_name(const OBJMesh &obj_mesh_data) const
{
const char *object_name = obj_mesh_data.get_object_name();
if (export_params_.export_object_groups) {
write_object_group(obj_mesh_data);
const std::string object_name = obj_mesh_data.get_object_name();
const char *mesh_name = obj_mesh_data.get_object_mesh_name();
file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + mesh_name);
return;
}
file_handler_->write<eOBJSyntaxElement::object_name>(object_name);
@ -278,13 +260,14 @@ int16_t OBJWriter::write_poly_material(const OBJMesh &obj_mesh_data,
file_handler_->write<eOBJSyntaxElement::poly_usemtl>(MATERIAL_GROUP_DISABLED);
return current_mat_nr;
}
if (export_params_.export_object_groups) {
write_object_group(obj_mesh_data);
}
const char *mat_name = matname_fn(current_mat_nr);
if (!mat_name) {
mat_name = MATERIAL_GROUP_DISABLED;
}
if (export_params_.export_material_groups) {
const std::string object_name = obj_mesh_data.get_object_name();
file_handler_->write<eOBJSyntaxElement::object_group>(object_name + "_" + mat_name);
}
file_handler_->write<eOBJSyntaxElement::poly_usemtl>(mat_name);
return current_mat_nr;

View File

@ -65,10 +65,6 @@ class OBJWriter : NonMovable, NonCopyable {
* Write object's name or group.
*/
void write_object_name(const OBJMesh &obj_mesh_data) const;
/**
* Write an object's group with mesh and/or material name appended conditionally.
*/
void write_object_group(const OBJMesh &obj_mesh_data) const;
/**
* Write file name of Material Library in .OBJ file.
*/

View File

@ -205,11 +205,6 @@ const Material *OBJMesh::get_object_material(const int16_t mat_nr) const
*/
Object *obj = const_cast<Object *>(&export_object_eval_);
const Material *r_mat = BKE_object_material_get(obj, mat_nr + 1);
#ifdef DEBUG
if (!r_mat) {
std::cerr << "Material not found for mat_nr = " << mat_nr << std::endl;
}
#endif
return r_mat;
}

View File

@ -479,4 +479,17 @@ TEST_F(obj_exporter_regression_test, all_objects)
_export.params);
}
TEST_F(obj_exporter_regression_test, all_objects_mat_groups)
{
OBJExportParamsDefault _export;
_export.params.forward_axis = OBJ_AXIS_Y_FORWARD;
_export.params.up_axis = OBJ_AXIS_Z_UP;
_export.params.export_smooth_groups = true;
_export.params.export_material_groups = true;
compare_obj_export_to_golden("io_tests/blend_scene/all_objects.blend",
"io_tests/obj/all_objects_mat_groups.obj",
"io_tests/obj/all_objects_mat_groups.mtl",
_export.params);
}
} // namespace blender::io::obj