Blender V5.0
object_volume.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstring>
10
11#include "BLI_listbase.h"
12#include "BLI_math_constants.h"
13#include "BLI_path_utils.hh"
14#include "BLI_string.h"
15
16#include "DNA_object_types.h"
17#include "DNA_volume_types.h"
18
19#include "RNA_access.hh"
20#include "RNA_define.hh"
21
22#include "BKE_context.hh"
23#include "BKE_lib_id.hh"
24#include "BKE_main.hh"
25#include "BKE_report.hh"
26#include "BKE_volume.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "ED_image.hh"
32#include "ED_object.hh"
33#include "ED_screen.hh"
34
35#include "object_intern.hh"
36
37namespace blender::ed::object {
38
39/* Volume Add */
40
41static Object *object_volume_add(bContext *C, wmOperator *op, const char *name)
42{
43 ushort local_view_bits;
44 float loc[3], rot[3];
45
46 add_generic_get_opts(C, op, 'Z', loc, rot, nullptr, nullptr, &local_view_bits, nullptr);
47
48 return add_type(C, OB_VOLUME, name, loc, rot, false, local_view_bits);
49}
50
55
57{
58 /* identifiers */
59 ot->name = "Add Volume";
60 ot->description = "Add a volume object to the scene";
61 ot->idname = "OBJECT_OT_volume_add";
62
63 /* API callbacks. */
66
67 /* flags */
69
70 add_generic_props(ot, false);
71}
72
73/* Volume Import */
74
76{
77 Main *bmain = CTX_data_main(C);
78 const bool is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
79 bool imported = false;
80
81 const char *blendfile_path = BKE_main_blendfile_path(bmain);
82 ListBase ranges = ED_image_filesel_detect_sequences(blendfile_path, blendfile_path, op, false);
83 LISTBASE_FOREACH (ImageFrameRange *, range, &ranges) {
84 char filepath[FILE_MAX];
85 BLI_path_split_file_part(range->filepath, filepath, sizeof(filepath));
87
88 Object *object = object_volume_add(C, op, filepath);
89 Volume *volume = (Volume *)object->data;
90
91 STRNCPY(volume->filepath, range->filepath);
92 if (is_relative_path) {
94 }
95
96 if (!BKE_volume_load(volume, bmain)) {
99 "Volume \"%s\" failed to load: %s",
100 filepath,
102 BKE_id_delete(bmain, &object->id);
103 BKE_id_delete(bmain, &volume->id);
104 continue;
105 }
106 if (BKE_volume_is_points_only(volume)) {
109 "Volume \"%s\" contains points, only voxel grids are supported",
110 filepath);
111 BKE_id_delete(bmain, &object->id);
112 BKE_id_delete(bmain, &volume->id);
113 continue;
114 }
115
116 /* Set sequence parameters after trying to load the first frame, for file validation we want
117 * to use a consistent frame rather than whatever corresponds to the current scene frame. */
118 volume->is_sequence = (range->length > 1);
119 volume->frame_duration = (volume->is_sequence) ? range->length : 0;
120 volume->frame_start = 1;
121 volume->frame_offset = (volume->is_sequence) ? range->offset - 1 : 0;
122
123 if (BKE_volume_is_y_up(volume)) {
124 object->rot[0] += M_PI_2;
125 }
126
127 BKE_volume_unload(volume);
128
129 imported = true;
130
131 BLI_freelistN(&range->frames);
132 }
133 BLI_freelistN(&ranges);
134
135 return (imported) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
136}
137
139 wmOperator *op,
140 const wmEvent * /*event*/)
141{
142 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
143 return volume_import_exec(C, op);
144 }
145
146 RNA_string_set(op->ptr, "filepath", U.textudir);
148
150}
151
153{
154 /* identifiers */
155 ot->name = "Import OpenVDB Volume";
156 ot->description = "Import OpenVDB volume file";
157 ot->idname = "OBJECT_OT_volume_import";
158
159 /* API callbacks. */
160 ot->exec = volume_import_exec;
161 ot->invoke = volume_import_invoke;
162
163 /* flags */
165
166 /* properties */
175
177 ot->srna,
178 "use_sequence_detection",
179 true,
180 "Detect Sequences",
181 "Automatically detect animated sequences in selected volume files (based on file names)");
182
183 add_generic_props(ot, false);
184}
185
186} // namespace blender::ed::object
Main * CTX_data_main(const bContext *C)
void BKE_id_delete(Main *bmain, void *idv) ATTR_NONNULL()
const char * BKE_main_blendfile_path(const Main *bmain) ATTR_NONNULL()
Definition main.cc:887
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_WARNING
Definition BKE_report.hh:38
Volume data-block.
bool BKE_volume_is_y_up(const Volume *volume)
bool BKE_volume_load(const Volume *volume, const Main *bmain)
bool BKE_volume_is_points_only(const Volume *volume)
void BKE_volume_unload(Volume *volume)
const char * BKE_volume_grids_error_msg(const Volume *volume)
#define LISTBASE_FOREACH(type, var, list)
void void BLI_freelistN(ListBase *listbase) ATTR_NONNULL(1)
Definition listbase.cc:497
#define M_PI_2
bool bool BLI_path_extension_strip(char *path) ATTR_NONNULL(1)
#define FILE_MAX
void void void BLI_path_split_file_part(const char *filepath, char *file, size_t file_maxncpy) ATTR_NONNULL(1
bool void BLI_path_rel(char path[FILE_MAX], const char *basepath) ATTR_NONNULL(1)
char * STRNCPY(char(&dst)[N], const char *src)
Definition BLI_string.h:693
unsigned short ushort
Object is a sort of wrapper for general info.
@ OB_VOLUME
@ FILE_SORT_DEFAULT
@ FILE_SPECIAL
@ FILE_TYPE_VOLUME
@ FILE_TYPE_FOLDER
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
ListBase ED_image_filesel_detect_sequences(blender::StringRefNull blendfile_path, blender::StringRefNull root_path, wmOperator *op, bool detect_udim)
bool ED_operator_objectmode(bContext *C)
#define C
Definition RandGen.cpp:29
@ WM_FILESEL_FILES
Definition WM_api.hh:1125
@ WM_FILESEL_DIRECTORY
Definition WM_api.hh:1122
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1121
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1124
@ FILE_OPENFILE
Definition WM_api.hh:1133
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define U
#define rot(x, k)
static wmOperatorStatus volume_import_invoke(bContext *C, wmOperator *op, const wmEvent *)
Object * add_type(bContext *C, int type, const char *name, const float loc[3], const float rot[3], bool enter_editmode, unsigned short local_view_bits) ATTR_NONNULL(1) ATTR_RETURNS_NONNULL
static wmOperatorStatus volume_import_exec(bContext *C, wmOperator *op)
static Object * object_volume_add(bContext *C, wmOperator *op, const char *name)
void OBJECT_OT_volume_add(wmOperatorType *ot)
void OBJECT_OT_volume_import(wmOperatorType *ot)
static wmOperatorStatus object_volume_add_exec(bContext *C, wmOperator *op)
void add_generic_props(wmOperatorType *ot, bool do_editmode)
void add_generic_get_opts(bContext *C, wmOperator *op, char view_align_axis, float r_loc[3], float r_rot[3], float r_scale[3], bool *r_enter_editmode, unsigned short *r_local_view_bits, bool *r_is_view_aligned)
const char * name
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
bool RNA_boolean_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)
int frame_duration
char filepath[1024]
char is_sequence
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition wm_files.cc:4237
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)