Blender  V3.3
object_volume.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2008 Blender Foundation. All rights reserved. */
3 
8 #include <string.h>
9 
10 #include "BLI_listbase.h"
11 #include "BLI_math_base.h"
12 #include "BLI_path_util.h"
13 #include "BLI_string.h"
14 
15 #include "DNA_object_types.h"
16 #include "DNA_volume_types.h"
17 
18 #include "RNA_access.h"
19 #include "RNA_define.h"
20 
21 #include "BKE_context.h"
22 #include "BKE_lib_id.h"
23 #include "BKE_main.h"
24 #include "BKE_report.h"
25 #include "BKE_volume.h"
26 
27 #include "WM_api.h"
28 #include "WM_types.h"
29 
30 #include "ED_image.h"
31 #include "ED_object.h"
32 #include "ED_screen.h"
33 
34 #include "object_intern.h"
35 
36 /* Volume Add */
37 
38 static Object *object_volume_add(bContext *C, wmOperator *op, const char *name)
39 {
40  ushort local_view_bits;
41  float loc[3], rot[3];
42 
43  if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, NULL, &local_view_bits, NULL)) {
44  return NULL;
45  }
46  return ED_object_add_type(C, OB_VOLUME, name, loc, rot, false, local_view_bits);
47 }
48 
50 {
52 }
53 
55 {
56  /* identifiers */
57  ot->name = "Add Volume";
58  ot->description = "Add a volume object to the scene";
59  ot->idname = "OBJECT_OT_volume_add";
60 
61  /* api callbacks */
64 
65  /* flags */
67 
69 }
70 
71 /* Volume Import */
72 
74 {
75  Main *bmain = CTX_data_main(C);
76  const bool is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
77  bool imported = false;
78 
79  ListBase ranges = ED_image_filesel_detect_sequences(bmain, op, false);
80  LISTBASE_FOREACH (ImageFrameRange *, range, &ranges) {
81  char filename[FILE_MAX];
82  BLI_split_file_part(range->filepath, filename, sizeof(filename));
83  BLI_path_extension_replace(filename, sizeof(filename), "");
84 
85  Object *object = object_volume_add(C, op, filename);
86  Volume *volume = (Volume *)object->data;
87 
88  STRNCPY(volume->filepath, range->filepath);
89  if (is_relative_path) {
91  }
92 
93  if (!BKE_volume_load(volume, bmain)) {
94  BKE_reportf(op->reports,
96  "Volume \"%s\" failed to load: %s",
97  filename,
99  BKE_id_delete(bmain, &object->id);
100  BKE_id_delete(bmain, &volume->id);
101  continue;
102  }
103  if (BKE_volume_is_points_only(volume)) {
104  BKE_reportf(op->reports,
105  RPT_WARNING,
106  "Volume \"%s\" contains points, only voxel grids are supported",
107  filename);
108  BKE_id_delete(bmain, &object->id);
109  BKE_id_delete(bmain, &volume->id);
110  continue;
111  }
112 
113  /* Set sequence parameters after trying to load the first frame, for file validation we want
114  * to use a consistent frame rather than whatever corresponds to the current scene frame. */
115  volume->is_sequence = (range->length > 1);
116  volume->frame_duration = (volume->is_sequence) ? range->length : 0;
117  volume->frame_start = 1;
118  volume->frame_offset = (volume->is_sequence) ? range->offset - 1 : 0;
119 
120  if (BKE_volume_is_y_up(volume)) {
121  object->rot[0] += M_PI_2;
122  }
123 
124  BKE_volume_unload(volume);
125 
126  imported = true;
127  }
128  BLI_freelistN(&ranges);
129 
130  return (imported) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
131 }
132 
133 static int volume_import_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
134 {
135  if (RNA_struct_property_is_set(op->ptr, "filepath")) {
136  return volume_import_exec(C, op);
137  }
138 
139  RNA_string_set(op->ptr, "filepath", U.textudir);
141 
142  return OPERATOR_RUNNING_MODAL;
143 }
144 
146 {
147  /* identifiers */
148  ot->name = "Import OpenVDB Volume";
149  ot->description = "Import OpenVDB volume file";
150  ot->idname = "OBJECT_OT_volume_import";
151 
152  /* api callbacks */
155 
156  /* flags */
158 
159  /* properties */
162  FILE_SPECIAL,
168 
170  ot->srna,
171  "use_sequence_detection",
172  true,
173  "Detect Sequences",
174  "Automatically detect animated sequences in selected volume files (based on file names)");
175 
177 }
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
void BKE_id_delete(struct Main *bmain, void *idv) ATTR_NONNULL()
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Volume data-block.
void BKE_volume_unload(struct Volume *volume)
Definition: volume.cc:930
bool BKE_volume_is_points_only(const struct Volume *volume)
const char * BKE_volume_grids_error_msg(const struct Volume *volume)
bool BKE_volume_load(const struct Volume *volume, const struct Main *bmain)
bool BKE_volume_is_y_up(const struct Volume *volume)
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:466
#define M_PI_2
Definition: BLI_math_base.h:23
#define FILE_MAX
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1393
void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL()
Definition: path_util.c:450
void BLI_split_file_part(const char *string, char *file, size_t filelen)
Definition: path_util.c:1495
#define STRNCPY(dst, src)
Definition: BLI_string.h:483
unsigned short ushort
Definition: BLI_sys_types.h:68
#define UNUSED(x)
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(struct Main *bmain, struct wmOperator *op, bool detect_udim)
struct Object * ED_object_add_type(struct 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
Definition: object_add.cc:668
bool ED_object_add_generic_get_opts(struct bContext *C, struct 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)
Definition: object_add.cc:457
void ED_object_add_generic_props(struct wmOperatorType *ot, bool do_editmode)
Definition: object_add.cc:399
bool ED_operator_objectmode(struct bContext *C)
Definition: screen_ops.c:186
#define C
Definition: RandGen.cpp:25
@ WM_FILESEL_FILES
Definition: WM_api.h:756
@ WM_FILESEL_DIRECTORY
Definition: WM_api.h:753
@ WM_FILESEL_RELPATH
Definition: WM_api.h:752
@ WM_FILESEL_FILEPATH
Definition: WM_api.h:755
@ FILE_OPENFILE
Definition: WM_api.h:764
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
unsigned int U
Definition: btGjkEpa3.h:78
#define rot(x, k)
void OBJECT_OT_volume_import(wmOperatorType *ot)
static int volume_import_exec(bContext *C, wmOperator *op)
Definition: object_volume.c:73
static int object_volume_add_exec(bContext *C, wmOperator *op)
Definition: object_volume.c:49
static Object * object_volume_add(bContext *C, wmOperator *op, const char *name)
Definition: object_volume.c:38
static int volume_import_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
void OBJECT_OT_volume_add(wmOperatorType *ot)
Definition: object_volume.c:54
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:5155
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:5301
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:4863
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3493
Definition: BKE_main.h:121
void * data
int frame_duration
char filepath[1024]
int frame_start
char is_sequence
int frame_offset
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:919
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition: wm_files.c:3479
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)