diff options
author | makowalski <makowalski@nvidia.com> | 2021-03-07 23:08:11 +0300 |
---|---|---|
committer | makowalski <makowalski@nvidia.com> | 2021-03-07 23:10:23 +0300 |
commit | 42bf732dccbd62ba50e04ebf2695b7f6932c58c8 (patch) | |
tree | 987c5b4563d5278898bcdda1421dda6c91e267bb /source/blender | |
parent | 9732d8fd6aa392b521d0f952b2ce4b11d49f1107 (diff) |
USD Import: Convert to Z Up option.
Added a Convert to Z Up import option and logic to rotate
imported root objects 90 degrees about the X-axis if this
option is checked and the USD stage up-axis is Y.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/editors/io/io_usd.c | 13 | ||||
-rw-r--r-- | source/blender/io/usd/intern/usd_capi.cc | 25 | ||||
-rw-r--r-- | source/blender/io/usd/intern/usd_reader_xform.cc | 18 | ||||
-rw-r--r-- | source/blender/io/usd/usd.h | 1 |
4 files changed, 52 insertions, 5 deletions
diff --git a/source/blender/editors/io/io_usd.c b/source/blender/editors/io/io_usd.c index d5f379c8381..c9ec4e2016b 100644 --- a/source/blender/editors/io/io_usd.c +++ b/source/blender/editors/io/io_usd.c @@ -316,6 +316,8 @@ static int wm_usd_import_exec(bContext *C, wmOperator *op) const bool import_usd_preview = RNA_boolean_get(op->ptr, "import_usd_preview"); const bool set_material_blend = RNA_boolean_get(op->ptr, "set_material_blend"); + const bool convert_to_z_up = RNA_boolean_get(op->ptr, "convert_to_z_up"); + int offset = 0; int sequence_len = 1; @@ -360,6 +362,7 @@ static int wm_usd_import_exec(bContext *C, wmOperator *op) use_instancing, import_usd_preview, set_material_blend, + convert_to_z_up, }; bool ok = USD_import(C, filename, ¶ms, as_background_job); @@ -420,6 +423,9 @@ static void wm_usd_import_draw(bContext *UNUSED(C), wmOperator *op) row = uiLayoutRow(box, false); uiItemR(row, ptr, "create_collection", 0, NULL, ICON_NONE); + row = uiLayoutRow(box, false); + uiItemR(row, ptr, "convert_to_z_up", 0, NULL, ICON_NONE); + // row = uiLayoutRow(box, false); // uiItemR(row, ptr, "prim_path_mask", 0, NULL, ICON_NONE); @@ -615,6 +621,13 @@ void WM_OT_usd_import(struct wmOperatorType *ot) "When checked and if the Import Usd Preview option is enabled, " "the material blend method will automatically be set based on the " "shader's opacity and opacityThreshold inputs"); + + RNA_def_boolean(ot->srna, + "convert_to_z_up", + false, + "Convert to Z Up", + "When checked and if the USD stage up-axis is Y, apply a rotation " + "to the imported objects to convert their orientation to Z up "); } #endif /* WITH_USD */ diff --git a/source/blender/io/usd/intern/usd_capi.cc b/source/blender/io/usd/intern/usd_capi.cc index 7f8775c570b..702104bb2ea 100644 --- a/source/blender/io/usd/intern/usd_capi.cc +++ b/source/blender/io/usd/intern/usd_capi.cc @@ -65,6 +65,7 @@ #include "BLI_fileops.h" #include "BLI_listbase.h" #include "BLI_math_matrix.h" +#include "BLI_math_rotation.h" #include "BLI_path_util.h" #include "BLI_string.h" @@ -238,6 +239,26 @@ static void create_proto_collections(Main *bmain, } } +// Update the given import settings with the global rotation matrix to orient +// imported objects with Z-up, if necessary +static void set_global_rotation(pxr::UsdStageRefPtr stage, ImportSettings &r_settings) +{ + if (!stage || pxr::UsdGeomGetStageUpAxis(stage) == pxr::UsdGeomTokens->z) { + // Nothing to do. + return; + } + + r_settings.do_convert_mat = true; + + // Rotate 90 degrees about the X-axis. + float rmat[3][3]; + float axis[3] = { 1.0f, 0.0f, 0.0f }; + axis_angle_normalized_to_mat3(rmat, axis, M_PI/2.0f); + + unit_m4(r_settings.conversion_mat); + copy_m4_m3(r_settings.conversion_mat, rmat); +} + /* ********************** Export file ********************** */ struct ExportJobData { @@ -550,6 +571,10 @@ static void import_startjob(void *customdata, short *stop, short *do_update, flo return; } + if (data->params.convert_to_z_up) { + set_global_rotation(archive->stage(), data->settings); + } + // Set up the stage for animated data. if (data->params.set_frame_range) { // archive->stage()->SetTimeCodesPerSecond(FPS); diff --git a/source/blender/io/usd/intern/usd_reader_xform.cc b/source/blender/io/usd/intern/usd_reader_xform.cc index 9a7f5c73503..d167cf1a052 100644 --- a/source/blender/io/usd/intern/usd_reader_xform.cc +++ b/source/blender/io/usd/intern/usd_reader_xform.cc @@ -122,11 +122,19 @@ void USDXformReader::read_matrix(float r_mat[4][4] /* local matrix */, mul_m4_m4m4(r_mat, r_mat, t_mat); } - /* Apply scaling only to root objects, parenting will propagate it. */ - if (scale != 1.0 && is_root_xform_object()) { - float scale_mat[4][4]; - scale_m4_fl(scale_mat, scale); - mul_m4_m4m4(r_mat, scale_mat, r_mat); + /* Apply global scaling and rotation only to root objects, parenting + * will propagate it. */ + if ((scale != 1.0 || m_settings->do_convert_mat) && is_root_xform_object()) { + + if (scale != 1.0f) { + float scale_mat[4][4]; + scale_m4_fl(scale_mat, scale); + mul_m4_m4m4(r_mat, scale_mat, r_mat); + } + + if (m_settings->do_convert_mat) { + mul_m4_m4m4(r_mat, m_settings->conversion_mat, r_mat); + } } } diff --git a/source/blender/io/usd/usd.h b/source/blender/io/usd/usd.h index 533749c6a8e..3e41f9653cd 100644 --- a/source/blender/io/usd/usd.h +++ b/source/blender/io/usd/usd.h @@ -70,6 +70,7 @@ struct USDImportParams { bool use_instancing; bool import_usd_preview; bool set_material_blend; + bool convert_to_z_up; }; /* The USD_export takes a as_background_job parameter, and returns a boolean. |