Blender V5.3
obj_import_mesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10
12#include "DNA_material_types.h"
13#include "DNA_meshdata_types.h"
14#include "DNA_object_types.h"
15
16#include "BKE_attribute.h"
17#include "BKE_attribute.hh"
18#include "BKE_deform.hh"
19#include "BKE_lib_id.hh"
20#include "BKE_material.hh"
21#include "BKE_mesh.hh"
23#include "BKE_object.hh"
24
25#include "BLI_listbase.hh"
26#include "BLI_math_vector_c.hh"
27#include "BLI_set.hh"
28
29#include "IO_validate.hh"
30#include "IO_wavefront_obj.hh"
32#include "obj_export_mtl.hh"
33#include "obj_import_mesh.hh"
34
35#include "CLG_log.h"
36
37namespace blender {
38
39static CLG_LogRef LOG = {"io.obj"};
40
41namespace io::obj {
42
44{
45 const int64_t tot_verts_object{mesh_geometry_.get_vertex_count()};
46 if (tot_verts_object <= 0) {
47 /* Empty mesh */
48 return nullptr;
49 }
50
51 this->fixup_invalid_faces();
52
53 if (!validate::size_fits_in_int(tot_verts_object) ||
54 !validate::size_fits_in_int(mesh_geometry_.edges_.size()) ||
55 !validate::size_fits_in_int(mesh_geometry_.face_elements_.size()) ||
56 !validate::size_fits_in_int(mesh_geometry_.total_corner_))
57 {
58 CLOG_WARN(&LOG, "OBJ mesh too large to import, exceeds max int size");
59 return nullptr;
60 }
61
62 /* Includes explicitly imported edges, not the ones belonging the faces to be created. */
63 Mesh *mesh = BKE_mesh_new_nomain(tot_verts_object,
64 mesh_geometry_.edges_.size(),
65 mesh_geometry_.face_elements_.size(),
66 mesh_geometry_.total_corner_);
67
68 this->create_vertices(mesh);
69 this->create_faces(mesh, import_params.import_vertex_groups && !import_params.use_split_groups);
70 this->create_edges(mesh);
71 this->create_uv_verts(mesh);
72 this->create_normals(mesh);
73 this->create_colors(mesh);
74 this->create_vertex_groups(mesh);
75
76 if (import_params.validate_meshes || mesh_geometry_.has_invalid_faces_) {
77 bool verbose_validate = false;
78#ifndef NDEBUG
79 verbose_validate = true;
80#endif
81 bke::mesh_validate(*mesh, verbose_validate);
82 }
83
84 return mesh;
85}
86
88 Main *bmain,
89 Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
90 Map<std::string, Material *> &created_materials,
91 const OBJImportParams &import_params)
92{
93 Mesh *mesh = this->create_mesh(import_params);
94
95 if (mesh == nullptr) {
96 return nullptr;
97 }
98
99 std::string ob_name = get_geometry_name(mesh_geometry_.geometry_name_,
100 import_params.collection_separator);
101 if (ob_name.empty()) {
102 ob_name = "Untitled";
103 }
104
105 Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name.c_str());
106 obj->data = static_cast<ID *>(BKE_object_obdata_add_from_type(bmain, OB_MESH, ob_name.c_str()));
107
108 this->create_materials(bmain,
109 materials,
110 created_materials,
111 obj,
112 import_params.relative_paths,
113 import_params.mtl_name_collision_mode);
114
115 BKE_mesh_nomain_to_mesh(mesh, id_cast<Mesh *>(obj->data), obj);
116
117 transform_object(obj, import_params);
118
119 return obj;
120}
121
122void MeshFromGeometry::fixup_invalid_faces()
123{
124 for (int64_t face_idx = 0; face_idx < mesh_geometry_.face_elements_.size(); ++face_idx) {
125 const FaceElem &curr_face = mesh_geometry_.face_elements_[face_idx];
126
127 if (curr_face.corner_count_ < 3) {
128 /* Skip and remove faces that have fewer than 3 corners. */
129 mesh_geometry_.total_corner_ -= curr_face.corner_count_;
130 mesh_geometry_.face_elements_.remove_and_reorder(face_idx);
131 --face_idx;
132 continue;
133 }
134
135 /* Check if face is invalid for Blender conventions:
136 * basically whether it has duplicate vertex indices. */
137 bool valid = true;
138 Set<int, 8> used_verts;
139 for (const int64_t i : IndexRange(curr_face.corner_count_)) {
140 const int64_t corner_idx = curr_face.start_index_ + i;
141 const int vertex_idx = mesh_geometry_.face_corners_[corner_idx].vert_index;
142 if (used_verts.contains(vertex_idx)) {
143 valid = false;
144 break;
145 }
146 used_verts.add(vertex_idx);
147 }
148 if (valid) {
149 continue;
150 }
151
152 /* We have an invalid face, have to turn it into possibly
153 * multiple valid faces. */
154 Vector<int, 8> face_verts;
155 Vector<int, 8> face_uvs;
156 Vector<int, 8> face_normals;
157 face_verts.reserve(curr_face.corner_count_);
158 face_uvs.reserve(curr_face.corner_count_);
159 face_normals.reserve(curr_face.corner_count_);
160 for (const int64_t i : IndexRange(curr_face.corner_count_)) {
161 const int64_t corner_idx = curr_face.start_index_ + i;
162 const FaceCorner &corner = mesh_geometry_.face_corners_[corner_idx];
163 face_verts.append(corner.vert_index);
164 face_normals.append(corner.vertex_normal_index);
165 face_uvs.append(corner.uv_vert_index);
166 }
167 int face_vertex_group = curr_face.vertex_group_index;
168 int face_material = curr_face.material_index;
169 bool face_shaded_smooth = curr_face.shaded_smooth;
170
171 /* Remove the invalid face. */
172 mesh_geometry_.total_corner_ -= curr_face.corner_count_;
173 mesh_geometry_.face_elements_.remove_and_reorder(face_idx);
174 --face_idx;
175
176 Vector<Vector<int>> new_faces = fixup_invalid_face(global_vertices_.vertices, face_verts);
177
178 /* Create the newly formed faces. */
179 for (Span<int> face : new_faces) {
180 if (face.size() < 3) {
181 continue;
182 }
183 FaceElem new_face{};
184 new_face.vertex_group_index = face_vertex_group;
185 new_face.material_index = face_material;
186 new_face.shaded_smooth = face_shaded_smooth;
187 new_face.start_index_ = mesh_geometry_.face_corners_.size();
188 new_face.corner_count_ = face.size();
189 for (int idx : face) {
190 BLI_assert(idx >= 0 && idx < face_verts.size());
191 mesh_geometry_.face_corners_.append({face_verts[idx], face_uvs[idx], face_normals[idx]});
192 }
193 mesh_geometry_.face_elements_.append(new_face);
194 mesh_geometry_.total_corner_ += face.size();
195 }
196 }
197}
198
199void MeshFromGeometry::create_vertices(Mesh *mesh)
200{
201 MutableSpan<float3> positions = mesh->vert_positions_for_write();
202 /* Go through all the global vertex indices from min to max,
203 * checking which ones are actually and building a global->local
204 * index mapping. Write out the used vertex positions into the Mesh
205 * data. */
206 mesh_geometry_.global_to_local_vertices_.clear();
207 mesh_geometry_.global_to_local_vertices_.reserve(mesh_geometry_.vertices_.size());
208 for (int vi = mesh_geometry_.vertex_index_min_; vi <= mesh_geometry_.vertex_index_max_; ++vi) {
209 BLI_assert(vi >= 0 && vi < global_vertices_.vertices.size());
210 if (!mesh_geometry_.vertices_.contains(vi)) {
211 continue;
212 }
213 int local_vi = int(mesh_geometry_.global_to_local_vertices_.size());
214 BLI_assert(local_vi >= 0 && local_vi < mesh->verts_num);
215 copy_v3_v3(positions[local_vi], global_vertices_.vertices[vi]);
216 mesh_geometry_.global_to_local_vertices_.add_new(vi, local_vi);
217 }
218}
219
220void MeshFromGeometry::create_faces(Mesh *mesh, bool use_vertex_groups)
221{
222 MutableSpan<MDeformVert> dverts;
223 const int64_t total_verts = mesh_geometry_.get_vertex_count();
224 if (use_vertex_groups && total_verts && mesh_geometry_.has_vertex_groups_) {
225 dverts = mesh->deform_verts_for_write();
226 }
227
228 Span<float3> positions = mesh->vert_positions();
229 MutableSpan<int> face_offsets = mesh->face_offsets_for_write();
230 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
231 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
232 bke::SpanAttributeWriter<int> material_indices =
233 attributes.lookup_or_add_for_write_only_span<int>("material_index", bke::AttrDomain::Face);
234
235 const bool set_face_sharpness = !has_normals();
236 bke::SpanAttributeWriter<bool> sharp_faces = attributes.lookup_or_add_for_write_span<bool>(
237 "sharp_face", bke::AttrDomain::Face);
238
239 int corner_index = 0;
240
241 for (int face_idx = 0; face_idx < mesh->faces_num; ++face_idx) {
242 const FaceElem &curr_face = mesh_geometry_.face_elements_[face_idx];
243 if (curr_face.corner_count_ < 3) {
244 /* Don't add single vertex face, or edges. */
245 CLOG_WARN(&LOG, "Face with less than 3 vertices found, skipping.");
246 continue;
247 }
248
249 face_offsets[face_idx] = corner_index;
250 if (set_face_sharpness) {
251 /* If we have no vertex normals, set face sharpness flag based on
252 * whether smooth shading is off. */
253 sharp_faces.span[face_idx] = !curr_face.shaded_smooth;
254 }
255
256 material_indices.span[face_idx] = curr_face.material_index;
257 /* Importing obj files without any materials would result in negative indices, which is not
258 * supported. */
259 material_indices.span[face_idx] = std::max(material_indices.span[face_idx], 0);
260
261 for (int64_t idx = 0; idx < curr_face.corner_count_; ++idx) {
262 const FaceCorner &curr_corner = mesh_geometry_.face_corners_[curr_face.start_index_ + idx];
263 corner_verts[corner_index] = mesh_geometry_.global_to_local_vertices_.lookup_default(
264 curr_corner.vert_index, 0);
265
266 /* Setup vertex group data, if needed. */
267 if (!dverts.is_empty()) {
268 const int group_index = curr_face.vertex_group_index;
269 /* NOTE: face might not belong to any group. */
270 if (group_index >= 0) {
271 MDeformWeight *dw = BKE_defvert_ensure_index(&dverts[corner_verts[corner_index]],
272 group_index);
273 dw->weight = 1.0f;
274 }
275 }
276
277 corner_index++;
278 }
279
280 if (!set_face_sharpness) {
281 /* If we do have vertex normals, we do not want to set face sharpness.
282 * Exception is, if degenerate faces (zero area, with co-colocated
283 * vertices) are present in the input data; this confuses custom
284 * corner normals calculation in Blender. Set such faces as sharp,
285 * they will be not shared across smooth vertex face fans. */
286 const float area = bke::mesh::face_area_calc(
287 positions, corner_verts.slice(face_offsets[face_idx], curr_face.corner_count_));
288 if (area < 1.0e-12f) {
289 sharp_faces.span[face_idx] = true;
290 }
291 }
292 }
293
294 material_indices.finish();
295 sharp_faces.finish();
296}
297
298void MeshFromGeometry::create_vertex_groups(Mesh *mesh)
299{
300 if (mesh->deform_verts().is_empty()) {
301 return;
302 }
303 for (const std::string &name : mesh_geometry_.group_order_) {
304 bDeformGroup *defgroup = MEM_new<bDeformGroup>(__func__);
305 StringRef(name).copy_utf8_truncated(defgroup->name);
306 BLI_addtail(&mesh->vertex_group_names, defgroup);
307 }
308}
309
310void MeshFromGeometry::create_edges(Mesh *mesh)
311{
312 MutableSpan<int2> edges = mesh->edges_for_write();
313
314 const int64_t tot_edges{mesh_geometry_.edges_.size()};
315 const int64_t total_verts{mesh_geometry_.get_vertex_count()};
316 UNUSED_VARS_NDEBUG(total_verts);
317 for (int i = 0; i < tot_edges; ++i) {
318 const int2 &src_edge = mesh_geometry_.edges_[i];
319 int2 &dst_edge = edges[i];
320 dst_edge[0] = mesh_geometry_.global_to_local_vertices_.lookup_default(src_edge[0], 0);
321 dst_edge[1] = mesh_geometry_.global_to_local_vertices_.lookup_default(src_edge[1], 0);
322 BLI_assert(dst_edge[0] < total_verts && dst_edge[1] < total_verts);
323 }
324
325 /* Set argument `update` to true so that existing, explicitly imported edges can be merged
326 * with the new ones created from faces. */
327 bke::mesh_calc_edges(*mesh, true, false);
328}
329
330void MeshFromGeometry::create_uv_verts(Mesh *mesh)
331{
332 if (global_vertices_.uv_vertices.size() <= 0) {
333 return;
334 }
335
336 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
337 bke::SpanAttributeWriter<float2> uv_map = attributes.lookup_or_add_for_write_only_span<float2>(
338 "UVMap", bke::AttrDomain::Corner);
339
340 int corner_index = 0;
341 bool added_uv = false;
342
343 for (const FaceElem &curr_face : mesh_geometry_.face_elements_) {
344 for (int64_t idx = 0; idx < curr_face.corner_count_; ++idx) {
345 const FaceCorner &curr_corner = mesh_geometry_.face_corners_[curr_face.start_index_ + idx];
346 if (curr_corner.uv_vert_index >= 0 &&
347 curr_corner.uv_vert_index < global_vertices_.uv_vertices.size())
348 {
349 uv_map.span[corner_index] = global_vertices_.uv_vertices[curr_corner.uv_vert_index];
350 added_uv = true;
351 }
352 else {
353 uv_map.span[corner_index] = {0.0f, 0.0f};
354 }
355 corner_index++;
356 }
357 }
358
359 uv_map.finish();
360
361 /* If we have an object without UVs which resides in the same `.obj` file
362 * as an object which *does* have UVs we can end up adding a UV layer
363 * filled with zeroes.
364 * We could maybe check before creating this layer but that would need
365 * iterating over the whole mesh to check for UVs and as this is probably
366 * the exception rather than the rule, just delete it afterwards.
367 */
368 if (!added_uv) {
369 attributes.remove("UVMap");
370 }
371 else {
372 mesh->uv_maps_active_set("UVMap");
373 mesh->uv_maps_default_set("UVMap");
374 }
375}
376
378 const std::string &name,
379 Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
380 Map<std::string, Material *> &created_materials,
381 bool relative_paths,
382 eOBJMtlNameCollisionMode mtl_name_collision_mode)
383{
384 /* Have we created this material already in this import session? */
385 Material **found_mat = created_materials.lookup_ptr(name);
386 if (found_mat != nullptr) {
387 return *found_mat;
388 }
389
390 /* Check if a material with this name already exists in the main database */
391 Material *existing_mat = id_cast<Material *>(BKE_libblock_find_name(bmain, ID_MA, name.c_str()));
392 if (existing_mat != nullptr &&
393 mtl_name_collision_mode == OBJ_MTL_NAME_COLLISION_REFERENCE_EXISTING)
394 {
395 /* If the collision mode is set to reference existing materials, use the existing one */
396 created_materials.add_new(name, existing_mat);
397 return existing_mat;
398 }
399
400 /* We need to create a new material */
401 const MTLMaterial &mtl = *materials.lookup_or_add(name, std::make_unique<MTLMaterial>());
402
403 /* If we're in MAKE_UNIQUE mode and a material with this name already exists,
404 * BKE_material_add will automatically create a unique name */
405 Material *mat = BKE_material_add(bmain, name.c_str());
406 id_us_min(&mat->id);
407
408 mat->nodetree = create_mtl_node_tree(bmain, mtl, mat, relative_paths);
409 BKE_ntree_update_after_single_tree_change(*bmain, *mat->nodetree);
410
411 created_materials.add_new(name, mat);
412 return mat;
413}
414
415void MeshFromGeometry::create_materials(Main *bmain,
416 Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
417 Map<std::string, Material *> &created_materials,
418 Object *obj,
419 bool relative_paths,
420 eOBJMtlNameCollisionMode mtl_name_collision_mode)
421{
422 for (const std::string &name : mesh_geometry_.material_order_) {
424 bmain, name, materials, created_materials, relative_paths, mtl_name_collision_mode);
425 if (mat == nullptr) {
426 continue;
427 }
428 BKE_object_material_assign_single_obdata(bmain, obj, mat, obj->totcol + 1);
429 }
430 if (obj->totcol > 0) {
431 obj->actcol = 1;
432 }
433}
434
435bool MeshFromGeometry::has_normals() const
436{
437 return !global_vertices_.vert_normals.is_empty() && mesh_geometry_.total_corner_ != 0;
438}
439
440void MeshFromGeometry::create_normals(Mesh *mesh)
441{
442 if (!has_normals()) {
443 return;
444 }
445
446 Array<float3> corner_normals(mesh_geometry_.total_corner_);
447 int corner_index = 0;
448 for (const FaceElem &curr_face : mesh_geometry_.face_elements_) {
449 for (int64_t idx = 0; idx < curr_face.corner_count_; ++idx) {
450 const FaceCorner &curr_corner = mesh_geometry_.face_corners_[curr_face.start_index_ + idx];
451 int n_index = curr_corner.vertex_normal_index;
452 float3 normal(0, 0, 0);
453 if (n_index >= 0 && n_index < global_vertices_.vert_normals.size()) {
454 normal = global_vertices_.vert_normals[n_index];
455 }
456 corner_normals[corner_index] = normal;
457 corner_index++;
458 }
459 }
460 bke::mesh_set_custom_normals(*mesh, corner_normals);
461}
462
463void MeshFromGeometry::create_colors(Mesh *mesh)
464{
465 /* Nothing to do if we don't have vertex colors at all. */
466 if (global_vertices_.vertex_colors.is_empty()) {
467 return;
468 }
469
470 /* First pass to determine if we need to create a color attribute. */
471 for (int vi : mesh_geometry_.vertices_) {
472 if (!global_vertices_.has_vertex_color(vi)) {
473 return;
474 }
475 }
476
477 AttributeOwner owner = AttributeOwner::from_id(&mesh->id);
478 const std::string name = BKE_attribute_calc_unique_name(owner, "Color");
479 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
480 bke::SpanAttributeWriter attr = attributes.lookup_or_add_for_write_span<ColorGeometry4f>(
482 BKE_id_attributes_active_color_set(&mesh->id, name);
484 MutableSpan<float4> colors = attr.span.cast<float4>();
485
486 /* Second pass to fill out the data. */
487 for (auto item : mesh_geometry_.global_to_local_vertices_.items()) {
488 const int vi = item.key;
489 const int local_vi = item.value;
490 BLI_assert(vi >= 0 && vi < global_vertices_.vertex_colors.size());
491 BLI_assert(local_vi >= 0 && local_vi < mesh->verts_num);
492 const float3 &c = global_vertices_.vertex_colors[vi];
493 colors[local_vi] = float4(c.x, c.y, c.z, 1.0);
494 }
495
496 attr.finish();
497}
498
499} // namespace io::obj
500} // namespace blender
Generic geometry attributes built on CustomData.
support for deformation groups and hooks.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
#define UNUSED_VARS_NDEBUG(...)
#define BLI_assert(a)
void clear(bool preserve_shaders=false) override
static AttributeOwner from_id(ID *id)
constexpr IndexRange()=default
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
void add_new(const Key &key, const Value &value)
Definition BLI_map.hh:265
void reserve(const int64_t n)
Definition BLI_set.hh:656
bool contains(const Key &key) const
Definition BLI_set.hh:310
bool add(const Key &key)
Definition BLI_set.hh:248
constexpr StringRef()
Mesh * create_mesh(const OBJImportParams &import_params)
Object * create_mesh_object(Main *bmain, Map< std::string, std::unique_ptr< MTLMaterial > > &materials, Map< std::string, Material * > &created_materials, const OBJImportParams &import_params)
float face_area_calc(Span< float3 > vert_positions, Span< int > face_verts)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
bool mesh_validate(Mesh &mesh, bool verbose=false, bool allow_missing_edges=false)
void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > corner_normals)
static Material * get_or_create_material(Main *bmain, const std::string &name, Map< std::string, std::unique_ptr< MTLMaterial > > &materials, Map< std::string, Material * > &created_materials, bool relative_paths, eOBJMtlNameCollisionMode mtl_name_collision_mode)
bNodeTree * create_mtl_node_tree(Main *bmain, const MTLMaterial &mtl_mat, Material *mat, bool relative_paths)
void transform_object(Object *object, const OBJImportParams &import_params)
Vector< Vector< int > > fixup_invalid_face(Span< float3 > vert_positions, Span< int > face_verts)
std::string get_geometry_name(const std::string &full_name, char separator)
bool size_fits_in_int(const int64_t size)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition lib_id.cc:1808
void BKE_id_attributes_default_color_set(struct ID *id, std::optional< StringRef > name)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void BKE_mesh_nomain_to_mesh(Mesh *mesh_src, Mesh *mesh_dst, Object *ob, bool process_shape_keys=true)
void * BKE_object_obdata_add_from_type(Main *bmain, ObjectType type, const char *name) ATTR_NONNULL(1)
Definition object.cc:2185
std::string BKE_attribute_calc_unique_name(const AttributeOwner &owner, StringRef name)
VecBase< float, 4 > float4
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
void BKE_ntree_update_after_single_tree_change(Main &bmain, bNodeTree &modified_tree, const NodeTreeUpdateExtraParams &params={})
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
MDeformWeight * BKE_defvert_ensure_index(MDeformVert *dv, int defgroup)
Definition deform.cc:830
void BLI_addtail(ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition listbase.cc:118
void BKE_id_attributes_active_color_set(struct ID *id, std::optional< StringRef > name)
@ OBJ_MTL_NAME_COLLISION_REFERENCE_EXISTING
static CLG_LogRef LOG
Definition logging.cpp:12
void BKE_object_material_assign_single_obdata(Main *bmain, Object *ob, Material *ma, short act)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Material * BKE_material_add(Main *bmain, const char *name)
VecBase< float, 3 > float3
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
void id_us_min(ID *id)
Definition lib_id.cc:441
eOBJMtlNameCollisionMode mtl_name_collision_mode
Vector< std::string > material_order_
Vector< FaceCorner > face_corners_
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136