File headers: use SPDX license identifiers
[blender-addons.git] / io_scene_gltf2 / blender / imp / gltf2_blender_light.py
blobfb0605984fed6997ee71ef8974872e0fa5e4bfdd
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2018-2021 The glTF-Blender-IO authors.
4 import bpy
5 from math import pi
7 from ..com.gltf2_blender_extras import set_extras
8 from io_scene_gltf2.io.imp.gltf2_io_user_extensions import import_user_extensions
11 class BlenderLight():
12 """Blender Light."""
13 def __new__(cls, *args, **kwargs):
14 raise RuntimeError("%s should not be instantiated" % cls)
16 @staticmethod
17 def create(gltf, vnode, light_id):
18 """Light creation."""
19 pylight = gltf.data.extensions['KHR_lights_punctual']['lights'][light_id]
21 import_user_extensions('gather_import_light_before_hook', gltf, vnode, pylight)
23 if pylight['type'] == "directional":
24 light = BlenderLight.create_directional(gltf, light_id)
25 elif pylight['type'] == "point":
26 light = BlenderLight.create_point(gltf, light_id)
27 elif pylight['type'] == "spot":
28 light = BlenderLight.create_spot(gltf, light_id)
30 if 'color' in pylight.keys():
31 light.color = pylight['color']
33 if 'intensity' in pylight.keys():
34 light.energy = pylight['intensity']
36 # TODO range
38 set_extras(light, pylight.get('extras'))
40 return light
42 @staticmethod
43 def create_directional(gltf, light_id):
44 pylight = gltf.data.extensions['KHR_lights_punctual']['lights'][light_id]
46 if 'name' not in pylight.keys():
47 pylight['name'] = "Sun"
49 sun = bpy.data.lights.new(name=pylight['name'], type="SUN")
50 return sun
52 @staticmethod
53 def create_point(gltf, light_id):
54 pylight = gltf.data.extensions['KHR_lights_punctual']['lights'][light_id]
56 if 'name' not in pylight.keys():
57 pylight['name'] = "Point"
59 point = bpy.data.lights.new(name=pylight['name'], type="POINT")
60 return point
62 @staticmethod
63 def create_spot(gltf, light_id):
64 pylight = gltf.data.extensions['KHR_lights_punctual']['lights'][light_id]
66 if 'name' not in pylight.keys():
67 pylight['name'] = "Spot"
69 spot = bpy.data.lights.new(name=pylight['name'], type="SPOT")
71 # Angles
72 if 'spot' in pylight.keys() and 'outerConeAngle' in pylight['spot']:
73 spot.spot_size = pylight['spot']['outerConeAngle'] * 2
74 else:
75 spot.spot_size = pi / 2
77 if 'spot' in pylight.keys() and 'innerConeAngle' in pylight['spot']:
78 spot.spot_blend = 1 - ( pylight['spot']['innerConeAngle'] / pylight['spot']['outerConeAngle'] )
79 else:
80 spot.spot_blend = 1.0
82 return spot