Blender V5.3
io_ply_ops.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#ifdef WITH_IO_PLY
10
11# include "BKE_context.hh"
12# include "BKE_file_handler.hh"
13# include "BKE_main.hh"
14# include "BKE_report.hh"
15
16# include "BLI_string.hh"
17# include "BLI_string_utf8.hh"
18
19# include "WM_api.hh"
20# include "WM_types.hh"
21
22# include "DNA_space_types.h"
23
24# include "ED_fileselect.hh"
25# include "ED_outliner.hh"
26
27# include "RNA_access.hh"
28# include "RNA_define.hh"
29
30# include "BLT_translation.hh"
31
32# include "UI_interface.hh"
33# include "UI_interface_layout.hh"
34# include "UI_resources.hh"
35
36# include "IO_orientation.hh"
37
38# include "IO_ply.hh"
39# include "io_ply_ops.hh"
40# include "io_utils.hh"
41
42namespace blender {
43
44static const EnumPropertyItem ply_vertex_colors_mode[] = {
45 {int(ePLYVertexColorMode::None), "NONE", 0, "None", "Do not import/export color attributes"},
47 "SRGB",
48 0,
49 "sRGB",
50 "Vertex colors in the file are in sRGB color space"},
52 "LINEAR",
53 0,
54 "Linear",
55 "Vertex colors in the file are in linear color space"},
56 {0, nullptr, 0, nullptr, nullptr}};
57
58static const EnumPropertyItem io_ply_export_evaluation_mode[] = {
59 {DAG_EVAL_RENDER, "DAG_EVAL_RENDER", 0, "Render", "Export objects as they appear in render"},
61 "DAG_EVAL_VIEWPORT",
62 0,
63 "Viewport",
64 "Export objects as they appear in the viewport (Multiresolution modifiers in Sculpt Mode "
65 "will not be evaluated)"},
66 {0, nullptr, 0, nullptr, nullptr}};
67
68static wmOperatorStatus wm_ply_export_invoke(bContext *C,
69 wmOperator *op,
70 const wmEvent * /*event*/)
71{
73
76}
77
78static wmOperatorStatus wm_ply_export_exec(bContext *C, wmOperator *op)
79{
80 if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
81 BKE_report(op->reports, RPT_ERROR, "No filepath given");
82 return OPERATOR_CANCELLED;
83 }
84 PLYExportParams export_params;
85 export_params.file_base_for_tests[0] = '\0';
86 RNA_string_get(op->ptr, "filepath", export_params.filepath);
87 export_params.blen_filepath = CTX_data_main(C)->filepath;
88
89 export_params.forward_axis = eIOAxis(RNA_enum_get(op->ptr, "forward_axis"));
90 export_params.up_axis = eIOAxis(RNA_enum_get(op->ptr, "up_axis"));
91 export_params.global_scale = RNA_float_get(op->ptr, "global_scale");
92 export_params.apply_modifiers = RNA_boolean_get(op->ptr, "apply_modifiers");
93 export_params.evaluation_mode = eEvaluationMode(RNA_enum_get(op->ptr, "evaluation_mode"));
94
95 export_params.export_selected_objects = RNA_boolean_get(op->ptr, "export_selected_objects");
96 export_params.export_uv = RNA_boolean_get(op->ptr, "export_uv");
97 export_params.export_normals = RNA_boolean_get(op->ptr, "export_normals");
98 export_params.vertex_colors = ePLYVertexColorMode(RNA_enum_get(op->ptr, "export_colors"));
99 export_params.export_attributes = RNA_boolean_get(op->ptr, "export_attributes");
100 export_params.export_triangulated_mesh = RNA_boolean_get(op->ptr, "export_triangulated_mesh");
101 export_params.ascii_format = RNA_boolean_get(op->ptr, "ascii_format");
102
103 RNA_string_get(op->ptr, "collection", export_params.collection);
104
105 export_params.reports = op->reports;
106
107 PLY_export(C, export_params);
108
109 if (BKE_reports_contain(op->reports, RPT_ERROR)) {
110 return OPERATOR_CANCELLED;
111 }
112
113 BKE_report(op->reports, RPT_INFO, "File exported successfully");
114 return OPERATOR_FINISHED;
115}
116
117static void wm_ply_export_draw(bContext *C, wmOperator *op)
118{
119 ui::Layout &layout = *op->layout;
120 PointerRNA *ptr = op->ptr;
121
122 layout.use_property_split_set(true);
123 layout.use_property_decorate_set(false);
124
125 if (ui::Layout *panel = layout.panel(C, "PLY_export_general", false, IFACE_("General"))) {
126 ui::Layout &col = panel->column(false);
127
128 {
129 ui::Layout &sub = col.column(false, IFACE_("Format"));
130 sub.prop(ptr, "ascii_format", UI_ITEM_NONE, IFACE_("ASCII"), ICON_NONE);
131 }
132 /* The Selection only options only make sense when using regular export. */
133 if (CTX_wm_space_file(C)) {
134 ui::Layout &sub = col.column(false, IFACE_("Include"));
135 sub.prop(ptr, "export_selected_objects", UI_ITEM_NONE, IFACE_("Selection Only"), ICON_NONE);
136 }
137
138 col.prop(ptr, "global_scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
139 col.prop(ptr, "forward_axis", UI_ITEM_NONE, IFACE_("Forward Axis"), ICON_NONE);
140 col.prop(ptr, "up_axis", UI_ITEM_NONE, IFACE_("Up Axis"), ICON_NONE);
141 }
142
143 if (ui::Layout *panel = layout.panel(C, "PLY_export_geometry", false, IFACE_("Geometry"))) {
144 ui::Layout &col = panel->column(false);
145
146 col.prop(ptr, "export_uv", UI_ITEM_NONE, IFACE_("UV Coordinates"), ICON_NONE);
147 col.prop(ptr, "export_normals", UI_ITEM_NONE, IFACE_("Vertex Normals"), ICON_NONE);
148 col.prop(ptr, "export_attributes", UI_ITEM_NONE, IFACE_("Vertex Attributes"), ICON_NONE);
149 col.prop(ptr, "export_colors", UI_ITEM_NONE, IFACE_("Vertex Colors"), ICON_NONE);
150
151 col.prop(
152 ptr, "export_triangulated_mesh", UI_ITEM_NONE, IFACE_("Triangulated Mesh"), ICON_NONE);
153 col.prop(ptr, "apply_modifiers", UI_ITEM_NONE, IFACE_("Apply Modifiers"), ICON_NONE);
154 col.prop(ptr, "evaluation_mode", UI_ITEM_NONE, IFACE_("Properties"), ICON_NONE);
155 }
156}
157
161static bool wm_ply_export_check(bContext * /*C*/, wmOperator *op)
162{
163 char filepath[FILE_MAX];
164 bool changed = false;
165 RNA_string_get(op->ptr, "filepath", filepath);
166
167 if (!BLI_path_extension_check(filepath, ".ply")) {
168 BLI_path_extension_ensure(filepath, FILE_MAX, ".ply");
169 RNA_string_set(op->ptr, "filepath", filepath);
170 changed = true;
171 }
172 return changed;
173}
174
176{
177 PropertyRNA *prop;
178
179 ot->name = "Export PLY";
180 ot->description = "Save the scene to a PLY file";
181 ot->idname = "WM_OT_ply_export";
182
183 ot->invoke = wm_ply_export_invoke;
184 ot->exec = wm_ply_export_exec;
185 ot->poll = WM_operator_winactive;
186 ot->ui = wm_ply_export_draw;
187 ot->check = wm_ply_export_check;
188
189 ot->flag = OPTYPE_PRESET;
190
194 FILE_SAVE,
198
199 /* Object transform options. */
200 prop = RNA_def_enum(ot->srna, "forward_axis", io_transform_axis, IO_AXIS_Y, "Forward Axis", "");
202 prop = RNA_def_enum(ot->srna, "up_axis", io_transform_axis, IO_AXIS_Z, "Up Axis", "");
205 ot->srna,
206 "global_scale",
207 1.0f,
208 0.0001f,
209 10000.0f,
210 "Scale",
211 "Value by which to enlarge or shrink the objects with respect to the world's origin",
212 0.0001f,
213 10000.0f);
214 /* File Writer options. */
216 ot->srna, "apply_modifiers", true, "Apply Modifiers", "Apply modifiers to exported meshes");
217 RNA_def_enum(ot->srna,
218 "evaluation_mode",
219 io_ply_export_evaluation_mode,
221 "Object Properties",
222 "Determines properties like object visibility, modifiers etc., where they differ "
223 "for Render and Viewport");
224 RNA_def_boolean(ot->srna,
225 "export_selected_objects",
226 false,
227 "Export Selected Objects",
228 "Export only selected objects instead of all supported objects");
229 prop = RNA_def_string(ot->srna,
230 "collection",
231 nullptr,
232 MAX_ID_NAME - 2,
233 "Source Collection",
234 "Export only objects from this collection (and its children)");
236
237 RNA_def_boolean(ot->srna, "export_uv", true, "Export UVs", "");
239 ot->srna,
240 "export_normals",
241 false,
242 "Export Vertex Normals",
243 "Export specific vertex normals if available, export calculated normals otherwise");
244 RNA_def_enum(ot->srna,
245 "export_colors",
246 ply_vertex_colors_mode,
248 "Export Vertex Colors",
249 "Export vertex color attributes");
250 RNA_def_boolean(ot->srna,
251 "export_attributes",
252 true,
253 "Export Vertex Attributes",
254 "Export custom vertex attributes");
255 RNA_def_boolean(ot->srna,
256 "export_triangulated_mesh",
257 false,
258 "Export Triangulated Mesh",
259 "All ngons with four or more vertices will be triangulated. Meshes in "
260 "the scene will not be affected. Behaves like Triangulate Modifier with "
261 "ngon-method: \"Beauty\", quad-method: \"Shortest Diagonal\", min vertices: 4");
262 RNA_def_boolean(ot->srna,
263 "ascii_format",
264 false,
265 "ASCII Format",
266 "Export file in ASCII format, export as binary otherwise");
267
268 /* Only show `.ply` files by default. */
269 prop = RNA_def_string(ot->srna, "filter_glob", "*.ply", 0, "Extension Filter", "");
271}
272
273static wmOperatorStatus wm_ply_import_exec(bContext *C, wmOperator *op)
274{
275 PLYImportParams params;
276 params.forward_axis = eIOAxis(RNA_enum_get(op->ptr, "forward_axis"));
277 params.up_axis = eIOAxis(RNA_enum_get(op->ptr, "up_axis"));
278 params.use_scene_unit = RNA_boolean_get(op->ptr, "use_scene_unit");
279 params.global_scale = RNA_float_get(op->ptr, "global_scale");
280 params.merge_verts = RNA_boolean_get(op->ptr, "merge_verts");
281 params.import_attributes = RNA_boolean_get(op->ptr, "import_attributes");
282 params.vertex_colors = ePLYVertexColorMode(RNA_enum_get(op->ptr, "import_colors"));
283
284 params.reports = op->reports;
285
286 const auto paths = ed::io::paths_from_operator_properties(op->ptr);
287
288 if (paths.is_empty()) {
289 BKE_report(op->reports, RPT_ERROR, "No filepath given");
290 return OPERATOR_CANCELLED;
291 }
292 for (const auto &path : paths) {
293 STRNCPY(params.filepath, path.c_str());
294 PLY_import(C, params);
295 };
296
297 Scene *scene = CTX_data_scene(C);
302
303 return OPERATOR_FINISHED;
304}
305
306static void ui_ply_import_settings(const bContext *C, ui::Layout &layout, PointerRNA *ptr)
307{
308 layout.use_property_split_set(true);
309 layout.use_property_decorate_set(false);
310
311 if (ui::Layout *panel = layout.panel(C, "PLY_import_general", false, IFACE_("General"))) {
312 ui::Layout &col = panel->column(false);
313 col.prop(ptr, "global_scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
314 col.prop(ptr, "use_scene_unit", UI_ITEM_NONE, std::nullopt, ICON_NONE);
315 col.prop(ptr, "forward_axis", UI_ITEM_NONE, std::nullopt, ICON_NONE);
316 col.prop(ptr, "up_axis", UI_ITEM_NONE, std::nullopt, ICON_NONE);
317 }
318
319 if (ui::Layout *panel = layout.panel(C, "PLY_import_options", false, IFACE_("Options"))) {
320 ui::Layout &col = panel->column(false);
321 col.prop(ptr, "merge_verts", UI_ITEM_NONE, std::nullopt, ICON_NONE);
322 col.prop(ptr, "import_colors", UI_ITEM_NONE, std::nullopt, ICON_NONE);
323 }
324}
325
326static void wm_ply_import_draw(bContext *C, wmOperator *op)
327{
328 ui_ply_import_settings(C, *op->layout, op->ptr);
329}
330
332{
333 PropertyRNA *prop;
334
335 ot->name = "Import PLY";
336 ot->description = "Import an PLY file as an object";
337 ot->idname = "WM_OT_ply_import";
338
340 ot->exec = wm_ply_import_exec;
341 ot->ui = wm_ply_import_draw;
342 ot->poll = WM_operator_winactive;
343 ot->flag = OPTYPE_UNDO | OPTYPE_PRESET;
344
353
354 RNA_def_float(ot->srna, "global_scale", 1.0f, 1e-6f, 1e6f, "Scale", "", 0.001f, 1000.0f);
355 RNA_def_boolean(ot->srna,
356 "use_scene_unit",
357 false,
358 "Scene Unit",
359 "Apply current scene's unit (as defined by unit scale) to imported data");
360 prop = RNA_def_enum(ot->srna, "forward_axis", io_transform_axis, IO_AXIS_Y, "Forward Axis", "");
362 prop = RNA_def_enum(ot->srna, "up_axis", io_transform_axis, IO_AXIS_Z, "Up Axis", "");
364 RNA_def_boolean(ot->srna, "merge_verts", false, "Merge Vertices", "Merges vertices by distance");
365 RNA_def_enum(ot->srna,
366 "import_colors",
367 ply_vertex_colors_mode,
369 "Vertex Colors",
370 "Import vertex color attributes");
372 ot->srna, "import_attributes", true, "Vertex Attributes", "Import custom vertex attributes");
373
374 /* Only show `.ply` files by default. */
375 prop = RNA_def_string(ot->srna, "filter_glob", "*.ply", 0, "Extension Filter", "");
377}
378
379namespace ed::io {
381{
382 auto fh = std::make_unique<bke::FileHandlerType>();
383 STRNCPY_UTF8(fh->idname, "IO_FH_ply");
384 STRNCPY_UTF8(fh->import_operator, "WM_OT_ply_import");
385 STRNCPY_UTF8(fh->export_operator, "WM_OT_ply_export");
386 STRNCPY_UTF8(fh->label, "Stanford PLY");
387 STRNCPY_UTF8(fh->file_extensions_str, ".ply");
388 fh->poll_drop = poll_file_object_drop;
389 bke::file_handler_add(std::move(fh));
390}
391} // namespace ed::io
392} // namespace blender
393#endif /* WITH_IO_PLY */
#define FILE_MAX
#define STRNCPY_UTF8(dst, src)
#define IFACE_(msgid)
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define ND_OB_ACTIVE
Definition WM_types.hh:459
#define ND_OB_SELECT
Definition WM_types.hh:461
#define NC_SCENE
Definition WM_types.hh:391
#define ND_LAYER_CONTENT
Definition WM_types.hh:472
void file_handler_add(std::unique_ptr< FileHandlerType > file_handler)
bool poll_file_object_drop(const bContext *C, bke::FileHandlerType *)
Definition io_utils.cc:62
Vector< std::string > paths_from_operator_properties(PointerRNA *ptr)
Definition io_utils.cc:79
wmOperatorStatus filesel_drop_import_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_utils.cc:30
void ply_file_handler_add()
BMesh Iterators.
ePLYVertexColorMode
Definition IO_ply.hh:28
void io_ui_forward_axis_update(Main *, Scene *, PointerRNA *ptr)
char * STRNCPY(char(&dst)[N], const char *src)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, const int maxlen, const char *ui_name, const char *ui_description)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void PLY_export(bContext *C, const PLYExportParams &params)
Definition IO_ply.cc:32
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
bool RNA_struct_property_is_set_ex(PointerRNA *ptr, const char *identifier, bool use_ghost)
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)
void WM_OT_ply_export(wmOperatorType *ot)
void ED_outliner_select_sync_from_object_tag(bContext *C)
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
@ WM_FILESEL_DIRECTORY
Definition WM_api.hh:1330
@ WM_FILESEL_SHOW_PROPS
Definition WM_api.hh:1335
@ WM_FILESEL_FILES
Definition WM_api.hh:1333
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
void ED_fileselect_ensure_default_filepath(bContext *C, wmOperator *op, const char *extension)
Definition filesel.cc:1515
bool BKE_reports_contain(ReportList *reports, eReportType level)
Definition report.cc:394
@ DAG_EVAL_VIEWPORT
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
@ FILE_SAVE
Definition WM_api.hh:1342
@ FILE_OPENFILE
Definition WM_api.hh:1341
bool WM_operator_winactive(bContext *C)
Main * CTX_data_main(const bContext *C)
bool BLI_path_extension_check(const char *path, const char *ext) ATTR_NONNULL(1
@ PROP_HIDDEN
Definition RNA_types.hh:408
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
SpaceFile * CTX_wm_space_file(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
void PLY_import(bContext *C, const PLYImportParams &params)
Definition IO_ply.cc:39
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_PRESET
Definition WM_types.hh:203
std::string RNA_string_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
const EnumPropertyItem io_transform_axis[]
void io_ui_up_axis_update(Main *, Scene *, PointerRNA *ptr)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
float RNA_float_get(PointerRNA *ptr, const char *name)
void WM_event_add_fileselect(bContext *C, wmOperator *op)
void WM_OT_ply_import(wmOperatorType *ot)
char filepath[1024]
Definition BKE_main.hh:267
char file_base_for_tests[FILE_MAX]
Definition IO_ply.hh:38
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
Layout & column(bool align)