[Bf-blender-cvs] [1c42e28727f] usd-importer-T81257-merge: USD import: validate light attributes

makowalski noreply at git.blender.org
Tue Jul 27 19:59:29 CEST 2021


Commit: 1c42e28727fb2ce99561c3663c9a22e63a771ec4
Author: makowalski
Date:   Tue Jul 27 12:40:08 2021 -0400
Branches: usd-importer-T81257-merge
https://developer.blender.org/rB1c42e28727fb2ce99561c3663c9a22e63a771ec4

USD import: validate light attributes

Added checks to validate light attributes
and values, to help avoid errors when reading
light settings which might not have been
authored.

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

M	source/blender/io/usd/intern/usd_reader_light.cc

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

diff --git a/source/blender/io/usd/intern/usd_reader_light.cc b/source/blender/io/usd/intern/usd_reader_light.cc
index 1812a1c229c..f62576fcc81 100644
--- a/source/blender/io/usd/intern/usd_reader_light.cc
+++ b/source/blender/io/usd/intern/usd_reader_light.cc
@@ -46,13 +46,21 @@ void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime
 {
   Light *blight = (Light *)object_->data;
 
+  if (blight == nullptr) {
+    return;
+  }
+
+  if (!prim_) {
+    return;
+  }
+
   pxr::UsdLuxLight light_prim(prim_);
 
   if (!light_prim) {
     return;
   }
 
-  pxr::UsdLuxShapingAPI shapingAPI(light_prim);
+  pxr::UsdLuxShapingAPI shaping_api(light_prim);
 
   /* Set light type. */
 
@@ -68,7 +76,7 @@ void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime
   else if (prim_.IsA<pxr::UsdLuxSphereLight>()) {
     blight->type = LA_LOCAL;
 
-    if (shapingAPI.GetShapingConeAngleAttr().IsAuthored()) {
+    if (shaping_api && shaping_api.GetShapingConeAngleAttr().IsAuthored()) {
       blight->type = LA_SPOT;
     }
   }
@@ -78,10 +86,12 @@ void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime
 
   /* Set light values. */
 
-  pxr::VtValue intensity;
-  light_prim.GetIntensityAttr().Get(&intensity, motionSampleTime);
-
-  blight->energy = intensity.Get<float>() * this->import_params_.light_intensity_scale;
+  if (pxr::UsdAttribute intensity_attr = light_prim.GetIntensityAttr()) {
+    float intensity = 0.0f;
+    if (intensity_attr.Get(&intensity, motionSampleTime)) {
+      blight->energy = intensity * this->import_params_.light_intensity_scale;
+    }
+  }
 
   /* TODO(makowalsk): Not currently supported. */
 #if 0
@@ -95,17 +105,21 @@ void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime
   light_prim.GetDiffuseAttr().Get(&diffuse, motionSampleTime);
 #endif
 
-  pxr::VtValue specular;
-  light_prim.GetSpecularAttr().Get(&specular, motionSampleTime);
-  blight->spec_fac = specular.Get<float>();
+  if (pxr::UsdAttribute spec_attr = light_prim.GetSpecularAttr()) {
+    float spec = 0.0f;
+    if (spec_attr.Get(&spec, motionSampleTime)) {
+      blight->spec_fac = spec;
+    }
+  }
 
-  pxr::VtValue color;
-  light_prim.GetColorAttr().Get(&color, motionSampleTime);
-  /* Calling UncheckedGet() to silence compiler warning. */
-  pxr::GfVec3f color_vec = color.UncheckedGet<pxr::GfVec3f>();
-  blight->r = color_vec[0];
-  blight->g = color_vec[1];
-  blight->b = color_vec[2];
+  if (pxr::UsdAttribute color_attr = light_prim.GetColorAttr()) {
+    pxr::GfVec3f color;
+    if (color_attr.Get(&color, motionSampleTime)) {
+      blight->r = color[0];
+      blight->g = color[1];
+      blight->b = color[2];
+    }
+  }
 
   /* TODO(makowalski): Not currently supported. */
 #if 0
@@ -125,23 +139,38 @@ void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime
 
         pxr::UsdLuxRectLight rect_light(prim_);
 
-        pxr::VtValue width;
-        rect_light.GetWidthAttr().Get(&width, motionSampleTime);
-
-        pxr::VtValue height;
-        rect_light.GetHeightAttr().Get(&height, motionSampleTime);
-
-        blight->area_size = width.Get<float>();
-        blight->area_sizey = height.Get<float>();
+        if (!rect_light) {
+          break;
+        }
+
+        if (pxr::UsdAttribute width_attr = rect_light.GetWidthAttr()) {
+          float width = 0.0f;
+          if (width_attr.Get(&width, motionSampleTime)) {
+            blight->area_size = width;
+          }
+        }
+
+        if (pxr::UsdAttribute height_attr = rect_light.GetHeightAttr()) {
+          float height = 0.0f;
+          if (height_attr.Get(&height, motionSampleTime)) {
+            blight->area_sizey = height;
+          }
+        }
       }
       else if (blight->area_shape == LA_AREA_DISK && prim_.IsA<pxr::UsdLuxDiskLight>()) {
 
         pxr::UsdLuxDiskLight disk_light(prim_);
 
-        pxr::VtValue radius;
-        disk_light.GetRadiusAttr().Get(&radius, motionSampleTime);
+        if (!disk_light) {
+          break;
+        }
 
-        blight->area_size = radius.Get<float>() * 2.0f;
+        if (pxr::UsdAttribute radius_attr = disk_light.GetRadiusAttr()) {
+          float radius = 0.0f;
+          if (radius_attr.Get(&radius, motionSampleTime)) {
+            blight->area_size = radius * 2.0f;
+          }
+        }
       }
       break;
     case LA_LOCAL:
@@ -149,10 +178,16 @@ void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime
 
         pxr::UsdLuxSphereLight sphere_light(prim_);
 
-        pxr::VtValue radius;
-        sphere_light.GetRadiusAttr().Get(&radius, motionSampleTime);
+        if (!sphere_light) {
+          break;
+        }
 
-        blight->area_size = radius.Get<float>();
+        if (pxr::UsdAttribute radius_attr = sphere_light.GetRadiusAttr()) {
+          float radius = 0.0f;
+          if (radius_attr.Get(&radius, motionSampleTime)) {
+            blight->area_size = radius;
+          }
+        }
       }
       break;
     case LA_SPOT:
@@ -160,27 +195,50 @@ void USDLightReader::read_object_data(Main *bmain, const double motionSampleTime
 
         pxr::UsdLuxSphereLight sphere_light(prim_);
 
-        pxr::VtValue radius;
-        sphere_light.GetRadiusAttr().Get(&radius, motionSampleTime);
-
-        blight->area_size = radius.Get<float>();
-
-        pxr::VtValue coneAngle;
-        shapingAPI.GetShapingConeAngleAttr().Get(&coneAngle, motionSampleTime);
-        blight->spotsize = coneAngle.Get<float>() * ((float)M_PI / 180.0f) * 2.0f;
-
-        pxr::VtValue spotBlend;
-        shapingAPI.GetShapingConeSoftnessAttr().Get(&spotBlend, motionSampleTime);
-        blight->spotblend = spotBlend.Get<float>();
+        if (!sphere_light) {
+          break;
+        }
+
+        if (pxr::UsdAttribute radius_attr = sphere_light.GetRadiusAttr()) {
+          float radius = 0.0f;
+          if (radius_attr.Get(&radius, motionSampleTime)) {
+            blight->area_size = radius;
+          }
+        }
+
+        if (!shaping_api) {
+          break;
+        }
+
+        if (pxr::UsdAttribute cone_angle_attr = shaping_api.GetShapingConeAngleAttr()) {
+          float cone_angle = 0.0f;
+          if (cone_angle_attr.Get(&cone_angle, motionSampleTime)) {
+            blight->spotsize = cone_angle * ((float)M_PI / 180.0f) * 2.0f;
+          }
+        }
+
+        if (pxr::UsdAttribute cone_softness_attr = shaping_api.GetShapingConeSoftnessAttr()) {
+          float cone_softness = 0.0f;
+          if (cone_softness_attr.Get(&cone_softness, motionSampleTime)) {
+            blight->spotblend = cone_softness;
+          }
+        }
       }
       break;
     case LA_SUN:
       if (prim_.IsA<pxr::UsdLuxDistantLight>()) {
         pxr::UsdLuxDistantLight distant_light(prim_);
 
-        pxr::VtValue angle;
-        distant_light.GetAngleAttr().Get(&angle, motionSampleTime);
-        blight->sun_angle = angle.Get<float>();
+        if (!distant_light) {
+          break;
+        }
+
+        if (pxr::UsdAttribute angle_attr = distant_light.GetAngleAttr()) {
+          float angle = 0.0f;
+          if (angle_attr.Get(&angle, motionSampleTime)) {
+            blight->sun_angle = angle;
+          }
+        }
       }
       break;
   }



More information about the Bf-blender-cvs mailing list