Blender V4.5
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.h"
17
18# include "WM_api.hh"
19# include "WM_types.hh"
20
21# include "DNA_space_types.h"
22
23# include "ED_fileselect.hh"
24# include "ED_outliner.hh"
25
26# include "RNA_access.hh"
27# include "RNA_define.hh"
28
29# include "BLT_translation.hh"
30
31# include "UI_interface.hh"
32# include "UI_resources.hh"
33
34# include "IO_orientation.hh"
35
36# include "IO_ply.hh"
37# include "io_ply_ops.hh"
38# include "io_utils.hh"
39
40static const EnumPropertyItem ply_vertex_colors_mode[] = {
41 {int(ePLYVertexColorMode::None), "NONE", 0, "None", "Do not import/export color attributes"},
43 "SRGB",
44 0,
45 "sRGB",
46 "Vertex colors in the file are in sRGB color space"},
48 "LINEAR",
49 0,
50 "Linear",
51 "Vertex colors in the file are in linear color space"},
52 {0, nullptr, 0, nullptr, nullptr}};
53
54static wmOperatorStatus wm_ply_export_invoke(bContext *C,
55 wmOperator *op,
56 const wmEvent * /*event*/)
57{
59
62}
63
64static wmOperatorStatus wm_ply_export_exec(bContext *C, wmOperator *op)
65{
66 if (!RNA_struct_property_is_set_ex(op->ptr, "filepath", false)) {
67 BKE_report(op->reports, RPT_ERROR, "No filepath given");
68 return OPERATOR_CANCELLED;
69 }
70 PLYExportParams export_params;
71 export_params.file_base_for_tests[0] = '\0';
72 RNA_string_get(op->ptr, "filepath", export_params.filepath);
73 export_params.blen_filepath = CTX_data_main(C)->filepath;
74
75 export_params.forward_axis = eIOAxis(RNA_enum_get(op->ptr, "forward_axis"));
76 export_params.up_axis = eIOAxis(RNA_enum_get(op->ptr, "up_axis"));
77 export_params.global_scale = RNA_float_get(op->ptr, "global_scale");
78 export_params.apply_modifiers = RNA_boolean_get(op->ptr, "apply_modifiers");
79
80 export_params.export_selected_objects = RNA_boolean_get(op->ptr, "export_selected_objects");
81 export_params.export_uv = RNA_boolean_get(op->ptr, "export_uv");
82 export_params.export_normals = RNA_boolean_get(op->ptr, "export_normals");
83 export_params.vertex_colors = ePLYVertexColorMode(RNA_enum_get(op->ptr, "export_colors"));
84 export_params.export_attributes = RNA_boolean_get(op->ptr, "export_attributes");
85 export_params.export_triangulated_mesh = RNA_boolean_get(op->ptr, "export_triangulated_mesh");
86 export_params.ascii_format = RNA_boolean_get(op->ptr, "ascii_format");
87
88 RNA_string_get(op->ptr, "collection", export_params.collection);
89
90 export_params.reports = op->reports;
91
92 PLY_export(C, export_params);
93
95 return OPERATOR_CANCELLED;
96 }
97
98 BKE_report(op->reports, RPT_INFO, "File exported successfully");
99 return OPERATOR_FINISHED;
100}
101
102static void wm_ply_export_draw(bContext *C, wmOperator *op)
103{
104 uiLayout *layout = op->layout;
105 PointerRNA *ptr = op->ptr;
106
107 uiLayoutSetPropSep(layout, true);
108 uiLayoutSetPropDecorate(layout, false);
109
110 if (uiLayout *panel = layout->panel(C, "PLY_export_general", false, IFACE_("General"))) {
111 uiLayout *col = &panel->column(false);
112
113 uiLayout *sub = &col->column(false, IFACE_("Format"));
114 sub->prop(ptr, "ascii_format", UI_ITEM_NONE, IFACE_("ASCII"), ICON_NONE);
115
116 /* The Selection only options only make sense when using regular export. */
117 if (CTX_wm_space_file(C)) {
118 sub = &col->column(false, IFACE_("Include"));
119 sub->prop(ptr, "export_selected_objects", UI_ITEM_NONE, IFACE_("Selection Only"), ICON_NONE);
120 }
121
122 col->prop(ptr, "global_scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
123 col->prop(ptr, "forward_axis", UI_ITEM_NONE, IFACE_("Forward Axis"), ICON_NONE);
124 col->prop(ptr, "up_axis", UI_ITEM_NONE, IFACE_("Up Axis"), ICON_NONE);
125 }
126
127 if (uiLayout *panel = layout->panel(C, "PLY_export_geometry", false, IFACE_("Geometry"))) {
128 uiLayout *col = &panel->column(false);
129
130 col->prop(ptr, "export_uv", UI_ITEM_NONE, IFACE_("UV Coordinates"), ICON_NONE);
131 col->prop(ptr, "export_normals", UI_ITEM_NONE, IFACE_("Vertex Normals"), ICON_NONE);
132 col->prop(ptr, "export_attributes", UI_ITEM_NONE, IFACE_("Vertex Attributes"), ICON_NONE);
133 col->prop(ptr, "export_colors", UI_ITEM_NONE, IFACE_("Vertex Colors"), ICON_NONE);
134
135 col->prop(
136 ptr, "export_triangulated_mesh", UI_ITEM_NONE, IFACE_("Triangulated Mesh"), ICON_NONE);
137 col->prop(ptr, "apply_modifiers", UI_ITEM_NONE, IFACE_("Apply Modifiers"), ICON_NONE);
138 }
139}
140
144static bool wm_ply_export_check(bContext * /*C*/, wmOperator *op)
145{
146 char filepath[FILE_MAX];
147 bool changed = false;
148 RNA_string_get(op->ptr, "filepath", filepath);
149
150 if (!BLI_path_extension_check(filepath, ".ply")) {
151 BLI_path_extension_ensure(filepath, FILE_MAX, ".ply");
152 RNA_string_set(op->ptr, "filepath", filepath);
153 changed = true;
154 }
155 return changed;
156}
157
159{
160 PropertyRNA *prop;
161
162 ot->name = "Export PLY";
163 ot->description = "Save the scene to a PLY file";
164 ot->idname = "WM_OT_ply_export";
165
166 ot->invoke = wm_ply_export_invoke;
167 ot->exec = wm_ply_export_exec;
169 ot->ui = wm_ply_export_draw;
170 ot->check = wm_ply_export_check;
171
172 ot->flag = OPTYPE_PRESET;
173
177 FILE_SAVE,
181
182 /* Object transform options. */
183 prop = RNA_def_enum(ot->srna, "forward_axis", io_transform_axis, IO_AXIS_Y, "Forward Axis", "");
185 prop = RNA_def_enum(ot->srna, "up_axis", io_transform_axis, IO_AXIS_Z, "Up Axis", "");
188 ot->srna,
189 "global_scale",
190 1.0f,
191 0.0001f,
192 10000.0f,
193 "Scale",
194 "Value by which to enlarge or shrink the objects with respect to the world's origin",
195 0.0001f,
196 10000.0f);
197 /* File Writer options. */
199 ot->srna, "apply_modifiers", true, "Apply Modifiers", "Apply modifiers to exported meshes");
200 RNA_def_boolean(ot->srna,
201 "export_selected_objects",
202 false,
203 "Export Selected Objects",
204 "Export only selected objects instead of all supported objects");
205 prop = RNA_def_string(ot->srna,
206 "collection",
207 nullptr,
209 "Source Collection",
210 "Export only objects from this collection (and its children)");
212
213 RNA_def_boolean(ot->srna, "export_uv", true, "Export UVs", "");
215 ot->srna,
216 "export_normals",
217 false,
218 "Export Vertex Normals",
219 "Export specific vertex normals if available, export calculated normals otherwise");
220 RNA_def_enum(ot->srna,
221 "export_colors",
222 ply_vertex_colors_mode,
224 "Export Vertex Colors",
225 "Export vertex color attributes");
226 RNA_def_boolean(ot->srna,
227 "export_attributes",
228 true,
229 "Export Vertex Attributes",
230 "Export custom vertex attributes");
231 RNA_def_boolean(ot->srna,
232 "export_triangulated_mesh",
233 false,
234 "Export Triangulated Mesh",
235 "All ngons with four or more vertices will be triangulated. Meshes in "
236 "the scene will not be affected. Behaves like Triangulate Modifier with "
237 "ngon-method: \"Beauty\", quad-method: \"Shortest Diagonal\", min vertices: 4");
238 RNA_def_boolean(ot->srna,
239 "ascii_format",
240 false,
241 "ASCII Format",
242 "Export file in ASCII format, export as binary otherwise");
243
244 /* Only show `.ply` files by default. */
245 prop = RNA_def_string(ot->srna, "filter_glob", "*.ply", 0, "Extension Filter", "");
247}
248
249static wmOperatorStatus wm_ply_import_exec(bContext *C, wmOperator *op)
250{
252 params.forward_axis = eIOAxis(RNA_enum_get(op->ptr, "forward_axis"));
253 params.up_axis = eIOAxis(RNA_enum_get(op->ptr, "up_axis"));
254 params.use_scene_unit = RNA_boolean_get(op->ptr, "use_scene_unit");
255 params.global_scale = RNA_float_get(op->ptr, "global_scale");
256 params.merge_verts = RNA_boolean_get(op->ptr, "merge_verts");
257 params.import_attributes = RNA_boolean_get(op->ptr, "import_attributes");
258 params.vertex_colors = ePLYVertexColorMode(RNA_enum_get(op->ptr, "import_colors"));
259
260 params.reports = op->reports;
261
263
264 if (paths.is_empty()) {
265 BKE_report(op->reports, RPT_ERROR, "No filepath given");
266 return OPERATOR_CANCELLED;
267 }
268 for (const auto &path : paths) {
269 STRNCPY(params.filepath, path.c_str());
271 };
272
273 Scene *scene = CTX_data_scene(C);
278
279 return OPERATOR_FINISHED;
280}
281
282static void ui_ply_import_settings(const bContext *C, uiLayout *layout, PointerRNA *ptr)
283{
284 uiLayoutSetPropSep(layout, true);
285 uiLayoutSetPropDecorate(layout, false);
286
287 if (uiLayout *panel = layout->panel(C, "PLY_import_general", false, IFACE_("General"))) {
288 uiLayout *col = &panel->column(false);
289 col->prop(ptr, "global_scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
290 col->prop(ptr, "use_scene_unit", UI_ITEM_NONE, std::nullopt, ICON_NONE);
291 col->prop(ptr, "forward_axis", UI_ITEM_NONE, std::nullopt, ICON_NONE);
292 col->prop(ptr, "up_axis", UI_ITEM_NONE, std::nullopt, ICON_NONE);
293 }
294
295 if (uiLayout *panel = layout->panel(C, "PLY_import_options", false, IFACE_("Options"))) {
296 uiLayout *col = &panel->column(false);
297 col->prop(ptr, "merge_verts", UI_ITEM_NONE, std::nullopt, ICON_NONE);
298 col->prop(ptr, "import_colors", UI_ITEM_NONE, std::nullopt, ICON_NONE);
299 }
300}
301
302static void wm_ply_import_draw(bContext *C, wmOperator *op)
303{
304 ui_ply_import_settings(C, op->layout, op->ptr);
305}
306
308{
309 PropertyRNA *prop;
310
311 ot->name = "Import PLY";
312 ot->description = "Import an PLY file as an object";
313 ot->idname = "WM_OT_ply_import";
314
316 ot->exec = wm_ply_import_exec;
317 ot->ui = wm_ply_import_draw;
319 ot->flag = OPTYPE_UNDO | OPTYPE_PRESET;
320
329
330 RNA_def_float(ot->srna, "global_scale", 1.0f, 1e-6f, 1e6f, "Scale", "", 0.001f, 1000.0f);
331 RNA_def_boolean(ot->srna,
332 "use_scene_unit",
333 false,
334 "Scene Unit",
335 "Apply current scene's unit (as defined by unit scale) to imported data");
336 prop = RNA_def_enum(ot->srna, "forward_axis", io_transform_axis, IO_AXIS_Y, "Forward Axis", "");
338 prop = RNA_def_enum(ot->srna, "up_axis", io_transform_axis, IO_AXIS_Z, "Up Axis", "");
340 RNA_def_boolean(ot->srna, "merge_verts", false, "Merge Vertices", "Merges vertices by distance");
341 RNA_def_enum(ot->srna,
342 "import_colors",
343 ply_vertex_colors_mode,
345 "Vertex Colors",
346 "Import vertex color attributes");
348 ot->srna, "import_attributes", true, "Vertex Attributes", "Import custom vertex attributes");
349
350 /* Only show `.ply` files by default. */
351 prop = RNA_def_string(ot->srna, "filter_glob", "*.ply", 0, "Extension Filter", "");
353}
354
355namespace blender::ed::io {
357{
358 auto fh = std::make_unique<blender::bke::FileHandlerType>();
359 STRNCPY(fh->idname, "IO_FH_ply");
360 STRNCPY(fh->import_operator, "WM_OT_ply_import");
361 STRNCPY(fh->export_operator, "WM_OT_ply_export");
362 STRNCPY(fh->label, "Stanford PLY");
363 STRNCPY(fh->file_extensions_str, ".ply");
364 fh->poll_drop = poll_file_object_drop;
365 bke::file_handler_add(std::move(fh));
366}
367} // namespace blender::ed::io
368
369#endif /* WITH_IO_PLY */
SpaceFile * CTX_wm_space_file(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
Main * CTX_data_main(const bContext *C)
bool BKE_reports_contain(ReportList *reports, eReportType level)
Definition report.cc:343
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:126
#define FILE_MAX
bool BLI_path_extension_check(const char *path, const char *ext) ATTR_NONNULL(1
bool BLI_path_extension_ensure(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:688
#define IFACE_(msgid)
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_FOLDER
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
void ED_fileselect_ensure_default_filepath(bContext *C, wmOperator *op, const char *extension)
Definition filesel.cc:1490
void ED_outliner_select_sync_from_object_tag(bContext *C)
eIOAxis
@ IO_AXIS_Y
@ IO_AXIS_Z
void PLY_import(bContext *C, const PLYImportParams &params)
Definition IO_ply.cc:34
void PLY_export(bContext *C, const PLYExportParams &params)
Definition IO_ply.cc:27
ePLYVertexColorMode
Definition IO_ply.hh:21
@ PROP_HIDDEN
Definition RNA_types.hh:324
#define C
Definition RandGen.cpp:29
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
#define UI_ITEM_NONE
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
@ WM_FILESEL_FILES
Definition WM_api.hh:1076
@ WM_FILESEL_DIRECTORY
Definition WM_api.hh:1073
@ WM_FILESEL_SHOW_PROPS
Definition WM_api.hh:1078
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1075
@ FILE_OPENFILE
Definition WM_api.hh:1084
@ FILE_SAVE
Definition WM_api.hh:1085
#define ND_OB_ACTIVE
Definition WM_types.hh:437
@ OPTYPE_PRESET
Definition WM_types.hh:195
@ OPTYPE_UNDO
Definition WM_types.hh:182
#define ND_OB_SELECT
Definition WM_types.hh:439
#define NC_SCENE
Definition WM_types.hh:375
#define ND_LAYER_CONTENT
Definition WM_types.hh:450
uint col
#define MAX_IDPROP_NAME
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void WM_OT_ply_export(wmOperatorType *ot)
void WM_OT_ply_import(wmOperatorType *ot)
void file_handler_add(std::unique_ptr< FileHandlerType > file_handler)
bool poll_file_object_drop(const bContext *C, blender::bke::FileHandlerType *)
Definition io_utils.cc:57
Vector< std::string > paths_from_operator_properties(PointerRNA *ptr)
Definition io_utils.cc:74
wmOperatorStatus filesel_drop_import_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_utils.cc:25
void ply_file_handler_add()
const EnumPropertyItem io_transform_axis[]
void io_ui_forward_axis_update(Main *, Scene *, PointerRNA *ptr)
void io_ui_up_axis_update(Main *, Scene *, PointerRNA *ptr)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
bool RNA_struct_property_is_set_ex(PointerRNA *ptr, const char *identifier, bool use_ghost)
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
float RNA_float_get(PointerRNA *ptr, const char *name)
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
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)
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)
void RNA_def_property_update_runtime(PropertyRNA *prop, RNAPropertyUpdateFunc func)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, const char *ui_name, const char *ui_description)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
char filepath[1024]
Definition BKE_main.hh:155
eIOAxis forward_axis
Definition IO_ply.hh:40
const char * blen_filepath
Definition IO_ply.hh:34
bool ascii_format
Definition IO_ply.hh:37
bool apply_modifiers
Definition IO_ply.hh:46
bool export_attributes
Definition IO_ply.hh:50
char collection[MAX_IDPROP_NAME]
Definition IO_ply.hh:52
eIOAxis up_axis
Definition IO_ply.hh:41
ReportList * reports
Definition IO_ply.hh:54
ePLYVertexColorMode vertex_colors
Definition IO_ply.hh:49
char file_base_for_tests[FILE_MAX]
Definition IO_ply.hh:31
char filepath[FILE_MAX]
Definition IO_ply.hh:29
float global_scale
Definition IO_ply.hh:42
bool export_triangulated_mesh
Definition IO_ply.hh:51
bool export_selected_objects
Definition IO_ply.hh:45
bool export_normals
Definition IO_ply.hh:48
PanelLayout panel(const bContext *C, blender::StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
struct ReportList * reports
struct uiLayout * layout
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
PointerRNA * ptr
Definition wm_files.cc:4226
wmOperatorType * ot
Definition wm_files.cc:4225
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)
bool WM_operator_winactive(bContext *C)