Fix T96470 new obj exporter writing material groups
This is patch D14349 from Aras Pranckevicius. 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.
This commit is contained in:
parent
5bfdaaa800
commit
b9123b806f
Notes:
blender-bot
2023-03-17 15:06:21 +01:00
Referenced by issue #103824, output_node.inputs.new() crash blender Referenced by issue #96705, Regression: Crash when pressing F3 outside a Blender window if Developer extras is on Referenced by issue #96665, Subdivision modifier is broken in this AMD card Referenced by issue #96470, New .obj exporter: Material groups not being written Referenced by issue #96241, 3.1: Potential candidates for corrective releases Referenced by issue #98629, Float curve
|
@ -342,7 +342,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",
|
||||
|
|
|
@ -174,34 +174,14 @@ void OBJWriter::write_mtllib_name(const StringRefNull mtl_filepath) const
|
|||
fh.write_to_file(outfile_);
|
||||
}
|
||||
|
||||
void OBJWriter::write_object_group(FormatHandler<eFileType::OBJ> &fh,
|
||||
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) {
|
||||
fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name + "_" +
|
||||
object_material_name);
|
||||
}
|
||||
else {
|
||||
fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + object_mesh_name);
|
||||
}
|
||||
}
|
||||
|
||||
void OBJWriter::write_object_name(FormatHandler<eFileType::OBJ> &fh,
|
||||
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(fh, 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();
|
||||
fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + mesh_name);
|
||||
return;
|
||||
}
|
||||
fh.write<eOBJSyntaxElement::object_name>(object_name);
|
||||
|
@ -363,13 +343,14 @@ void OBJWriter::write_poly_elements(FormatHandler<eFileType::OBJ> &fh,
|
|||
buf.write<eOBJSyntaxElement::poly_usemtl>(MATERIAL_GROUP_DISABLED);
|
||||
}
|
||||
else {
|
||||
if (export_params_.export_object_groups) {
|
||||
write_object_group(buf, obj_mesh_data);
|
||||
}
|
||||
const char *mat_name = matname_fn(mat);
|
||||
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();
|
||||
fh.write<eOBJSyntaxElement::object_group>(object_name + "_" + mat_name);
|
||||
}
|
||||
buf.write<eOBJSyntaxElement::poly_usemtl>(mat_name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -66,10 +66,6 @@ class OBJWriter : NonMovable, NonCopyable {
|
|||
* Write object's name or group.
|
||||
*/
|
||||
void write_object_name(FormatHandler<eFileType::OBJ> &fh, const OBJMesh &obj_mesh_data) const;
|
||||
/**
|
||||
* Write an object's group with mesh and/or material name appended conditionally.
|
||||
*/
|
||||
void write_object_group(FormatHandler<eFileType::OBJ> &fh, const OBJMesh &obj_mesh_data) const;
|
||||
/**
|
||||
* Write file name of Material Library in .OBJ file.
|
||||
*/
|
||||
|
|
|
@ -203,11 +203,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;
|
||||
}
|
||||
|
||||
|
|
|
@ -511,4 +511,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
|
||||
|
|
Loading…
Reference in New Issue