Blender V5.3
io_fbx_ops.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#ifdef WITH_IO_FBX
10
11# include "BKE_context.hh"
12# include "BKE_file_handler.hh"
13# include "BKE_report.hh"
14
15# include "BLI_string.hh"
16# include "BLI_string_utf8.hh"
17
18# include "WM_api.hh"
19
20# include "DNA_space_types.h"
21
22# include "ED_outliner.hh"
23
24# include "RNA_access.hh"
25# include "RNA_define.hh"
26
27# include "BLT_translation.hh"
28
29# include "UI_interface.hh"
30# include "UI_interface_layout.hh"
31
32# include "IO_fbx.hh"
33# include "io_fbx_ops.hh"
34# include "io_utils.hh"
35
36namespace blender {
37
38const EnumPropertyItem rna_enum_fbx_mtl_name_collision_mode_items[] = {
40 "MAKE_UNIQUE",
41 0,
42 "Make Unique",
43 "Import each FBX material as a unique Blender material"},
45 "REFERENCE_EXISTING",
46 0,
47 "Reference Existing",
48 "If a material with the same name already exists, reference that instead of importing"},
49 {0, nullptr, 0, nullptr, nullptr},
50};
51
52static const EnumPropertyItem fbx_vertex_colors_mode[] = {
53 {int(eFBXVertexColorMode::None), "NONE", 0, "None", "Do not import color attributes"},
55 "SRGB",
56 0,
57 "sRGB",
58 "Vertex colors in the file are in sRGB color space"},
60 "LINEAR",
61 0,
62 "Linear",
63 "Vertex colors in the file are in linear color space"},
64 {0, nullptr, 0, nullptr, nullptr}};
65
66static wmOperatorStatus wm_fbx_import_exec(bContext *C, wmOperator *op)
67{
68 FBXImportParams params;
69 params.global_scale = RNA_float_get(op->ptr, "global_scale");
70 params.use_custom_normals = RNA_boolean_get(op->ptr, "use_custom_normals");
71 params.use_custom_props = RNA_boolean_get(op->ptr, "use_custom_props");
72 params.props_enum_as_string = RNA_boolean_get(op->ptr, "use_custom_props_enum_as_string");
73 params.ignore_leaf_bones = RNA_boolean_get(op->ptr, "ignore_leaf_bones");
74 params.import_subdivision = RNA_boolean_get(op->ptr, "import_subdivision");
75 params.validate_meshes = RNA_boolean_get(op->ptr, "validate_meshes");
76 params.use_anim = RNA_boolean_get(op->ptr, "use_anim");
77 params.anim_offset = RNA_float_get(op->ptr, "anim_offset");
78 params.vertex_colors = eFBXVertexColorMode(RNA_enum_get(op->ptr, "import_colors"));
79 params.mtl_name_collision_mode = eFBXMtlNameCollisionMode(
80 RNA_enum_get(op->ptr, "mtl_name_collision_mode"));
81
82 params.reports = op->reports;
83
84 const auto paths = ed::io::paths_from_operator_properties(op->ptr);
85
86 if (paths.is_empty()) {
87 BKE_report(op->reports, RPT_ERROR, "No filepath given");
88 return OPERATOR_CANCELLED;
89 }
90 for (const auto &path : paths) {
91 STRNCPY(params.filepath, path.c_str());
92 FBX_import(C, params);
93 }
94
95 Scene *scene = CTX_data_scene(C);
100
101 return OPERATOR_FINISHED;
102}
103
104static bool wm_fbx_import_check(bContext * /*C*/, wmOperator * /*op*/)
105{
106 return false;
107}
108
109static void ui_fbx_import_settings(const bContext *C, ui::Layout &layout, PointerRNA *ptr)
110{
111 layout.use_property_split_set(true);
112 layout.use_property_decorate_set(false);
113
114 if (ui::Layout *panel = layout.panel(C, "FBX_import_general", false, IFACE_("General"))) {
115 ui::Layout &col = panel->column(false);
116 col.prop(ptr, "global_scale", UI_ITEM_NONE, std::nullopt, ICON_NONE);
117 col.prop(ptr, "use_custom_props", UI_ITEM_NONE, std::nullopt, ICON_NONE);
118 ui::Layout &subcol = col.column(false);
119 subcol.active_set(RNA_boolean_get(ptr, "use_custom_props"));
120 subcol.prop(ptr, "use_custom_props_enum_as_string", UI_ITEM_NONE, std::nullopt, ICON_NONE);
121 }
122
123 if (ui::Layout *panel = layout.panel(C, "FBX_import_geometry", false, IFACE_("Geometry"))) {
124 ui::Layout &col = panel->column(false);
125 col.prop(ptr, "use_custom_normals", UI_ITEM_NONE, std::nullopt, ICON_NONE);
126 col.prop(ptr, "import_subdivision", UI_ITEM_NONE, std::nullopt, ICON_NONE);
127 col.prop(ptr, "import_colors", UI_ITEM_NONE, std::nullopt, ICON_NONE);
128 col.prop(ptr, "validate_meshes", UI_ITEM_NONE, std::nullopt, ICON_NONE);
129 }
130
131 if (ui::Layout *panel = layout.panel(C, "FBX_import_material", true, IFACE_("Materials"))) {
132 ui::Layout &col = panel->column(false);
133 col.prop(ptr, "mtl_name_collision_mode", UI_ITEM_NONE, std::nullopt, ICON_NONE);
134 }
135
136 {
137 ui::PanelLayout panel = layout.panel(C, "FBX_import_anim", true);
138 panel.header->use_property_split_set(false);
139 panel.header->prop(ptr, "use_anim", UI_ITEM_NONE, "", ICON_NONE);
140 panel.header->label(IFACE_("Animation"), ICON_NONE);
141 if (panel.body) {
142 ui::Layout &col = panel.body->column(false);
143 col.prop(ptr, "anim_offset", UI_ITEM_NONE, std::nullopt, ICON_NONE);
144 }
145 }
146
147 if (ui::Layout *panel = layout.panel(C, "FBX_import_armature", false, IFACE_("Armature"))) {
148 ui::Layout &col = panel->column(false);
149 col.prop(ptr, "ignore_leaf_bones", UI_ITEM_NONE, std::nullopt, ICON_NONE);
150 }
151}
152
153static void wm_fbx_import_draw(bContext *C, wmOperator *op)
154{
155 ui_fbx_import_settings(C, *op->layout, op->ptr);
156}
157
159{
160 PropertyRNA *prop;
161
162 ot->name = "Import FBX";
163 ot->description = "Import FBX file into current scene";
164 ot->idname = "WM_OT_fbx_import";
165
167 ot->exec = wm_fbx_import_exec;
168 ot->poll = WM_operator_winactive;
169 ot->check = wm_fbx_import_check;
170 ot->ui = wm_fbx_import_draw;
171 ot->flag = OPTYPE_UNDO | OPTYPE_PRESET;
172
181
182 RNA_def_float(ot->srna, "global_scale", 1.0f, 1e-6f, 1e6f, "Scale", "", 0.001f, 1000.0f);
183
185 ot->srna,
186 "mtl_name_collision_mode",
187 rna_enum_fbx_mtl_name_collision_mode_items,
189 "Material Name Collision",
190 "Behavior when the name of an imported material conflicts with an existing material");
191 RNA_def_enum(ot->srna,
192 "import_colors",
193 fbx_vertex_colors_mode,
195 "Vertex Colors",
196 "Import vertex color attributes");
197
198 RNA_def_boolean(ot->srna,
199 "use_custom_normals",
200 true,
201 "Custom Normals",
202 "Import custom normals, if available (otherwise Blender will compute them)");
203 RNA_def_boolean(ot->srna,
204 "use_custom_props",
205 true,
206 "Custom Properties",
207 "Import user properties as custom properties");
208 RNA_def_boolean(ot->srna,
209 "use_custom_props_enum_as_string",
210 true,
211 "Enums As Strings",
212 "Store custom property enumeration values as strings");
213 RNA_def_boolean(ot->srna,
214 "import_subdivision",
215 false,
216 "Subdivision Data",
217 "Import FBX subdivision information as subdivision surface modifiers");
218 RNA_def_boolean(ot->srna,
219 "ignore_leaf_bones",
220 false,
221 "Ignore Leaf Bones",
222 "Ignore the last bone at the end of each chain (used to mark the length of the "
223 "previous bone)");
225 ot->srna,
226 "validate_meshes",
227 true,
228 "Validate Meshes",
229 "Ensure the data is valid "
230 "(when disabled, data may be imported which causes crashes displaying or editing)");
231
232 RNA_def_boolean(ot->srna, "use_anim", true, "Import Animation", "Import FBX animation");
233 prop = RNA_def_float(ot->srna,
234 "anim_offset",
235 1.0f,
236 -1e6f,
237 1e6f,
238 "Offset",
239 "Offset to apply to animation timestamps, in frames",
240 -1e4f,
241 1e4f);
242 RNA_def_property_ui_range(prop, -1e4f, 1e4f, 100, 1);
243
244 /* Only show `.fbx` files by default. */
245 prop = RNA_def_string(ot->srna, "filter_glob", "*.fbx", 0, "Extension Filter", "");
247}
248
249namespace ed::io {
251{
252 auto fh = std::make_unique<bke::FileHandlerType>();
253 STRNCPY_UTF8(fh->idname, "IO_FH_fbx");
254 STRNCPY_UTF8(fh->import_operator, "WM_OT_fbx_import");
255 STRNCPY_UTF8(fh->export_operator, "export_scene.fbx"); /* Use Python add-on for export. */
256 STRNCPY_UTF8(fh->label, "FBX");
257 STRNCPY_UTF8(fh->file_extensions_str, ".fbx");
258 fh->poll_drop = poll_file_object_drop;
259 bke::file_handler_add(std::move(fh));
260}
261
262} // namespace ed::io
263} // namespace blender
264
265#endif /* WITH_IO_FBX */
#define STRNCPY_UTF8(dst, src)
#define IFACE_(msgid)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define ND_OB_ACTIVE
Definition WM_types.hh:459
#define ND_OB_SELECT
Definition WM_types.hh:461
#define NC_SCENE
Definition WM_types.hh:391
#define ND_LAYER_CONTENT
Definition WM_types.hh:472
void file_handler_add(std::unique_ptr< FileHandlerType > file_handler)
bool poll_file_object_drop(const bContext *C, bke::FileHandlerType *)
Definition io_utils.cc:62
void fbx_file_handler_add()
Vector< std::string > paths_from_operator_properties(PointerRNA *ptr)
Definition io_utils.cc:79
wmOperatorStatus filesel_drop_import_invoke(bContext *C, wmOperator *op, const wmEvent *)
Definition io_utils.cc:30
BMesh Iterators.
char * STRNCPY(char(&dst)[N], const char *src)
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)
void WM_OT_fbx_import(wmOperatorType *ot)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
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)
void ED_outliner_select_sync_from_object_tag(bContext *C)
void RNA_def_property_ui_range(PropertyRNA *prop, double min, double max, double step, int precision)
eFBXVertexColorMode
Definition IO_fbx.hh:32
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
@ WM_FILESEL_DIRECTORY
Definition WM_api.hh:1330
@ WM_FILESEL_SHOW_PROPS
Definition WM_api.hh:1335
@ WM_FILESEL_FILES
Definition WM_api.hh:1333
@ WM_FILESEL_FILEPATH
Definition WM_api.hh:1332
void FBX_import(bContext *C, const FBXImportParams &params)
Definition IO_fbx.cc:31
bool RNA_boolean_get(PointerRNA *ptr, const char *name)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
@ FILE_OPENFILE
Definition WM_api.hh:1341
bool WM_operator_winactive(bContext *C)
@ PROP_HIDDEN
Definition RNA_types.hh:408
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_UNDO
Definition WM_types.hh:190
@ OPTYPE_PRESET
Definition WM_types.hh:203
PropertyRNA * RNA_def_boolean(StructOrFunctionRNA *cont_, const char *identifier, const bool default_value, 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)
float RNA_float_get(PointerRNA *ptr, const char *name)
eFBXMtlNameCollisionMode
Definition IO_fbx.hh:27
void use_property_split_set(bool is_sep)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & column(bool align)