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
14 def gather_camera(blender_camera
, export_settings
):
15 if not __filter_camera(blender_camera
, export_settings
):
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
)
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
):
40 def __gather_extras(blender_camera
, export_settings
):
41 if export_settings
['gltf_extras']:
42 return generate_extras(blender_camera
)
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(
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
71 def __gather_perspective(blender_camera
, export_settings
):
72 if __gather_type(blender_camera
, export_settings
) == "perspective":
73 perspective
= gltf2_io
.CameraPerspective(
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
87 if blender_camera
.sensor_fit
!= 'VERTICAL':
88 perspective
.yfov
= 2.0 * math
.atan(math
.tan(blender_camera
.angle
* 0.5) / perspective
.aspect_ratio
)
90 perspective
.yfov
= blender_camera
.angle
92 if blender_camera
.sensor_fit
!= 'HORIZONTAL':
93 perspective
.yfov
= blender_camera
.angle
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
104 def __gather_type(blender_camera
, export_settings
):
105 if blender_camera
.type == 'PERSP':
107 elif blender_camera
.type == 'ORTHO':
108 return "orthographic"