Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gltf2_io_get.py « exp « io « io_scene_gltf2 - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 35c65615d99829e1776ae991327d45a23935aff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright 2018 The glTF-Blender-IO authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#
# Imports
#

import os

#
# Globals
#

#
# Functions
#


def get_material_requires_texcoords(glTF, index):
    """Query function, if a material "needs" texture coordinates. This is the case, if a texture is present and used."""
    if glTF.materials is None:
        return False

    materials = glTF.materials

    if index < 0 or index >= len(materials):
        return False

    material = materials[index]

    # General

    if material.emissive_texture is not None:
        return True

    if material.normal_texture is not None:
        return True

    if material.occlusion_texture is not None:
        return True

    # Metallic roughness

    if material.pbr_metallic_roughness is not None and \
            material.pbr_metallic_roughness.base_color_texture is not None:
        return True

    if material.pbr_metallic_roughness is not None and \
            material.pbr_metallic_roughness.metallic_roughness_texture is not None:
        return True

    return False


def get_material_requires_normals(glTF, index):
    """
    Query function, if a material "needs" normals. This is the case, if a texture is present and used.

    At point of writing, same function as for texture coordinates.
    """
    return get_material_requires_texcoords(glTF, index)


def get_material_index(glTF, name):
    """Return the material index in the glTF array."""
    if name is None:
        return -1

    if glTF.materials is None:
        return -1

    index = 0
    for material in glTF.materials:
        if material.name == name:
            return index

        index += 1

    return -1


def get_mesh_index(glTF, name):
    """Return the mesh index in the glTF array."""
    if glTF.meshes is None:
        return -1

    index = 0
    for mesh in glTF.meshes:
        if mesh.name == name:
            return index

        index += 1

    return -1


def get_skin_index(glTF, name, index_offset):
    """Return the skin index in the glTF array."""
    if glTF.skins is None:
        return -1

    skeleton = get_node_index(glTF, name)

    index = 0
    for skin in glTF.skins:
        if skin.skeleton == skeleton:
            return index + index_offset

        index += 1

    return -1


def get_camera_index(glTF, name):
    """Return the camera index in the glTF array."""
    if glTF.cameras is None:
        return -1

    index = 0
    for camera in glTF.cameras:
        if camera.name == name:
            return index

        index += 1

    return -1


def get_light_index(glTF, name):
    """Return the light index in the glTF array."""
    if glTF.extensions is None:
        return -1

    extensions = glTF.extensions

    if extensions.get('KHR_lights_punctual') is None:
        return -1

    khr_lights_punctual = extensions['KHR_lights_punctual']

    if khr_lights_punctual.get('lights') is None:
        return -1

    lights = khr_lights_punctual['lights']

    index = 0
    for light in lights:
        if light['name'] == name:
            return index

        index += 1

    return -1


def get_node_index(glTF, name):
    """Return the node index in the glTF array."""
    if glTF.nodes is None:
        return -1

    index = 0
    for node in glTF.nodes:
        if node.name == name:
            return index

        index += 1

    return -1


def get_scene_index(glTF, name):
    """Return the scene index in the glTF array."""
    if glTF.scenes is None:
        return -1

    index = 0
    for scene in glTF.scenes:
        if scene.name == name:
            return index

        index += 1

    return -1


def get_texture_index(glTF, filename):
    """Return the texture index in the glTF array by a given file path."""
    if glTF.textures is None:
        return -1

    image_index = get_image_index(glTF, filename)

    if image_index == -1:
        return -1

    for texture_index, texture in enumerate(glTF.textures):
        if image_index == texture.source:
            return texture_index

    return -1


def get_image_index(glTF, filename):
    """Return the image index in the glTF array."""
    if glTF.images is None:
        return -1

    image_name = get_image_name(filename)

    for index, current_image in enumerate(glTF.images):
        if image_name == current_image.name:
            return index

    return -1


def get_image_name(filename):
    """Return user-facing, extension-agnostic name for image."""
    return os.path.splitext(filename)[0]


def get_scalar(default_value, init_value=0.0):
    """Return scalar with a given default/fallback value."""
    return_value = init_value

    if default_value is None:
        return return_value

    return_value = default_value

    return return_value


def get_vec2(default_value, init_value=[0.0, 0.0]):
    """Return vec2 with a given default/fallback value."""
    return_value = init_value

    if default_value is None or len(default_value) < 2:
        return return_value

    index = 0
    for number in default_value:
        return_value[index] = number

        index += 1
        if index == 2:
            return return_value

    return return_value


def get_vec3(default_value, init_value=[0.0, 0.0, 0.0]):
    """Return vec3 with a given default/fallback value."""
    return_value = init_value

    if default_value is None or len(default_value) < 3:
        return return_value

    index = 0
    for number in default_value:
        return_value[index] = number

        index += 1
        if index == 3:
            return return_value

    return return_value


def get_vec4(default_value, init_value=[0.0, 0.0, 0.0, 1.0]):
    """Return vec4 with a given default/fallback value."""
    return_value = init_value

    if default_value is None or len(default_value) < 4:
        return return_value

    index = 0
    for number in default_value:
        return_value[index] = number

        index += 1
        if index == 4:
            return return_value

    return return_value


def get_index(elements, name):
    """Return index of a glTF element by a given name."""
    if elements is None or name is None:
        return -1

    index = 0
    for element in elements:
        if isinstance(element, dict):
            if element.get('name') == name:
                return index
        else:
            if element.name == name:
                return index

        index += 1

    return -1