|
3 | 3 | from .data_classes import NormalTextureInfoData |
4 | 4 |
|
5 | 5 |
|
| 6 | +def create_if_data(cls, data, attr): |
| 7 | + return cls.from_data(data.get(attr)) if attr in data and len(data[attr]) else None |
| 8 | + |
| 9 | + |
6 | 10 | class KHR_materials_transmission(BaseGLTFDataClass): |
7 | 11 | """glTF extension that defines the optical transmission of a material. |
8 | 12 |
|
@@ -40,7 +44,7 @@ def from_data(cls, dct): |
40 | 44 | return None |
41 | 45 | return cls( |
42 | 46 | transmission_factor=dct.get("transmissionFactor"), |
43 | | - transmission_texture=TextureInfoData.from_data(dct.get("transmissionTexture")), |
| 47 | + transmission_texture=create_if_data(TextureInfoData, dct, "transmissionTexture"), |
44 | 48 | extensions=cls.extensions_from_data(dct.get("extensions")), |
45 | 49 | extras=dct.get("extras"), |
46 | 50 | ) |
@@ -91,9 +95,9 @@ def from_data(cls, dct): |
91 | 95 | return None |
92 | 96 | return cls( |
93 | 97 | specular_factor=dct.get("specularFactor"), |
94 | | - specular_texture=TextureInfoData.from_data(dct.get("specularTexture")), |
| 98 | + specular_texture=create_if_data(TextureInfoData, dct, "specularTexture"), |
95 | 99 | specular_color_factor=dct.get("specularColorFactor"), |
96 | | - specular_color_texture=TextureInfoData.from_data(dct.get("specularColorTexture")), |
| 100 | + specular_color_texture=create_if_data(TextureInfoData, dct, "specularColorTexture"), |
97 | 101 | extensions=cls.extensions_from_data(dct.get("extensions")), |
98 | 102 | extras=dct.get("extras"), |
99 | 103 | ) |
@@ -186,10 +190,10 @@ def from_data(cls, dct): |
186 | 190 | return None |
187 | 191 | return cls( |
188 | 192 | clearcoat_factor=dct.get("clearcoatFactor"), |
189 | | - clearcoat_texture=TextureInfoData.from_data(dct.get("clearcoatTexture")), |
| 193 | + clearcoat_texture=create_if_data(TextureInfoData, dct, "clearcoatTexture"), |
190 | 194 | clearcoat_roughness_factor=dct.get("clearcoatRoughnessFactor"), |
191 | | - clearcoat_roughness_texture=TextureInfoData.from_data(dct.get("clearcoatRoughnessTexture")), |
192 | | - clearcoat_normal_texture=NormalTextureInfoData.from_data(dct.get("clearcoatNormalTexture")), |
| 195 | + clearcoat_roughness_texture=create_if_data(TextureInfoData, dct, "clearcoatRoughnessTexture"), |
| 196 | + clearcoat_normal_texture=create_if_data(NormalTextureInfoData, dct, "clearcoatNormalTexture"), |
193 | 197 | extensions=cls.extensions_from_data(dct.get("extensions")), |
194 | 198 | extras=dct.get("extras"), |
195 | 199 | ) |
@@ -294,20 +298,139 @@ def from_data(cls, dct): |
294 | 298 | return None |
295 | 299 | return cls( |
296 | 300 | diffuse_factor=dct.get("diffuseFactor"), |
297 | | - diffuse_texture=TextureInfoData.from_data(dct.get("diffuseTexture")), |
| 301 | + diffuse_texture=create_if_data(TextureInfoData, dct, "diffuseTexture"), |
298 | 302 | specular_factor=dct.get("specularFactor"), |
299 | 303 | glossiness_factor=dct.get("glossinessFactor"), |
300 | | - specular_glossiness_texture=TextureInfoData.from_data(dct.get("specularGlossinessTexture")), |
| 304 | + specular_glossiness_texture=create_if_data(TextureInfoData, dct, "specularGlossinessTexture"), |
| 305 | + extensions=cls.extensions_from_data(dct.get("extensions")), |
| 306 | + extras=dct.get("extras"), |
| 307 | + ) |
| 308 | + |
| 309 | + |
| 310 | +class LightSpot(BaseGLTFDataClass): |
| 311 | + """LightSpot |
| 312 | +
|
| 313 | + https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/schema/light.spot.schema.json |
| 314 | + """ |
| 315 | + |
| 316 | + def __init__( |
| 317 | + self, |
| 318 | + innerConeAngle=None, |
| 319 | + outerConeAngle=None, |
| 320 | + extensions=None, |
| 321 | + extras=None, |
| 322 | + ): |
| 323 | + super(LightSpot, self).__init__(extras, extensions) |
| 324 | + self.innerConeAngle = innerConeAngle # Angle in radians from centre of spotlight where falloff ends. |
| 325 | + self.outerConeAngle = outerConeAngle # Angle in radians from centre of spotlight where falloff begins. |
| 326 | + |
| 327 | + def to_data(self, texture_index_by_key, **kwargs): |
| 328 | + dct = super(LightSpot, self).to_data(texture_index_by_key, **kwargs) |
| 329 | + if self.innerConeAngle is not None: |
| 330 | + dct["innerConeAngle"] = self.innerConeAngle |
| 331 | + if self.outerConeAngle is not None: |
| 332 | + dct["outerConeAngle"] = self.outerConeAngle |
| 333 | + return dct |
| 334 | + |
| 335 | + @classmethod |
| 336 | + def from_data(cls, dct): |
| 337 | + if dct is None: |
| 338 | + return None |
| 339 | + return cls( |
| 340 | + innerConeAngle=dct.get("innerConeAngle"), outerConeAngle=dct.get("outerConeAngle"), extensions=cls.extensions_from_data(dct.get("extensions")), extras=dct.get("extras") |
| 341 | + ) |
| 342 | + |
| 343 | + |
| 344 | +class LightType(object): |
| 345 | + """Specifies the light type.""" |
| 346 | + |
| 347 | + directional = "directional" |
| 348 | + point = "point" |
| 349 | + spot = "spot" |
| 350 | + |
| 351 | + |
| 352 | +class Light(BaseGLTFDataClass): |
| 353 | + """A directional, point, or spot light. |
| 354 | +
|
| 355 | + https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual/schema/light.schema.json |
| 356 | + """ |
| 357 | + |
| 358 | + key = "KHR_lights_punctual" |
| 359 | + |
| 360 | + def __init__(self, color=None, intensity=None, spot=None, type=None, range=None, name=None, extensions=None, extras=None): |
| 361 | + super(Light, self).__init__(extras, extensions) |
| 362 | + self.color = color # [1, 1, 1] |
| 363 | + self.intensity = intensity # 1.0 |
| 364 | + self.spot = spot # LightSpot |
| 365 | + self.type = type or LightType.directional |
| 366 | + self.range = range # A distance cutoff at which the light's intensity may be considered to have reached zero. |
| 367 | + self.name = name |
| 368 | + |
| 369 | + def to_data(self, texture_index_by_key, **kwargs): |
| 370 | + dct = super(Light, self).to_data(texture_index_by_key, **kwargs) |
| 371 | + if self.color is not None: |
| 372 | + dct["color"] = self.color |
| 373 | + if self.intensity is not None: |
| 374 | + dct["intensity"] = self.intensity |
| 375 | + if self.spot is not None: |
| 376 | + dct["spot"] = self.spot.to_data() |
| 377 | + if self.type is not None: |
| 378 | + dct["type"] = self.type |
| 379 | + if self.range is not None: |
| 380 | + dct["range"] = self.range |
| 381 | + if self.name is not None: |
| 382 | + dct["name"] = self.name |
| 383 | + return dct |
| 384 | + |
| 385 | + @classmethod |
| 386 | + def from_data(cls, dct): |
| 387 | + if dct is None: |
| 388 | + return None |
| 389 | + return cls( |
| 390 | + color=dct.get("color"), |
| 391 | + intensity=dct.get("intensity"), |
| 392 | + spot=LightSpot.from_data(dct.get("spot")), |
| 393 | + type=dct.get("type"), |
| 394 | + range=dct.get("range"), |
| 395 | + name=dct.get("name"), |
301 | 396 | extensions=cls.extensions_from_data(dct.get("extensions")), |
302 | 397 | extras=dct.get("extras"), |
303 | 398 | ) |
304 | 399 |
|
305 | 400 |
|
| 401 | +class KHR_lights_punctual(BaseGLTFDataClass): |
| 402 | + """""" |
| 403 | + |
| 404 | + key = "KHR_lights_punctual" |
| 405 | + |
| 406 | + def __init__( |
| 407 | + self, |
| 408 | + lights=None, |
| 409 | + extensions=None, |
| 410 | + extras=None, |
| 411 | + ): |
| 412 | + super(KHR_lights_punctual, self).__init__(extras, extensions) |
| 413 | + self.lights = lights |
| 414 | + |
| 415 | + def to_data(self, texture_index_by_key, **kwargs): |
| 416 | + dct = super(KHR_lights_punctual, self).to_data(texture_index_by_key, **kwargs) |
| 417 | + if self.lights is not None: |
| 418 | + dct["lights"] = self.lights |
| 419 | + return dct |
| 420 | + |
| 421 | + @classmethod |
| 422 | + def from_data(cls, dct): |
| 423 | + if dct is None: |
| 424 | + return None |
| 425 | + return cls(lights=dct.get("lights"), extensions=cls.extensions_from_data(dct.get("extensions")), extras=dct.get("extras")) |
| 426 | + |
| 427 | + |
306 | 428 | SUPPORTED_EXTENSIONS = { |
307 | 429 | KHR_materials_transmission.key: KHR_materials_transmission, |
308 | 430 | KHR_materials_clearcoat.key: KHR_materials_clearcoat, |
309 | 431 | KHR_Texture_Transform.key: KHR_Texture_Transform, |
310 | 432 | KHR_materials_pbrSpecularGlossiness.key: KHR_materials_pbrSpecularGlossiness, |
311 | 433 | KHR_materials_specular.key: KHR_materials_specular, |
312 | 434 | KHR_materials_ior.key: KHR_materials_ior, |
| 435 | + KHR_lights_punctual.key: KHR_lights_punctual, |
313 | 436 | } |
0 commit comments