1# Copyright 2018-2019 The glTF-Blender-IO authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import bpy
16from ..com.gltf2_blender_extras import set_extras
17
18
19class BlenderCamera():
20    """Blender Camera."""
21    def __new__(cls, *args, **kwargs):
22        raise RuntimeError("%s should not be instantiated" % cls)
23
24    @staticmethod
25    def create(gltf, camera_id):
26        """Camera creation."""
27        pycamera = gltf.data.cameras[camera_id]
28
29        if not pycamera.name:
30            pycamera.name = "Camera"
31
32        cam = bpy.data.cameras.new(pycamera.name)
33        set_extras(cam, pycamera.extras)
34
35        # Blender create a perspective camera by default
36        if pycamera.type == "orthographic":
37            cam.type = "ORTHO"
38
39            # TODO: xmag/ymag
40
41            cam.clip_start = pycamera.orthographic.znear
42            cam.clip_end = pycamera.orthographic.zfar
43
44        else:
45            cam.angle_y = pycamera.perspective.yfov
46            cam.lens_unit = "FOV"
47            cam.sensor_fit = "VERTICAL"
48
49            # TODO: fov/aspect ratio
50
51            cam.clip_start = pycamera.perspective.znear
52            if pycamera.perspective.zfar is not None:
53                cam.clip_end = pycamera.perspective.zfar
54            else:
55                # Infinite projection
56                cam.clip_end = 1e12  # some big number
57
58
59        return cam
60