diff options
Diffstat (limited to 'source/blender/io/wavefront_obj')
12 files changed, 95 insertions, 29 deletions
diff --git a/source/blender/io/wavefront_obj/IO_wavefront_obj.h b/source/blender/io/wavefront_obj/IO_wavefront_obj.h index 0a92bbca477..cf6464eeb37 100644 --- a/source/blender/io/wavefront_obj/IO_wavefront_obj.h +++ b/source/blender/io/wavefront_obj/IO_wavefront_obj.h @@ -35,7 +35,7 @@ struct OBJExportParams { /* Geometry Transform options. */ eIOAxis forward_axis; eIOAxis up_axis; - float scaling_factor; + float global_scale; /* File Write Options. */ bool export_selected_objects; @@ -65,6 +65,7 @@ struct OBJImportParams { char filepath[FILE_MAX]; /** Value 0 disables clamping. */ float clamp_size; + float global_scale; eIOAxis forward_axis; eIOAxis up_axis; bool import_vertex_groups; diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc index 95be927589e..5c81cf7abca 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_file_writer.cc @@ -261,7 +261,7 @@ void OBJWriter::write_vertex_coords(FormatHandler &fh, BLI_assert(tot_count == attribute.size()); obj_parallel_chunked_output(fh, tot_count, [&](FormatHandler &buf, int i) { - float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.scaling_factor); + float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.global_scale); ColorGeometry4f linear = attribute.get(i); float srgb[3]; linearrgb_to_srgb_v3_v3(srgb, linear); @@ -270,7 +270,7 @@ void OBJWriter::write_vertex_coords(FormatHandler &fh, } else { obj_parallel_chunked_output(fh, tot_count, [&](FormatHandler &buf, int i) { - float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.scaling_factor); + float3 vertex = obj_mesh_data.calc_vertex_coords(i, export_params_.global_scale); buf.write_obj_vertex(vertex[0], vertex[1], vertex[2]); }); } @@ -435,7 +435,7 @@ void OBJWriter::write_nurbs_curve(FormatHandler &fh, const OBJCurve &obj_nurbs_d const int total_vertices = obj_nurbs_data.total_spline_vertices(spline_idx); for (int vertex_idx = 0; vertex_idx < total_vertices; vertex_idx++) { const float3 vertex_coords = obj_nurbs_data.vertex_coordinates( - spline_idx, vertex_idx, export_params_.scaling_factor); + spline_idx, vertex_idx, export_params_.global_scale); fh.write_obj_vertex(vertex_coords[0], vertex_coords[1], vertex_coords[2]); } diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc index 9f19a6390d3..d00c09b9013 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.cc @@ -265,13 +265,13 @@ const char *OBJMesh::get_object_material_name(const int16_t mat_nr) const return mat->id.name + 2; } -float3 OBJMesh::calc_vertex_coords(const int vert_index, const float scaling_factor) const +float3 OBJMesh::calc_vertex_coords(const int vert_index, const float global_scale) const { float3 r_coords; const Span<MVert> verts = export_mesh_eval_->verts(); copy_v3_v3(r_coords, verts[vert_index].co); mul_m4_v3(world_and_axes_transform_, r_coords); - mul_v3_fl(r_coords, scaling_factor); + mul_v3_fl(r_coords, global_scale); return r_coords; } diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh index db29f5651ed..ec98468e2de 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh +++ b/source/blender/io/wavefront_obj/exporter/obj_export_mesh.hh @@ -161,7 +161,7 @@ class OBJMesh : NonCopyable { /** * Calculate coordinates of the vertex at the given index. */ - float3 calc_vertex_coords(int vert_index, float scaling_factor) const; + float3 calc_vertex_coords(int vert_index, float global_scale) const; /** * Calculate vertex indices of all vertices of the polygon at the given index. */ diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc index 172a59e5341..812c3e7b5d4 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc +++ b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.cc @@ -55,14 +55,14 @@ int OBJCurve::total_spline_vertices(const int spline_index) const float3 OBJCurve::vertex_coordinates(const int spline_index, const int vertex_index, - const float scaling_factor) const + const float global_scale) const { const Nurb *const nurb = static_cast<Nurb *>(BLI_findlink(&export_curve_->nurb, spline_index)); float3 r_coord; const BPoint &bpoint = nurb->bp[vertex_index]; copy_v3_v3(r_coord, bpoint.vec); mul_m4_v3(world_axes_transform_, r_coord); - mul_v3_fl(r_coord, scaling_factor); + mul_v3_fl(r_coord, global_scale); return r_coord; } diff --git a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh index 65389d44f59..3f93112200f 100644 --- a/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh +++ b/source/blender/io/wavefront_obj/exporter/obj_export_nurbs.hh @@ -37,7 +37,7 @@ class OBJCurve : NonCopyable { /** * Get coordinates of the vertex at the given index on the given spline. */ - float3 vertex_coordinates(int spline_index, int vertex_index, float scaling_factor) const; + float3 vertex_coordinates(int spline_index, int vertex_index, float global_scale) const; /** * Get total control points of the NURBS spline at the given index. This is different than total * vertices of a spline. diff --git a/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc b/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc index f33753d720d..204237088ab 100644 --- a/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc +++ b/source/blender/io/wavefront_obj/importer/importer_mesh_utils.cc @@ -103,6 +103,9 @@ void transform_object(Object *object, const OBJImportParams &import_params) IO_AXIS_Y, IO_AXIS_Z, import_params.forward_axis, import_params.up_axis, axes_transform); copy_m4_m3(obmat, axes_transform); + float scale_vec[3] = { + import_params.global_scale, import_params.global_scale, import_params.global_scale}; + rescale_m4(obmat, scale_vec); BKE_object_apply_mat4(object, obmat, true, false); if (import_params.clamp_size != 0.0f) { diff --git a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc index bd1c2d32a12..0781028c880 100644 --- a/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc +++ b/source/blender/io/wavefront_obj/importer/obj_import_file_reader.cc @@ -508,6 +508,15 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries, } /* Faces. */ else if (parse_keyword(p, end, "f")) { + /* If we don't have a material index assigned yet, get one. + * It means "usemtl" state came from the previous object. */ + if (state_material_index == -1 && !state_material_name.empty() && + curr_geom->material_indices_.is_empty()) { + curr_geom->material_indices_.add_new(state_material_name, 0); + curr_geom->material_order_.append(state_material_name); + state_material_index = 0; + } + geom_add_polygon(curr_geom, p, end, @@ -524,7 +533,10 @@ void OBJParser::parse(Vector<std::unique_ptr<Geometry>> &r_all_geometries, else if (parse_keyword(p, end, "o")) { state_shaded_smooth = false; state_group_name = ""; - state_material_name = ""; + /* Reset object-local material index that's used in face infos. + * NOTE: do not reset the material name; that has to carry over + * into the next object if needed. */ + state_material_index = -1; curr_geom = create_geometry( curr_geom, GEOM_MESH, StringRef(p, end).trim(), r_all_geometries); } diff --git a/source/blender/io/wavefront_obj/importer/obj_importer.cc b/source/blender/io/wavefront_obj/importer/obj_importer.cc index ccbcce64d65..a42ec47151d 100644 --- a/source/blender/io/wavefront_obj/importer/obj_importer.cc +++ b/source/blender/io/wavefront_obj/importer/obj_importer.cc @@ -42,9 +42,6 @@ static void geometry_to_blender_objects(Main *bmain, { LayerCollection *lc = BKE_layer_collection_get_active(view_layer); - /* Don't do collection syncs for each object, will do once after the loop. */ - BKE_layer_collection_resync_forbid(); - /* Sort objects by name: creating many objects is much faster if the creation * order is sorted by name. */ blender::parallel_sort( @@ -73,12 +70,8 @@ static void geometry_to_blender_objects(Main *bmain, } } - /* Sync the collection after all objects are created. */ - BKE_layer_collection_resync_allow(); - BKE_main_collection_sync(bmain); + /* Do object selections in a separate loop (allows just one view layer sync). */ BKE_view_layer_synced_ensure(scene, view_layer); - - /* After collection sync, select objects in the view layer and do DEG updates. */ for (Object *obj : objects) { Base *base = BKE_view_layer_base_find(view_layer, obj); BKE_view_layer_base_select_and_set_active(view_layer, base); diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc index dcba78ac99e..5de3cdcd851 100644 --- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc +++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.cc @@ -306,7 +306,7 @@ TEST_F(obj_exporter_regression_test, all_tris) TEST_F(obj_exporter_regression_test, all_quads) { OBJExportParamsDefault _export; - _export.params.scaling_factor = 2.0f; + _export.params.global_scale = 2.0f; _export.params.export_materials = false; compare_obj_export_to_golden( "io_tests/blend_geometry/all_quads.blend", "io_tests/obj/all_quads.obj", "", _export.params); @@ -429,7 +429,7 @@ TEST_F(obj_exporter_regression_test, cubes_positioned) { OBJExportParamsDefault _export; _export.params.export_materials = false; - _export.params.scaling_factor = 2.0f; + _export.params.global_scale = 2.0f; compare_obj_export_to_golden("io_tests/blend_geometry/cubes_positioned.blend", "io_tests/obj/cubes_positioned.obj", "", diff --git a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh index 006d86312b6..a4d452e1309 100644 --- a/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh +++ b/source/blender/io/wavefront_obj/tests/obj_exporter_tests.hh @@ -19,7 +19,7 @@ struct OBJExportParamsDefault { params.forward_axis = IO_AXIS_NEGATIVE_Z; params.up_axis = IO_AXIS_Y; - params.scaling_factor = 1.f; + params.global_scale = 1.f; params.apply_modifiers = true; params.export_eval_mode = DAG_EVAL_VIEWPORT; diff --git a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc index 8d1171097b8..f459e1ab1bd 100644 --- a/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc +++ b/source/blender/io/wavefront_obj/tests/obj_importer_tests.cc @@ -8,6 +8,7 @@ #include "BKE_curve.h" #include "BKE_customdata.h" #include "BKE_main.h" +#include "BKE_material.h" #include "BKE_mesh.h" #include "BKE_object.h" #include "BKE_scene.h" @@ -22,6 +23,7 @@ #include "DEG_depsgraph_query.h" #include "DNA_curve_types.h" +#include "DNA_material_types.h" #include "DNA_mesh_types.h" #include "DNA_meshdata_types.h" #include "DNA_scene_types.h" @@ -41,6 +43,7 @@ struct Expectation { float3 normal_first; float2 uv_first; float4 color_first = {-1, -1, -1, -1}; + std::string first_mat; }; class obj_importer_test : public BlendfileLoadingBaseTest { @@ -57,6 +60,7 @@ class obj_importer_test : public BlendfileLoadingBaseTest { } OBJImportParams params; + params.global_scale = 1.0f; params.clamp_size = 0; params.forward_axis = IO_AXIS_NEGATIVE_Z; params.up_axis = IO_AXIS_Y; @@ -132,6 +136,10 @@ class obj_importer_test : public BlendfileLoadingBaseTest { // int cyclic = (nurb->flagu & CU_NURB_CYCLIC) ? 1 : 0; // EXPECT_EQ(cyclic, exp.mesh_totloop_or_curve_cyclic); } + if (!exp.first_mat.empty()) { + Material *mat = BKE_object_material_get(object, 1); + ASSERT_STREQ(mat ? mat->id.name : "<null>", exp.first_mat.c_str()); + } ++object_index; } DEG_OBJECT_ITER_END; @@ -309,7 +317,42 @@ TEST_F(obj_importer_test, import_materials) { Expectation expect[] = { {"OBCube", OB_MESH, 8, 12, 6, 24, float3(1, 1, -1), float3(-1, 1, 1)}, - {"OBmaterials", OB_MESH, 8, 12, 6, 24, float3(-1, -1, 1), float3(1, -1, -1)}, + {"OBmaterials", + OB_MESH, + 8, + 12, + 6, + 24, + float3(-1, -1, 1), + float3(1, -1, -1), + float3(0), + float2(0), + float4(-1), + "MAno_textures_red"}, + {"OBObjMtlAfter", + OB_MESH, + 3, + 3, + 1, + 3, + float3(3, 0, 0), + float3(5, 0, 0), + float3(0), + float2(0), + float4(-1), + "MAno_textures_red"}, + {"OBObjMtlBefore", + OB_MESH, + 3, + 3, + 1, + 3, + float3(6, 0, 0), + float3(8, 0, 0), + float3(0), + float2(0), + float4(-1), + "MAClay"}, }; import_and_check("materials.obj", expect, std::size(expect), 4, 8); } @@ -327,7 +370,9 @@ TEST_F(obj_importer_test, import_cubes_with_textures_rel) float3(1, 1, -1), float3(-1, -1, 1), float3(0, 1, 0), - float2(0.9935f, 0.0020f)}, + float2(0.9935f, 0.0020f), + float4(-1), + "MAMat_BaseRoughEmissNormal10"}, {"OBCubeTexMul", OB_MESH, 8, @@ -337,7 +382,9 @@ TEST_F(obj_importer_test, import_cubes_with_textures_rel) float3(4, -2, -1), float3(2, -4, 1), float3(0, 1, 0), - float2(0.9935f, 0.0020f)}, + float2(0.9935f, 0.0020f), + float4(-1), + "MAMat_BaseMul"}, {"OBCubeTiledTex", OB_MESH, 8, @@ -347,7 +394,9 @@ TEST_F(obj_importer_test, import_cubes_with_textures_rel) float3(4, 1, -1), float3(2, -1, 1), float3(0, 1, 0), - float2(0.9935f, 0.0020f)}, + float2(0.9935f, 0.0020f), + float4(-1), + "MAMat_BaseTiled"}, {"OBCubeTiledTexFromAnotherFolder", OB_MESH, 8, @@ -357,7 +406,9 @@ TEST_F(obj_importer_test, import_cubes_with_textures_rel) float3(7, 1, -1), float3(5, -1, 1), float3(0, 1, 0), - float2(0.9935f, 0.0020f)}, + float2(0.9935f, 0.0020f), + float4(-1), + "MAMat_EmissTiledAnotherFolder"}, }; import_and_check("cubes_with_textures_rel.obj", expect, std::size(expect), 4, 4); } @@ -455,7 +506,10 @@ TEST_F(obj_importer_test, import_all_objects) 26, float3(28, 1, -1), float3(26, 1, 1), - float3(-1, 0, 0)}, + float3(-1, 0, 0), + float2(0), + float4(-1), + "MARed"}, {"OBNurbsCircle", OB_MESH, 96, @@ -491,7 +545,10 @@ TEST_F(obj_importer_test, import_all_objects) 26, float3(4, 1, -1), float3(2, 1, 1), - float3(0.5774f, 0.5773f, 0.5774f)}, + float3(0.5774f, 0.5773f, 0.5774f), + float2(0), + float4(-1), + "MAMaterial"}, {"OBSurface", OB_MESH, 256, |