[Bf-blender-cvs] [2627517f39d] usd-importer-T81257: USD light import support.

Michael A. Kowalski noreply at git.blender.org
Sun Nov 15 22:09:56 CET 2020


Commit: 2627517f39d9c950eaed6c6a40bbfc1ede8441e2
Author: Michael A. Kowalski
Date:   Sun Nov 15 16:07:25 2020 -0500
Branches: usd-importer-T81257
https://developer.blender.org/rB2627517f39d9c950eaed6c6a40bbfc1ede8441e2

USD light import support.

Added USDLightReader class and the logic
to instantiate it when iterating over
the USD prims.

===================================================================

M	source/blender/editors/io/io_usd.c
M	source/blender/io/usd/CMakeLists.txt
M	source/blender/io/usd/import/usd_prim_iterator.cc
A	source/blender/io/usd/import/usd_reader_light.cc
A	source/blender/io/usd/import/usd_reader_light.h
M	source/blender/io/usd/usd.h

===================================================================

diff --git a/source/blender/editors/io/io_usd.c b/source/blender/editors/io/io_usd.c
index a665b5e1aa1..ab40321e318 100644
--- a/source/blender/editors/io/io_usd.c
+++ b/source/blender/editors/io/io_usd.c
@@ -279,14 +279,21 @@ static int wm_usd_import_exec(bContext *C, wmOperator *op)
 
   const bool use_instancing = RNA_boolean_get(op->ptr, "use_instancing");
 
+  const float light_intensity_scale = RNA_float_get(op->ptr, "light_intensity_scale");
+
   /* Switch out of edit mode to avoid being stuck in it (T54326). */
   Object *obedit = CTX_data_edit_object(C);
   if (obedit) {
     ED_object_mode_set(C, OB_MODE_OBJECT);
   }
 
-  struct USDImportParams params = {
-      import_uvs, import_normals, import_materials, scale, debug, use_instancing};
+  struct USDImportParams params = {import_uvs,
+                                   import_normals,
+                                   import_materials,
+                                   scale,
+                                   debug,
+                                   use_instancing,
+                                   light_intensity_scale};
 
   bool ok = USD_import(C, filename, &params, as_background_job);
 
@@ -309,6 +316,7 @@ static void wm_usd_import_draw(bContext *UNUSED(C), wmOperator *op)
   uiItemR(col, ptr, "import_uvs", 0, NULL, ICON_NONE);
   uiItemR(col, ptr, "import_normals", 0, NULL, ICON_NONE);
   uiItemR(col, ptr, "import_materials", 0, NULL, ICON_NONE);
+  uiItemR(box, ptr, "light_intensity_scale", 0, NULL, ICON_NONE);
   uiItemR(col, ptr, "debug", 0, NULL, ICON_NONE);
 
   box = uiLayoutBox(layout);
@@ -365,6 +373,16 @@ void WM_OT_usd_import(wmOperatorType *ot)
       "Instancing",
       "When checked, instanced USD references are imported as shared data in Blender. "
       "When unchecked, instanced USD reference are imported as unique data in Blender.");
+
+  RNA_def_float(ot->srna,
+                "light_intensity_scale",
+                1.0f,
+                0.0001f,
+                10000.0f,
+                "Light Intensity Scale",
+                "Value by which to scale the intensity of imported lights",
+                0.0001f,
+                1000.0f);
 }
 
 #endif /* WITH_USD */
diff --git a/source/blender/io/usd/CMakeLists.txt b/source/blender/io/usd/CMakeLists.txt
index c965a613bd0..f9990a972b9 100644
--- a/source/blender/io/usd/CMakeLists.txt
+++ b/source/blender/io/usd/CMakeLists.txt
@@ -57,6 +57,7 @@ set(SRC
   import/usd_import_util.cc
   import/usd_prim_iterator.cc
   import/usd_reader_camera.cc
+  import/usd_reader_light.cc
   import/usd_reader_mesh.cc
   import/usd_reader_mesh_base.cc
   import/usd_reader_prim.cc
@@ -77,6 +78,7 @@ set(SRC
   import/usd_import_util.h
   import/usd_prim_iterator.h
   import/usd_reader_camera.h
+  import/usd_reader_light.h
   import/usd_reader_mesh.h
   import/usd_reader_mesh_base.h
   import/usd_reader_prim.h
diff --git a/source/blender/io/usd/import/usd_prim_iterator.cc b/source/blender/io/usd/import/usd_prim_iterator.cc
index 08daa651465..7f6f311e90a 100644
--- a/source/blender/io/usd/import/usd_prim_iterator.cc
+++ b/source/blender/io/usd/import/usd_prim_iterator.cc
@@ -22,6 +22,7 @@
 #include "usd.h"
 #include "usd_importer_context.h"
 #include "usd_reader_camera.h"
+#include "usd_reader_light.h"
 #include "usd_reader_mesh.h"
 #include "usd_reader_xform.h"
 #include "usd_reader_xformable.h"
@@ -36,6 +37,7 @@
 #include <pxr/usd/usdGeom/scope.h>
 #include <pxr/usd/usdGeom/tokens.h>
 #include <pxr/usd/usdGeom/xformable.h>
+#include <pxr/usd/usdLux/light.h>
 
 namespace blender::io::usd {
 
@@ -100,6 +102,9 @@ USDXformableReader *USDPrimIterator::get_object_reader(const pxr::UsdPrim &prim,
   else if (prim.IsA<pxr::UsdGeomXform>()) {
     result = new USDXformReader(prim, context);
   }
+  else if (prim.IsA<pxr::UsdLuxLight>()) {
+    result = new USDLightReader(prim, context);
+  }
 
   return result;
 }
diff --git a/source/blender/io/usd/import/usd_reader_light.cc b/source/blender/io/usd/import/usd_reader_light.cc
new file mode 100644
index 00000000000..78c427691e8
--- /dev/null
+++ b/source/blender/io/usd/import/usd_reader_light.cc
@@ -0,0 +1,182 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * The Original Code is Copyright (C) 2020 Blender Foundation.
+ * All rights reserved.
+ */
+
+#include "usd_reader_light.h"
+
+#include "BLI_assert.h"
+#include "BLI_utildefines.h"
+#include "BLI_math_base.h"
+#include "BLI_math_matrix.h"
+#include "BLI_math_rotation.h"
+
+#include "BKE_light.h"
+#include "BKE_object.h"
+
+#include "DNA_light_types.h"
+#include "DNA_object_types.h"
+
+#include <pxr/usd/usdLux/diskLight.h>
+#include <pxr/usd/usdLux/distantLight.h>
+#include <pxr/usd/usdLux/rectLight.h>
+#include <pxr/usd/usdLux/sphereLight.h>
+
+#include <iostream>
+
+namespace blender::io::usd {
+
+USDLightReader::USDLightReader(const pxr::UsdPrim &prim, const USDImporterContext &context)
+    : USDXformableReader(prim, context), light_(prim)
+{
+}
+
+bool USDLightReader::valid() const
+{
+  return static_cast<bool>(light_);
+}
+
+void USDLightReader::create_object(Main *bmain, double time)
+{
+   if (!this->valid()) {
+     return;
+   }
+
+   /* Determine prim visibility.
+    * TODO(makowalski): Consider optimizations to avoid this expensive call,
+    * for example, by determining visibility during stage traversal. */
+   pxr::TfToken vis_tok = this->light_.ComputeVisibility();
+
+   if (vis_tok == pxr::UsdGeomTokens->invisible) {
+     return;
+   }
+
+   std::string obj_name = get_object_name();
+
+   if (obj_name.empty()) {
+     /* Sanity check. */
+     std::cerr << "Warning: couldn't determine object name for " << this->prim_path() << std::endl;
+   }
+
+   this->object_ = BKE_object_add_only_object(bmain, OB_LAMP, obj_name.c_str());
+
+   std::string light_name = get_data_name();
+
+   if (light_name.empty()) {
+     /* Sanity check. */
+     std::cerr << "Warning: couldn't determine light name for " << this->prim_path() << std::endl;
+   }
+
+   Light *light = (Light *)BKE_light_add(bmain, light_name.c_str());
+
+   this->object_->data = light;
+
+   if (prim_.IsA<pxr::UsdLuxDiskLight>()) {
+     light->type = LA_AREA;
+     light->area_shape = LA_AREA_DISK;
+
+     pxr::UsdLuxDiskLight disk_light(this->prim_);
+     if (disk_light) {
+       if (pxr::UsdAttribute radius_attr = disk_light.GetRadiusAttr())
+       {
+         float radius = 1.0;
+         radius_attr.Get(&radius, time);
+         light->area_size = radius;
+       }
+     }
+   }
+   else if (prim_.IsA<pxr::UsdLuxDistantLight>()) {
+     light->type = LA_SUN;
+   }
+   else if (prim_.IsA<pxr::UsdLuxRectLight>()) {
+     light->type = LA_AREA;
+     light->area_shape = LA_AREA_RECT;
+
+     pxr::UsdLuxRectLight rect_light(this->prim_);
+     if (rect_light) {
+       if (pxr::UsdAttribute width_attr = rect_light.GetWidthAttr())
+       {
+         float width = 1.0;
+         width_attr.Get(&width, time);
+         light->area_size = width;
+       }
+       if (pxr::UsdAttribute height_attr = rect_light.GetHeightAttr())
+       {
+         float height = 1.0;
+         height_attr.Get(&height, time);
+         light->area_sizey = height;
+       }
+     }
+   }
+   else if (prim_.IsA<pxr::UsdLuxSphereLight>()) {
+     light->type = LA_LOCAL;
+
+     pxr::UsdLuxSphereLight sphere_light(this->prim_);
+     if (sphere_light) {
+       if (pxr::UsdAttribute radius_attr = sphere_light.GetRadiusAttr())
+       {
+         float radius = 1.0;
+         radius_attr.Get(&radius, time);
+         light->area_size = radius;
+       }
+     }
+   }
+
+   if (pxr::UsdAttribute intensity_attr = light_.GetIntensityAttr()) {
+     float intensity = 1.0;
+     intensity_attr.Get(&intensity, time);
+     light->energy = intensity * context_.import_params.light_intensity_scale;
+
+     /* Apply inverse of scaling applied on export in USDLightWriter::do_write().
+      * TODO(makowalski): confirm that this scaling is correct. */
+     if (light->type != LA_SUN) {
+       light->energy *= 100.0f;
+     }
+   }
+
+   if (pxr::UsdAttribute color_attr = light_.GetColorAttr()) {
+     pxr::GfVec3f color(1.0);
+     color_attr.Get(&color, time);
+     light->r = color[0];
+     light->g = color[1];
+     light->b = color[2];
+   }
+
+   if (pxr::UsdAttribute specular_attr = light_.GetSpecularAttr()) {
+     float specular = 1.0f;
+     specular_attr.Get(&specular, time);
+     light->spec_fac = specular;
+   }
+
+}
+
+void USDLightReader::read_matrix(float r_mat[4][4] /* local matrix */,
+                                 const double time,
+                                 const float scale) const
+{
+  USDXformableReader::read_matrix(r_mat, time, scale);
+
+  /* Conveting from y-up to z-up requires adjusting
+   * the light rotation. */
+  if (this->context_.stage_up_axis == pxr::UsdGeomTokens->y) {
+    float camera_rotation[4][4];
+    axis_angle_to_mat4_single(camera_rotation, 'X', M_PI_2);
+    mul_m4_m4m4(r_mat, r_mat, camera_rotation);
+  }
+}
+
+}  // namespace blender::io::usd
diff --git a/source/blender/io/usd/import/usd_reader_light.h b/source/blender/io/usd/import/usd_reader_light.h
new file mode 100644
index 00000000000..318e0e50af7
--- /dev/null
+++ b/source/blender/io/usd/import/usd_reader_light.h
@@ -0,0 +1,43 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list