Blender  V2.93
object_volume.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include <string.h>
25 
26 #include "BLI_listbase.h"
27 #include "BLI_math_base.h"
28 #include "BLI_path_util.h"
29 #include "BLI_string.h"
30 
31 #include "DNA_object_types.h"
32 #include "DNA_volume_types.h"
33 
34 #include "RNA_access.h"
35 #include "RNA_define.h"
36 
37 #include "BKE_context.h"
38 #include "BKE_lib_id.h"
39 #include "BKE_main.h"
40 #include "BKE_report.h"
41 #include "BKE_volume.h"
42 
43 #include "WM_api.h"
44 #include "WM_types.h"
45 
46 #include "ED_image.h"
47 #include "ED_object.h"
48 #include "ED_screen.h"
49 
50 #include "object_intern.h"
51 
52 /* Volume Add */
53 
54 static Object *object_volume_add(bContext *C, wmOperator *op, const char *name)
55 {
56  ushort local_view_bits;
57  float loc[3], rot[3];
58 
59  if (!ED_object_add_generic_get_opts(C, op, 'Z', loc, rot, NULL, NULL, &local_view_bits, NULL)) {
60  return NULL;
61  }
62  return ED_object_add_type(C, OB_VOLUME, name, loc, rot, false, local_view_bits);
63 }
64 
66 {
68 }
69 
71 {
72  /* identifiers */
73  ot->name = "Add Volume";
74  ot->description = "Add a volume object to the scene";
75  ot->idname = "OBJECT_OT_volume_add";
76 
77  /* api callbacks */
80 
81  /* flags */
83 
85 }
86 
87 /* Volume Import */
88 
90 {
91  Main *bmain = CTX_data_main(C);
92  const bool is_relative_path = RNA_boolean_get(op->ptr, "relative_path");
93  bool imported = false;
94 
95  ListBase ranges = ED_image_filesel_detect_sequences(bmain, op, false);
96  LISTBASE_FOREACH (ImageFrameRange *, range, &ranges) {
97  char filename[FILE_MAX];
98  BLI_split_file_part(range->filepath, filename, sizeof(filename));
99  BLI_path_extension_replace(filename, sizeof(filename), "");
100 
101  Object *object = object_volume_add(C, op, filename);
102  Volume *volume = (Volume *)object->data;
103 
104  STRNCPY(volume->filepath, range->filepath);
105  if (is_relative_path) {
107  }
108 
109  if (!BKE_volume_load(volume, bmain)) {
110  BKE_reportf(op->reports,
111  RPT_WARNING,
112  "Volume \"%s\" failed to load: %s",
113  filename,
115  BKE_id_delete(bmain, &object->id);
116  BKE_id_delete(bmain, &volume->id);
117  continue;
118  }
119  if (BKE_volume_is_points_only(volume)) {
120  BKE_reportf(op->reports,
121  RPT_WARNING,
122  "Volume \"%s\" contains points, only voxel grids are supported",
123  filename);
124  BKE_id_delete(bmain, &object->id);
125  BKE_id_delete(bmain, &volume->id);
126  continue;
127  }
128 
129  /* Set sequence parameters after trying to load the first frame, for file validation we want
130  * to use a consistent frame rather than whatever corresponds to the current scene frame. */
131  volume->is_sequence = (range->length > 1);
132  volume->frame_duration = (volume->is_sequence) ? range->length : 0;
133  volume->frame_start = 1;
134  volume->frame_offset = (volume->is_sequence) ? range->offset - 1 : 0;
135 
136  if (BKE_volume_is_y_up(volume)) {
137  object->rot[0] += M_PI_2;
138  }
139 
140  BKE_volume_unload(volume);
141 
142  imported = true;
143  }
144  BLI_freelistN(&ranges);
145 
146  return (imported) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
147 }
148 
149 static int volume_import_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
150 {
151  if (RNA_struct_property_is_set(op->ptr, "filepath")) {
152  return volume_import_exec(C, op);
153  }
154 
155  RNA_string_set(op->ptr, "filepath", U.textudir);
157 
158  return OPERATOR_RUNNING_MODAL;
159 }
160 
161 /* called by other space types too */
163 {
164  /* identifiers */
165  ot->name = "Import OpenVDB Volume";
166  ot->description = "Import OpenVDB volume file";
167  ot->idname = "OBJECT_OT_volume_import";
168 
169  /* api callbacks */
172 
173  /* flags */
175 
176  /* properties */
179  FILE_SPECIAL,
185 
187  ot->srna,
188  "use_sequence_detection",
189  true,
190  "Detect Sequences",
191  "Automatically detect animated sequences in selected volume files (based on file names)");
192 
194 }
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
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, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Volume datablock.
void BKE_volume_unload(struct Volume *volume)
Definition: volume.cc:849
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:172
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:547
#define M_PI_2
Definition: BLI_math_base.h:41
#define FILE_MAX
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1571
void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL()
Definition: path_util.c:519
void BLI_split_file_part(const char *string, char *file, const size_t filelen)
Definition: path_util.c:1690
#define STRNCPY(dst, src)
Definition: BLI_string.h:163
unsigned short ushort
Definition: BLI_sys_types.h:84
#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_OPENFILE
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
ListBase ED_image_filesel_detect_sequences(struct Main *bmain, struct wmOperator *op, const bool detect_udim)
struct Object * ED_object_add_type(struct bContext *C, const int type, const char *name, const float loc[3], const float rot[3], const bool enter_editmode, const unsigned short local_view_bits) ATTR_NONNULL(1) ATTR_RETURNS_NONNULL
Definition: object_add.c:659
void ED_object_add_generic_props(struct wmOperatorType *ot, bool do_editmode)
Definition: object_add.c:397
bool ED_object_add_generic_get_opts(struct bContext *C, struct wmOperator *op, const char view_align_axis, float loc[3], float rot[3], float scale[3], bool *enter_editmode, unsigned short *local_view_bits, bool *is_view_aligned)
Definition: object_add.c:455
bool ED_operator_objectmode(struct bContext *C)
Definition: screen_ops.c:201
#define C
Definition: RandGen.cpp:39
#define WM_FILESEL_DIRECTORY
Definition: WM_api.h:535
#define WM_FILESEL_RELPATH
Definition: WM_api.h:533
#define WM_FILESEL_FILEPATH
Definition: WM_api.h:537
#define WM_FILESEL_FILES
Definition: WM_api.h:538
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
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:89
static int object_volume_add_exec(bContext *C, wmOperator *op)
Definition: object_volume.c:65
static Object * object_volume_add(bContext *C, wmOperator *op, const char *name)
Definition: object_volume.c:54
static int volume_import_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
void OBJECT_OT_volume_add(wmOperatorType *ot)
Definition: object_volume.c:70
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:6685
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6261
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, bool default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3481
Definition: BKE_main.h:116
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:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display, short sort)