Blender  V3.3
obj_importer.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <string>
8 
9 #include "BLI_map.hh"
10 #include "BLI_math_vec_types.hh"
11 #include "BLI_set.hh"
12 #include "BLI_sort.hh"
13 #include "BLI_string_ref.hh"
14 
15 #include "BKE_layer.h"
16 #include "BKE_scene.h"
17 
18 #include "DEG_depsgraph_build.h"
19 
20 #include "DNA_collection_types.h"
21 
23 #include "obj_import_mesh.hh"
24 #include "obj_import_nurbs.hh"
25 #include "obj_import_objects.hh"
26 #include "obj_importer.hh"
27 
28 namespace blender::io::obj {
29 
33 static void geometry_to_blender_objects(Main *bmain,
34  Scene *scene,
35  ViewLayer *view_layer,
36  const OBJImportParams &import_params,
37  Vector<std::unique_ptr<Geometry>> &all_geometries,
38  const GlobalVertices &global_vertices,
39  Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
40  Map<std::string, Material *> &created_materials)
41 {
43 
44  /* Don't do collection syncs for each object, will do once after the loop. */
46 
47  /* Sort objects by name: creating many objects is much faster if the creation
48  * order is sorted by name. */
50  all_geometries.begin(), all_geometries.end(), [](const auto &a, const auto &b) {
51  const char *na = a ? a->geometry_name_.c_str() : "";
52  const char *nb = b ? b->geometry_name_.c_str() : "";
53  return BLI_strcasecmp(na, nb) < 0;
54  });
55 
56  /* Create all the objects. */
57  Vector<Object *> objects;
58  objects.reserve(all_geometries.size());
59  for (const std::unique_ptr<Geometry> &geometry : all_geometries) {
60  Object *obj = nullptr;
61  if (geometry->geom_type_ == GEOM_MESH) {
62  MeshFromGeometry mesh_ob_from_geometry{*geometry, global_vertices};
63  obj = mesh_ob_from_geometry.create_mesh(bmain, materials, created_materials, import_params);
64  }
65  else if (geometry->geom_type_ == GEOM_CURVE) {
66  CurveFromGeometry curve_ob_from_geometry(*geometry, global_vertices);
67  obj = curve_ob_from_geometry.create_curve(bmain, import_params);
68  }
69  if (obj != nullptr) {
70  BKE_collection_object_add(bmain, lc->collection, obj);
71  objects.append(obj);
72  }
73  }
74 
75  /* Sync the collection after all objects are created. */
78 
79  /* After collection sync, select objects in the view layer and do DEG updates. */
80  for (Object *obj : objects) {
81  Base *base = BKE_view_layer_base_find(view_layer, obj);
83 
87  DEG_id_tag_update_ex(bmain, &obj->id, flags);
88  }
89 
92 }
93 
94 void importer_main(bContext *C, const OBJImportParams &import_params)
95 {
96  Main *bmain = CTX_data_main(C);
98  ViewLayer *view_layer = CTX_data_view_layer(C);
99  importer_main(bmain, scene, view_layer, import_params);
100 }
101 
102 void importer_main(Main *bmain,
103  Scene *scene,
104  ViewLayer *view_layer,
105  const OBJImportParams &import_params,
106  size_t read_buffer_size)
107 {
108  /* List of Geometry instances to be parsed from OBJ file. */
109  Vector<std::unique_ptr<Geometry>> all_geometries;
110  /* Container for vertex and UV vertex coordinates. */
111  GlobalVertices global_vertices;
112  /* List of MTLMaterial instances to be parsed from MTL file. */
114  Map<std::string, Material *> created_materials;
115 
116  OBJParser obj_parser{import_params, read_buffer_size};
117  obj_parser.parse(all_geometries, global_vertices);
118 
119  for (StringRefNull mtl_library : obj_parser.mtl_libraries()) {
120  MTLParser mtl_parser{mtl_library, import_params.filepath};
121  mtl_parser.parse_and_store(materials);
122  }
123 
124  if (import_params.clear_selection) {
126  }
128  scene,
129  view_layer,
130  import_params,
131  all_geometries,
132  global_vertices,
133  materials,
134  created_materials);
135 }
136 } // namespace blender::io::obj
bool BKE_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *ob)
Definition: collection.c:1125
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
struct LayerCollection * BKE_layer_collection_get_active(struct ViewLayer *view_layer)
Definition: layer.c:636
void BKE_main_collection_sync(const struct Main *bmain)
void BKE_layer_collection_resync_allow(void)
Definition: layer.c:766
void BKE_view_layer_base_deselect_all(struct ViewLayer *view_layer)
Definition: layer.c:388
struct Base * BKE_view_layer_base_find(struct ViewLayer *view_layer, struct Object *ob)
Definition: layer.c:379
void BKE_view_layer_base_select_and_set_active(struct ViewLayer *view_layer, struct Base *selbase)
Definition: layer.c:397
void BKE_layer_collection_resync_forbid(void)
Definition: layer.c:761
void DEG_id_tag_update_ex(struct Main *bmain, struct ID *id, int flag)
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
@ ID_RECALC_TRANSFORM
Definition: DNA_ID.h:771
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:834
@ ID_RECALC_ANIMATION
Definition: DNA_ID.h:794
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:791
@ ID_RECALC_BASE_FLAGS
Definition: DNA_ID.h:821
Object groups, one object can be in many groups at once.
#define C
Definition: RandGen.cpp:25
void append(const T &value)
Definition: BLI_vector.hh:433
void reserve(const int64_t min_capacity)
Definition: BLI_vector.hh:340
Object * create_curve(Main *bmain, const OBJImportParams &import_params)
Scene scene
smooth(Type::VEC4, "color_mul") .smooth(Type gpFillTexture gpSceneDepthTexture materials[GPENCIL_MATERIAL_BUFFER_LEN]
Definition: gpencil_info.hh:29
static unsigned a[3]
Definition: RandGen.cpp:78
static void geometry_to_blender_objects(Main *bmain, Scene *scene, ViewLayer *view_layer, const OBJImportParams &import_params, Vector< std::unique_ptr< Geometry >> &all_geometries, const GlobalVertices &global_vertices, Map< std::string, std::unique_ptr< MTLMaterial >> &materials, Map< std::string, Material * > &created_materials)
Definition: obj_importer.cc:33
void importer_main(bContext *C, const OBJImportParams &import_params)
Definition: obj_importer.cc:94
void parallel_sort(RandomAccessIterator begin, RandomAccessIterator end)
Definition: BLI_sort.hh:21
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
struct Collection * collection
Definition: BKE_main.h:121
char filepath[FILE_MAX]