Blender V5.3
obj_importer.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 <string>
10
11#include "BLI_bounds.hh"
12#include "BLI_listbase.hh"
13#include "BLI_map.hh"
14#include "BLI_math_vector_c.hh"
15#include "BLI_set.hh"
16#include "BLI_sort.hh"
17#include "BLI_string.hh"
18#include "BLI_string_ref.hh"
19
20#include "BKE_context.hh"
22#include "BKE_geometry_set.hh"
23#include "BKE_instances.hh"
24#include "BKE_layer.hh"
25#include "BKE_lib_id.hh"
26#include "BKE_library.hh"
27#include "BKE_object.hh"
28#include "BKE_report.hh"
29
31
33
34#include "obj_export_mtl.hh"
36#include "obj_import_mesh.hh"
37#include "obj_import_nurbs.hh"
38#include "obj_import_objects.hh"
39#include "obj_importer.hh"
40
41#include "CLG_log.h"
42
43namespace blender {
44
45static CLG_LogRef LOG = {"io.obj"};
46
47namespace io::obj {
48
50 Collection *target,
51 const std::string &geom_name,
52 const OBJImportParams &import_params)
53{
54 if (target == nullptr || import_params.collection_separator == 0) {
55 return target;
56 }
57 size_t subname_start = 0;
58 size_t sep_pos = geom_name.find(import_params.collection_separator);
59 if (sep_pos == std::string::npos) {
60 return target;
61 }
62 while (sep_pos != std::string::npos) {
63 /* Get current sub-name, find or create collection with that name. */
64 if (sep_pos > subname_start) {
65 std::string subname = geom_name.substr(subname_start, sep_pos - subname_start);
66 bool found = false;
67 for (CollectionChild &child : target->children) {
68 if (GS(child.collection->id.name) == ID_GR &&
69 STREQ(child.collection->id.name + 2, subname.c_str()))
70 {
71 target = child.collection;
72 found = true;
73 break;
74 }
75 }
76 if (!found) {
77 target = BKE_collection_add(bmain, target, subname.c_str());
78 }
79 }
80
81 /* Proceed to next sub-name component. */
82 subname_start = sep_pos + 1;
83 if (subname_start >= geom_name.size()) {
84 break;
85 }
86 sep_pos = geom_name.find(import_params.collection_separator, subname_start);
87 }
88 return target;
89}
90
91static void geometry_to_blender_geometry_set(const OBJImportParams &import_params,
92 const Span<std::unique_ptr<Geometry>> all_geometries,
93 const GlobalVertices &global_vertices,
94 Vector<bke::GeometrySet> &geometries)
95{
96 for (const std::unique_ptr<Geometry> &geometry : all_geometries) {
97 bke::GeometrySet geometry_set;
98
99 if (geometry->geom_type_ == GEOM_MESH) {
100 MeshFromGeometry mesh_ob_from_geometry{*geometry, global_vertices};
101 Mesh *mesh = mesh_ob_from_geometry.create_mesh(import_params);
102 geometry_set = bke::GeometrySet::from_mesh(mesh);
103 }
104 else if (geometry->geom_type_ == GEOM_CURVE) {
105 CurveFromGeometry curve_ob_from_geometry(*geometry, global_vertices);
106 Curves *curves_id = curve_ob_from_geometry.create_curve(import_params);
107 geometry_set = bke::GeometrySet::from_curves(curves_id);
108 }
109
110 geometry_set.set_name(geometry->geometry_name_);
111 geometries.append(std::move(geometry_set));
112 }
113}
114
119 Scene *scene,
120 ViewLayer *view_layer,
121 const OBJImportParams &import_params,
122 MutableSpan<std::unique_ptr<Geometry>> all_geometries,
123 const GlobalVertices &global_vertices,
124 Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
125 Map<std::string, Material *> &created_materials)
126{
128
129 /* Sort objects by name: creating many objects is much faster if the creation
130 * order is sorted by name. */
131 parallel_sort(all_geometries.begin(), all_geometries.end(), [](const auto &a, const auto &b) {
132 const char *na = a ? a->geometry_name_.c_str() : "";
133 const char *nb = b ? b->geometry_name_.c_str() : "";
134 return BLI_strcasecmp(na, nb) < 0;
135 });
136
137 /* Create all the objects. */
138 Vector<Object *> objects;
139 objects.reserve(all_geometries.size());
140 Set<Collection *> collections;
141 for (const std::unique_ptr<Geometry> &geometry : all_geometries) {
142 Object *obj = nullptr;
143 if (geometry->geom_type_ == GEOM_MESH) {
144 MeshFromGeometry mesh_ob_from_geometry{*geometry, global_vertices};
145 obj = mesh_ob_from_geometry.create_mesh_object(
146 bmain, materials, created_materials, import_params);
147 }
148 else if (geometry->geom_type_ == GEOM_CURVE) {
149 CurveFromGeometry curve_ob_from_geometry(*geometry, global_vertices);
150 obj = curve_ob_from_geometry.create_curve_object(bmain, import_params);
151 }
152 if (obj != nullptr) {
153 Collection *target_collection = find_or_create_collection(
154 bmain, lc->collection, geometry->geometry_name_, import_params);
155 collections.add(target_collection);
156
157 BKE_collection_object_add(bmain, target_collection, obj);
158 objects.append(obj);
159 }
160 }
161
162 /* Clamp object size if needed. */
163 if (import_params.clamp_size > 0.0f) {
164 std::optional<Bounds<float3>> bounds = std::nullopt;
165 for (Object *obj : objects) {
167 }
168 if (bounds.has_value()) {
169 const float max_diff = math::reduce_max(bounds->max - bounds->min);
170 if (import_params.clamp_size < max_diff * import_params.global_scale) {
171 const float scale = import_params.clamp_size / max_diff;
172 for (Object *obj : objects) {
173 copy_v3_fl(obj->scale, scale);
174 }
175 }
176 }
177 }
178
179 /* Do object selections in a separate loop (allows just one view layer sync). */
180 BKE_view_layer_synced_ensure(*bmain, scene, view_layer);
181 bool has_instantiated_object = false;
182 bool has_uninstantiated_object = false;
183 for (Object *obj : objects) {
184 Base *base = BKE_view_layer_base_find(view_layer, obj);
185 if (!base) {
186 /* Object not instantiated in current viewlayer. */
187 has_uninstantiated_object = true;
188 continue;
189 }
190 has_instantiated_object = true;
192
195 DEG_id_tag_update_ex(bmain, &obj->id, flags);
196 }
197 if (has_instantiated_object && has_uninstantiated_object) {
198 CLOG_ERROR(&LOG, "Some imported objects were not instantiated, while others were");
199 }
200
201 for (Collection *col : collections) {
203 }
204
207}
208
209void importer_geometry(const OBJImportParams &import_params, Vector<bke::GeometrySet> &geometries)
210{
211 /* List of geometries to be parsed from OBJ file. */
212 Vector<std::unique_ptr<Geometry>> all_geometries;
213 /* Container for vertex and UV vertex coordinates. */
214 GlobalVertices global_vertices;
215
216 OBJParser obj_parser{import_params};
217 obj_parser.parse(all_geometries, global_vertices);
218
219 geometry_to_blender_geometry_set(import_params, all_geometries, global_vertices, geometries);
220}
221
222void importer_main(bContext *C, const OBJImportParams &import_params)
223{
224 Main *bmain = CTX_data_main(C);
225 Scene *scene = CTX_data_scene(C);
226 ViewLayer *view_layer = CTX_data_view_layer(C);
227
228 /* List of geometries to be parsed from OBJ file. */
229 Vector<std::unique_ptr<Geometry>> all_geometries;
230 /* Container for vertex and UV vertex coordinates. */
231 GlobalVertices global_vertices;
232 /* List of MTLMaterial instances to be parsed from MTL file. */
234 Map<std::string, Material *> created_materials;
235
236 OBJParser obj_parser{import_params};
237 obj_parser.parse(all_geometries, global_vertices);
238
239 /* Parse all referenced MTL files */
240 for (StringRefNull mtl_library : obj_parser.mtl_libraries()) {
241 MTLParser mtl_parser{mtl_library, import_params.filepath};
242 mtl_parser.parse_and_store(materials);
243 }
244
245 if (import_params.clear_selection) {
246 BKE_view_layer_base_deselect_all(*bmain, scene, view_layer);
247 }
248
250 if (!ID_IS_EDITABLE(lc->collection)) {
251 BKE_report(import_params.reports,
253 "Could not find an editable collection in current scene, imported data will not be "
254 "instantiated");
255 }
256
257 /* Create Blender objects from the parsed geometries */
259 scene,
260 view_layer,
261 import_params,
262 all_geometries,
263 global_vertices,
264 materials,
265 created_materials);
266}
267} // namespace io::obj
268} // namespace blender
General operations, lookup, etc. for blender objects.
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
Object groups, one object can be in many groups at once.
#define C
Definition RandGen.cpp:29
#define STREQ(a, b)
bool add(const Key &key)
Definition BLI_set.hh:248
void append(const T &value)
void reserve(const int64_t min_capacity)
Curves * create_curve(const OBJImportParams &import_params)
Object * create_curve_object(Main *bmain, const OBJImportParams &import_params)
void parse_and_store(Map< std::string, std::unique_ptr< MTLMaterial > > &r_materials)
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)
Span< std::string > mtl_libraries() const
void parse(Vector< std::unique_ptr< Geometry > > &r_all_geometries, GlobalVertices &r_global_vertices)
float3x3 scale(float3x3 mat, float2 scale)
Bounds< T > merge(const Bounds< T > &a, const Bounds< T > &b)
Definition BLI_bounds.hh:27
static Collection * find_or_create_collection(Main *bmain, Collection *target, const std::string &geom_name, const OBJImportParams &import_params)
static void geometry_to_blender_objects(Main *bmain, Scene *scene, ViewLayer *view_layer, const OBJImportParams &import_params, MutableSpan< std::unique_ptr< Geometry > > all_geometries, const GlobalVertices &global_vertices, Map< std::string, std::unique_ptr< MTLMaterial > > &materials, Map< std::string, Material * > &created_materials)
void importer_geometry(const OBJImportParams &import_params, Vector< bke::GeometrySet > &geometries)
static void geometry_to_blender_geometry_set(const OBJImportParams &import_params, const Span< std::unique_ptr< Geometry > > all_geometries, const GlobalVertices &global_vertices, Vector< bke::GeometrySet > &geometries)
void importer_main(bContext *C, const OBJImportParams &import_params)
T reduce_max(const VecBase< T, Size > &a)
BMesh Iterators.
LayerCollection * BKE_layer_collection_get_active_editable(ViewLayer *view_layer)
void BKE_view_layer_base_select_and_set_active(ViewLayer *view_layer, Base *selbase)
@ RPT_WARNING
Definition BKE_report.hh:40
LayerCollection * BKE_layer_collection_get_active(ViewLayer *view_layer)
ViewLayer * CTX_data_view_layer(const bContext *C)
void parallel_sort(RandomAccessIterator begin, RandomAccessIterator end)
Definition BLI_sort.hh:37
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
void BKE_view_layer_base_deselect_all(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
Main * CTX_data_main(const bContext *C)
MINLINE void copy_v3_fl(float r[3], float f)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
@ ID_RECALC_BASE_FLAGS
Definition DNA_ID.h:1104
ID_Type GS(const char *name)
Definition DNA_ID.h:410
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
static CLG_LogRef LOG
Definition logging.cpp:12
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
std::optional< Bounds< float3 > > BKE_object_boundbox_get(const Object *ob)
Definition object.cc:3726
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
ListBaseT< CollectionChild > children
char name[258]
Definition DNA_ID.h:433
struct Collection * collection
void set_name(std::string name)
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static GeometrySet from_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)