Export_3ds: Improved distance cue node search
[blender-addons.git] / io_scene_gltf2 / blender / imp / gltf2_blender_camera.py
blobddd56671677dd9fab2ff2d46610fc717e0ac6036
1 # SPDX-FileCopyrightText: 2018-2021 The glTF-Blender-IO authors
3 # SPDX-License-Identifier: Apache-2.0
5 import bpy
6 from math import tan
7 from ..com.gltf2_blender_extras import set_extras
8 from ...io.imp.gltf2_io_user_extensions import import_user_extensions
11 class BlenderCamera():
12 """Blender Camera."""
13 def __new__(cls, *args, **kwargs):
14 raise RuntimeError("%s should not be instantiated" % cls)
16 @staticmethod
17 def create(gltf, vnode, camera_id):
18 """Camera creation."""
19 pycamera = gltf.data.cameras[camera_id]
21 import_user_extensions('gather_import_camera_before_hook', gltf, vnode, pycamera)
23 if not pycamera.name:
24 pycamera.name = "Camera"
26 cam = bpy.data.cameras.new(pycamera.name)
27 set_extras(cam, pycamera.extras)
29 # Blender create a perspective camera by default
30 if pycamera.type == "orthographic":
31 cam.type = "ORTHO"
33 cam.ortho_scale = max(pycamera.orthographic.xmag, pycamera.orthographic.ymag) * 2
35 cam.clip_start = pycamera.orthographic.znear
36 cam.clip_end = pycamera.orthographic.zfar
38 # Store multiple channel data, as we will need all channels to convert to blender data when animated by KHR_animation_pointer
39 if gltf.data.extensions_used is not None and "KHR_animation_pointer" in gltf.data.extensions_used:
40 if len(pycamera.animations) > 0:
41 for anim_idx in pycamera.animations.keys():
42 for channel_idx in pycamera.animations[anim_idx]:
43 channel = gltf.data.animations[anim_idx].channels[channel_idx]
44 pointer_tab = channel.target.extensions["KHR_animation_pointer"]["pointer"].split("/")
45 if len(pointer_tab) == 5 and pointer_tab[1] == "cameras" and \
46 pointer_tab[3] == "orthographic" and \
47 pointer_tab[4] in ["xmag", "ymag"]:
48 # Store multiple channel data, as we will need all channels to convert to blender data when animated
49 if not hasattr(pycamera, "multiple_channels_mag"):
50 pycamera.multiple_channels_mag = {}
51 pycamera.multiple_channels_mag[pointer_tab[4]] = (anim_idx, channel_idx)
53 else:
54 cam.angle_y = pycamera.perspective.yfov
55 cam.lens_unit = "FOV"
56 cam.sensor_fit = "VERTICAL"
58 # TODO: fov/aspect ratio
60 cam.clip_start = pycamera.perspective.znear
61 if pycamera.perspective.zfar is not None:
62 cam.clip_end = pycamera.perspective.zfar
63 else:
64 # Infinite projection
65 cam.clip_end = 1e12 # some big number
67 pycamera.blender_object_data = cam # Needed in case of KHR_animation_pointer
69 return cam
72 @staticmethod
73 def calc_lens_from_fov(gltf, input_value, sensor):
74 return (sensor / 2.0) / tan(input_value * 0.5)