File headers: use SPDX license identifiers
[blender-addons.git] / io_scene_gltf2 / blender / exp / gltf2_blender_gather_cameras.py
blobcee3cb0643dcfe29a335ad5ba3db15b1ee9357dd
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2018-2021 The glTF-Blender-IO authors.
4 from io_scene_gltf2.blender.exp.gltf2_blender_gather_cache import cached
5 from ..com.gltf2_blender_extras import generate_extras
6 from io_scene_gltf2.io.com import gltf2_io
7 from io_scene_gltf2.io.exp.gltf2_io_user_extensions import export_user_extensions
9 import bpy
10 import math
13 @cached
14 def gather_camera(blender_camera, export_settings):
15 if not __filter_camera(blender_camera, export_settings):
16 return None
18 camera = gltf2_io.Camera(
19 extensions=__gather_extensions(blender_camera, export_settings),
20 extras=__gather_extras(blender_camera, export_settings),
21 name=__gather_name(blender_camera, export_settings),
22 orthographic=__gather_orthographic(blender_camera, export_settings),
23 perspective=__gather_perspective(blender_camera, export_settings),
24 type=__gather_type(blender_camera, export_settings)
27 export_user_extensions('gather_camera_hook', export_settings, camera, blender_camera)
29 return camera
32 def __filter_camera(blender_camera, export_settings):
33 return bool(__gather_type(blender_camera, export_settings))
36 def __gather_extensions(blender_camera, export_settings):
37 return None
40 def __gather_extras(blender_camera, export_settings):
41 if export_settings['gltf_extras']:
42 return generate_extras(blender_camera)
43 return None
46 def __gather_name(blender_camera, export_settings):
47 return blender_camera.name
50 def __gather_orthographic(blender_camera, export_settings):
51 if __gather_type(blender_camera, export_settings) == "orthographic":
52 orthographic = gltf2_io.CameraOrthographic(
53 extensions=None,
54 extras=None,
55 xmag=None,
56 ymag=None,
57 zfar=None,
58 znear=None
61 orthographic.xmag = blender_camera.ortho_scale
62 orthographic.ymag = blender_camera.ortho_scale
64 orthographic.znear = blender_camera.clip_start
65 orthographic.zfar = blender_camera.clip_end
67 return orthographic
68 return None
71 def __gather_perspective(blender_camera, export_settings):
72 if __gather_type(blender_camera, export_settings) == "perspective":
73 perspective = gltf2_io.CameraPerspective(
74 aspect_ratio=None,
75 extensions=None,
76 extras=None,
77 yfov=None,
78 zfar=None,
79 znear=None
82 width = bpy.context.scene.render.pixel_aspect_x * bpy.context.scene.render.resolution_x
83 height = bpy.context.scene.render.pixel_aspect_y * bpy.context.scene.render.resolution_y
84 perspective.aspect_ratio = width / height
86 if width >= height:
87 if blender_camera.sensor_fit != 'VERTICAL':
88 perspective.yfov = 2.0 * math.atan(math.tan(blender_camera.angle * 0.5) / perspective.aspect_ratio)
89 else:
90 perspective.yfov = blender_camera.angle
91 else:
92 if blender_camera.sensor_fit != 'HORIZONTAL':
93 perspective.yfov = blender_camera.angle
94 else:
95 perspective.yfov = 2.0 * math.atan(math.tan(blender_camera.angle * 0.5) / perspective.aspect_ratio)
97 perspective.znear = blender_camera.clip_start
98 perspective.zfar = blender_camera.clip_end
100 return perspective
101 return None
104 def __gather_type(blender_camera, export_settings):
105 if blender_camera.type == 'PERSP':
106 return "perspective"
107 elif blender_camera.type == 'ORTHO':
108 return "orthographic"
109 return None