Blender  V2.93
io_usd.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) 2019 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #ifdef WITH_USD
25 # include "DNA_space_types.h"
26 
27 # include "BKE_context.h"
28 # include "BKE_main.h"
29 # include "BKE_report.h"
30 
31 # include "BLI_path_util.h"
32 # include "BLI_string.h"
33 # include "BLI_utildefines.h"
34 
35 # include "BLT_translation.h"
36 
37 # include "MEM_guardedalloc.h"
38 
39 # include "RNA_access.h"
40 # include "RNA_define.h"
41 
42 # include "UI_interface.h"
43 # include "UI_resources.h"
44 
45 # include "WM_api.h"
46 # include "WM_types.h"
47 
48 # include "DEG_depsgraph.h"
49 
50 # include "io_usd.h"
51 # include "usd.h"
52 
53 const EnumPropertyItem rna_enum_usd_export_evaluation_mode_items[] = {
55  "RENDER",
56  0,
57  "Render",
58  "Use Render settings for object visibility, modifier settings, etc"},
60  "VIEWPORT",
61  0,
62  "Viewport",
63  "Use Viewport settings for object visibility, modifier settings, etc"},
64  {0, NULL, 0, NULL, NULL},
65 };
66 
67 /* Stored in the wmOperator's customdata field to indicate it should run as a background job.
68  * This is set when the operator is invoked, and not set when it is only executed. */
69 enum { AS_BACKGROUND_JOB = 1 };
70 typedef struct eUSDOperatorOptions {
71  bool as_background_job;
72 } eUSDOperatorOptions;
73 
74 static int wm_usd_export_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
75 {
76  eUSDOperatorOptions *options = MEM_callocN(sizeof(eUSDOperatorOptions), "eUSDOperatorOptions");
77  options->as_background_job = true;
78  op->customdata = options;
79 
80  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
81  Main *bmain = CTX_data_main(C);
82  char filepath[FILE_MAX];
83  const char *main_blendfile_path = BKE_main_blendfile_path(bmain);
84 
85  if (main_blendfile_path[0] == '\0') {
86  BLI_strncpy(filepath, "untitled", sizeof(filepath));
87  }
88  else {
89  BLI_strncpy(filepath, main_blendfile_path, sizeof(filepath));
90  }
91 
92  BLI_path_extension_replace(filepath, sizeof(filepath), ".usdc");
93  RNA_string_set(op->ptr, "filepath", filepath);
94  }
95 
97 
99 }
100 
101 static int wm_usd_export_exec(bContext *C, wmOperator *op)
102 {
103  if (!RNA_struct_property_is_set(op->ptr, "filepath")) {
104  BKE_report(op->reports, RPT_ERROR, "No filename given");
105  return OPERATOR_CANCELLED;
106  }
107 
108  char filename[FILE_MAX];
109  RNA_string_get(op->ptr, "filepath", filename);
110 
111  eUSDOperatorOptions *options = (eUSDOperatorOptions *)op->customdata;
112  const bool as_background_job = (options != NULL && options->as_background_job);
114 
115  const bool selected_objects_only = RNA_boolean_get(op->ptr, "selected_objects_only");
116  const bool visible_objects_only = RNA_boolean_get(op->ptr, "visible_objects_only");
117  const bool export_animation = RNA_boolean_get(op->ptr, "export_animation");
118  const bool export_hair = RNA_boolean_get(op->ptr, "export_hair");
119  const bool export_uvmaps = RNA_boolean_get(op->ptr, "export_uvmaps");
120  const bool export_normals = RNA_boolean_get(op->ptr, "export_normals");
121  const bool export_materials = RNA_boolean_get(op->ptr, "export_materials");
122  const bool use_instancing = RNA_boolean_get(op->ptr, "use_instancing");
123  const bool evaluation_mode = RNA_enum_get(op->ptr, "evaluation_mode");
124 
125  struct USDExportParams params = {
127  export_hair,
135  };
136 
137  bool ok = USD_export(C, filename, &params, as_background_job);
138 
139  return as_background_job || ok ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
140 }
141 
142 static void wm_usd_export_draw(bContext *UNUSED(C), wmOperator *op)
143 {
144  uiLayout *layout = op->layout;
145  uiLayout *col;
146  struct PointerRNA *ptr = op->ptr;
147 
148  uiLayoutSetPropSep(layout, true);
149 
150  uiLayout *box = uiLayoutBox(layout);
151 
152  col = uiLayoutColumn(box, true);
153  uiItemR(col, ptr, "selected_objects_only", 0, NULL, ICON_NONE);
154  uiItemR(col, ptr, "visible_objects_only", 0, NULL, ICON_NONE);
155 
156  col = uiLayoutColumn(box, true);
157  uiItemR(col, ptr, "export_animation", 0, NULL, ICON_NONE);
158  uiItemR(col, ptr, "export_hair", 0, NULL, ICON_NONE);
159  uiItemR(col, ptr, "export_uvmaps", 0, NULL, ICON_NONE);
160  uiItemR(col, ptr, "export_normals", 0, NULL, ICON_NONE);
161  uiItemR(col, ptr, "export_materials", 0, NULL, ICON_NONE);
162 
163  col = uiLayoutColumn(box, true);
164  uiItemR(col, ptr, "evaluation_mode", 0, NULL, ICON_NONE);
165 
166  box = uiLayoutBox(layout);
167  uiItemL(box, IFACE_("Experimental"), ICON_NONE);
168  uiItemR(box, ptr, "use_instancing", 0, NULL, ICON_NONE);
169 }
170 
171 void WM_OT_usd_export(struct wmOperatorType *ot)
172 {
173  ot->name = "Export USD";
174  ot->description = "Export current scene in a USD archive";
175  ot->idname = "WM_OT_usd_export";
176 
177  ot->invoke = wm_usd_export_invoke;
178  ot->exec = wm_usd_export_exec;
180  ot->ui = wm_usd_export_draw;
181 
184  FILE_BLENDER,
185  FILE_SAVE,
189 
191  "selected_objects_only",
192  false,
193  "Selection Only",
194  "Only selected objects are exported. Unselected parents of selected objects are "
195  "exported as empty transform");
196 
198  "visible_objects_only",
199  true,
200  "Visible Only",
201  "Only visible objects are exported. Invisible parents of exported objects are "
202  "exported as empty transform");
203 
205  "export_animation",
206  false,
207  "Animation",
208  "When checked, the render frame range is exported. When false, only the current "
209  "frame is exported");
211  ot->srna, "export_hair", false, "Hair", "When checked, hair is exported as USD curves");
213  "export_uvmaps",
214  true,
215  "UV Maps",
216  "When checked, all UV maps of exported meshes are included in the export");
218  "export_normals",
219  true,
220  "Normals",
221  "When checked, normals of exported meshes are included in the export");
223  "export_materials",
224  true,
225  "Materials",
226  "When checked, the viewport settings of materials are exported as USD preview "
227  "materials, and material assignments are exported as geometry subsets");
228 
230  "use_instancing",
231  false,
232  "Instancing",
233  "When checked, instanced objects are exported as references in USD. "
234  "When unchecked, instanced objects are exported as real objects");
235 
237  "evaluation_mode",
238  rna_enum_usd_export_evaluation_mode_items,
240  "Use Settings for",
241  "Determines visibility of objects, modifier settings, and other areas where there "
242  "are different settings for viewport and rendering");
243 }
244 
245 #endif /* WITH_USD */
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
const char * BKE_main_blendfile_path(const struct Main *bmain) ATTR_NONNULL()
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
#define FILE_MAX
bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL()
Definition: path_util.c:1571
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
#define UNUSED(x)
#define IFACE_(msgid)
@ DAG_EVAL_RENDER
Definition: DEG_depsgraph.h:62
@ DAG_EVAL_VIEWPORT
Definition: DEG_depsgraph.h:61
@ FILE_SORT_DEFAULT
@ FILE_BLENDER
@ FILE_TYPE_FOLDER
@ FILE_TYPE_USD
@ FILE_SAVE
@ FILE_DEFAULTDISPLAY
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
Read Guarded memory(de)allocation.
#define MEM_SAFE_FREE(v)
#define C
Definition: RandGen.cpp:39
uiLayout * uiLayoutColumn(uiLayout *layout, bool align)
void uiItemL(uiLayout *layout, const char *name, int icon)
uiLayout * uiLayoutBox(uiLayout *layout)
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
#define WM_FILESEL_SHOW_PROPS
Definition: WM_api.h:540
#define WM_FILESEL_FILEPATH
Definition: WM_api.h:537
CCL_NAMESPACE_BEGIN struct Options options
uint col
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void WM_OT_usd_export(struct wmOperatorType *ot)
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
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
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
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
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3771
Definition: BKE_main.h:116
bool export_animation
Definition: usd.h:31
enum eEvaluationMode evaluation_mode
Definition: usd.h:39
bool export_materials
Definition: usd.h:35
bool selected_objects_only
Definition: usd.h:36
bool visible_objects_only
Definition: usd.h:37
bool export_normals
Definition: usd.h:34
bool export_hair
Definition: usd.h:32
bool use_instancing
Definition: usd.h:38
bool export_uvmaps
Definition: usd.h:33
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
void(* ui)(struct bContext *, struct wmOperator *)
Definition: WM_types.h:787
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
struct uiLayout * layout
struct PointerRNA * ptr
bool USD_export(bContext *C, const char *filepath, const USDExportParams *params, bool as_background_job)
Definition: usd_capi.cc:188
void WM_event_add_fileselect(bContext *C, wmOperator *op)
PointerRNA * ptr
Definition: wm_files.c:3157
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)
bool WM_operator_winactive(bContext *C)