49 const ufbx_scene &
fbx;
61 ufbx_transform root_tr;
62 root_tr.translation = ufbx_zero_vec3;
63 root_tr.rotation = this->fbx.metadata.root_rotation;
64 root_tr.scale.x = root_tr.scale.y = root_tr.scale.z = this->fbx.metadata.root_scale;
69 debug_file_path = debug_file_path.substr(0, debug_file_path.size() - 4) +
"-dbg-b.txt";
70 g_debug_file =
BLI_fopen(debug_file_path.c_str(),
"wb");
98 double fps = this->
fbx.settings.frames_per_second;
105 for (
const ufbx_material *fmat : this->
fbx.materials) {
112 if (mat ==
nullptr) {
133 if (
node->node_depth == 1 &&
node->children.count == 0 &&
134 STREQ(
"MotionBuilder",
fbx.metadata.original_application.name.data))
137 if (
STREQ(
"Producer Perspective",
node->name.data) ||
138 STREQ(
"Producer Front",
node->name.data) ||
STREQ(
"Producer Back",
node->name.data) ||
139 STREQ(
"Producer Right",
node->name.data) ||
STREQ(
"Producer Left",
node->name.data) ||
151 for (
const ufbx_camera *fcam : this->
fbx.cameras) {
152 if (fcam->instances.count == 0) {
158 const ufbx_node *
node = fcam->instances[0];
165 bcam->type = fcam->projection_mode == UFBX_PROJECTION_MODE_ORTHOGRAPHIC ?
CAM_ORTHO :
167 bcam->dof.focus_distance = ufbx_find_real(&fcam->props,
"FocusDistance", 10.0f) *
168 this->
fbx.metadata.geometry_scale * this->
fbx.metadata.root_scale;
169 if (ufbx_find_bool(&fcam->props,
"UseDepthOfField",
false)) {
172 bcam->lens = fcam->focal_length_mm;
173 constexpr double m_to_in = 0.0393700787;
174 bcam->sensor_x = fcam->film_size_inch.x / m_to_in;
175 bcam->sensor_y = fcam->film_size_inch.y / m_to_in;
179 bcam->ortho_scale = ufbx_find_real(&fcam->props,
"OrthoZoom", 1.0);
181 bcam->shiftx = ufbx_find_real(&fcam->props,
"FilmOffsetX", 0.0) / (m_to_in * bcam->sensor_x);
182 bcam->shifty = ufbx_find_real(&fcam->props,
"FilmOffsetY", 0.0) / (m_to_in * bcam->sensor_x);
183 bcam->clip_start = fcam->near_plane * this->
fbx.metadata.root_scale;
184 bcam->clip_end = fcam->far_plane * this->
fbx.metadata.root_scale;
187 obj->data = id_cast<ID *>(bcam);
188 if (!
node->visible) {
202 for (
const ufbx_light *flight : this->
fbx.lights) {
203 if (flight->instances.count == 0) {
206 const ufbx_node *
node = flight->instances[0];
212 switch (flight->type) {
213 case UFBX_LIGHT_POINT:
216 case UFBX_LIGHT_DIRECTIONAL:
219 case UFBX_LIGHT_SPOT:
221 lamp->spotsize =
DEG2RAD(flight->outer_angle);
222 lamp->spotblend = 1.0f - flight->inner_angle / flight->outer_angle;
228 lamp->r = flight->
color.
x;
229 lamp->g = flight->
color.
y;
230 lamp->b = flight->
color.
z;
231 lamp->energy = flight->intensity;
232 lamp->exposure = ufbx_find_real(&flight->props,
"Exposure", 0.0);
236 obj->data = id_cast<ID *>(lamp);
237 if (!
node->visible) {
258 for (
const ufbx_node *
node : this->
fbx.nodes) {
260 if (
node->is_root || this->mapping.node_is_blender_bone.contains(
node) ||
261 this->mapping.el_to_object.contains(&
node->element))
267 if (
ELEM(
node->attrib_type, UFBX_ELEMENT_CAMERA, UFBX_ELEMENT_CAMERA_SWITCHER) &&
268 node->node_depth == 1 &&
node->children.count == 0)
274 if (!
node->visible) {
297 if (item.value->parent !=
nullptr) {
300 const ufbx_node *
node = ufbx_as_node(item.key);
301 if (
node ==
nullptr) {
306 if (!
ELEM(obj_par,
nullptr, item.value)) {
307 item.value->parent = obj_par;
314 ufbx_thread_pool_context ctx,
320 ufbx_thread_pool_run_task(ctx,
index);
325 ufbx_thread_pool_context ,
342 ufbx_load_opts opts = {};
343 opts.filename.data = params.
filepath;
344 opts.filename.length = strlen(params.
filepath);
345 opts.evaluate_skinning =
false;
346 opts.evaluate_caches =
false;
347 opts.load_external_files =
false;
348 opts.clean_skin_weights =
true;
349 opts.use_blender_pbr_material =
true;
351 opts.geometry_transform_handling = UFBX_GEOMETRY_TRANSFORM_HANDLING_MODIFY_GEOMETRY;
352 opts.pivot_handling = UFBX_PIVOT_HANDLING_ADJUST_TO_ROTATION_PIVOT;
354 opts.space_conversion = UFBX_SPACE_CONVERSION_ADJUST_TRANSFORMS;
355 opts.target_axes.right = UFBX_COORDINATE_AXIS_POSITIVE_X;
356 opts.target_axes.up = UFBX_COORDINATE_AXIS_POSITIVE_Z;
357 opts.target_axes.front = UFBX_COORDINATE_AXIS_NEGATIVE_Y;
360 opts.target_camera_axes.right = UFBX_COORDINATE_AXIS_POSITIVE_X;
361 opts.target_camera_axes.up = UFBX_COORDINATE_AXIS_POSITIVE_Y;
362 opts.target_camera_axes.front = UFBX_COORDINATE_AXIS_POSITIVE_Z;
363 opts.target_light_axes.right = UFBX_COORDINATE_AXIS_POSITIVE_X;
364 opts.target_light_axes.up = UFBX_COORDINATE_AXIS_POSITIVE_Y;
365 opts.target_light_axes.front = UFBX_COORDINATE_AXIS_POSITIVE_Z;
371 ufbx_error fbx_error;
372 ufbx_scene *
fbx = ufbx_load_stdio(file, &opts, &fbx_error);
377 "Failed to import FBX file '%s': '%s'\n",
379 fbx_error.description.data);
382 "FBX Import: Cannot import file '%s': '%s'",
384 fbx_error.description.data);
392 "Could not find an editable collection in current scene, imported data will not be "
400#ifdef FBX_DEBUG_PRINT
402 fprintf(g_debug_file,
"Initial NODE local matrices:\n");
404 for (
const ufbx_node *
node : ctx.
fbx.nodes) {
410 std::ranges::sort(
nodes, [](
const ufbx_node *a,
const ufbx_node *
b) {
411 int ncmp = strcmp(a->name.data,
b->name.data);
415 return a->attrib_type >
b->attrib_type;
418 ufbx_matrix mtx = ufbx_matrix_mul(
node->node_depth < 2 ? &
node->node_to_world :
419 &
node->node_to_parent,
420 &
node->geometry_to_node);
421 fprintf(g_debug_file,
"init NODE %s self.matrix:\n",
node->name.data);
424 fprintf(g_debug_file,
"\n");
437 ufbx_free_scene(
fbx);
447 bool has_instantiated_object =
false;
448 bool has_uninstantiated_object =
false;
453 has_uninstantiated_object =
true;
456 has_instantiated_object =
true;
464 if (has_instantiated_object && has_uninstantiated_object) {
465 CLOG_ERROR(&
LOG,
"Some imported objects were not instantiated, while others were");
Camera data-block and utility functions.
General operations, lookup, etc. for blender lights.
General operations, lookup, etc. for blender objects.
File and directory operations.
#define CLOG_ERROR(clg_ref,...)
#define ID_IS_EDITABLE(_id)
Object groups, one object can be in many groups at once.
constexpr IndexRange()=default
void camera(KernelGlobals &kg, const ShadingData &sd, float3 &outview, float &outdepth, float &outdist)
static void fbx_task_run_fn(void *, ufbx_thread_pool_context ctx, uint32_t, uint32_t start_index, uint32_t count)
const char * get_fbx_name(const ufbx_string &name, const char *def)
static bool should_import_camera(const ufbx_scene &fbx, const ufbx_camera *camera)
void read_custom_properties(const ufbx_props &props, ID &id, bool enums_as_strings)
void node_matrix_to_obj(const ufbx_node *node, Object *obj, const FbxElementMapping &mapping)
Material * import_material(Main *bmain, const std::string &base_dir, const ufbx_material &fmat)
void importer_main(Main *bmain, Scene *scene, ViewLayer *view_layer, const FBXImportParams ¶ms)
void import_animations(Main &bmain, const ufbx_scene &fbx, const FbxElementMapping &mapping, const double fps, const float anim_offset)
static void fbx_task_wait_fn(void *, ufbx_thread_pool_context, uint32_t, uint32_t)
void import_armatures(Main &bmain, const ufbx_scene &fbx, FbxElementMapping &mapping, const FBXImportParams ¶ms)
void import_meshes(Main &bmain, const ufbx_scene &fbx, FbxElementMapping &mapping, const FBXImportParams ¶ms)
void parallel_for_each(Range &&range, const Function &function)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
LayerCollection * BKE_layer_collection_get_active_editable(ViewLayer *view_layer)
void BKE_view_layer_base_select_and_set_active(ViewLayer *view_layer, Base *selbase)
ID * BKE_libblock_find_name(Main *bmain, short type, const char *name, const std::optional< Library * > lib=std::nullopt) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
struct Camera * BKE_camera_add(struct Main *bmain, const char *name)
bool BKE_view_layer_synced_ensure(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
void BKE_view_layer_base_deselect_all(const Main &bmain, const Scene *scene, ViewLayer *view_layer)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Light * BKE_light_add(Main *bmain, const char *name) ATTR_WARN_UNUSED_RESULT
Base * BKE_view_layer_base_find(ViewLayer *view_layer, Object *ob)
void DEG_relations_tag_update(Main *bmain)
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
eFBXMtlNameCollisionMode mtl_name_collision_mode
struct Collection * collection
Map< const ufbx_material *, Material * > mat_to_material
Map< const ufbx_element *, Object * > el_to_object
ufbx_matrix global_conv_matrix
Set< Object * > imported_objects
FbxImportContext(Main *main, const ufbx_scene *fbx, const FBXImportParams ¶ms)
FbxElementMapping mapping
void import_globals(Scene *scene) const
void import_animation(double fps)
const FBXImportParams & params