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

gltf2_blender_mesh.py « imp « blender « io_scene_gltf2 - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: adf052817e91a36c44fc95a037eeaf5a02235ac2 (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
# Copyright 2018-2019 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.

import bpy
import bmesh
from mathutils import Vector

from ..com.gltf2_blender_extras import set_extras
from .gltf2_blender_material import BlenderMaterial
from .gltf2_blender_primitive import BlenderPrimitive
from ...io.imp.gltf2_io_binary import BinaryData


class BlenderMesh():
    """Blender Mesh."""
    def __new__(cls, *args, **kwargs):
        raise RuntimeError("%s should not be instantiated" % cls)

    @staticmethod
    def create(gltf, mesh_idx, skin_idx):
        """Mesh creation."""
        pymesh = gltf.data.meshes[mesh_idx]

        # Create one bmesh, add all primitives to it, and then convert it to a
        # mesh.
        bme = bmesh.new()

        # List of all the materials this mesh will use. The material each
        # primitive uses is set by giving an index into this list.
        materials = []

        # Process all primitives
        for prim in pymesh.primitives:
            if prim.material is None:
                material_idx = None
            else:
                pymaterial = gltf.data.materials[prim.material]

                vertex_color = None
                if 'COLOR_0' in prim.attributes:
                    vertex_color = 'COLOR_0'

                # Create Blender material if needed
                if vertex_color not in pymaterial.blender_material:
                    BlenderMaterial.create(gltf, prim.material, vertex_color)
                material_name = pymaterial.blender_material[vertex_color]
                material = bpy.data.materials[material_name]

                try:
                    material_idx = materials.index(material.name)
                except ValueError:
                    materials.append(material.name)
                    material_idx = len(materials) - 1

            BlenderPrimitive.add_primitive_to_bmesh(gltf, bme, pymesh, prim, skin_idx, material_idx)

        name = pymesh.name or 'Mesh_' + str(mesh_idx)
        mesh = bpy.data.meshes.new(name)
        BlenderMesh.bmesh_to_mesh(gltf, pymesh, bme, mesh)
        bme.free()
        for name_material in materials:
            mesh.materials.append(bpy.data.materials[name_material])
        mesh.update()

        set_extras(mesh, pymesh.extras, exclude=['targetNames'])

        # Clear accessor cache after all primitives are done
        gltf.accessor_cache = {}

        return mesh

    @staticmethod
    def bmesh_to_mesh(gltf, pymesh, bme, mesh):
        bme.to_mesh(mesh)

        # Unfortunately need to do shapekeys/normals/smoothing ourselves.

        # Shapekeys
        if len(bme.verts.layers.shape) != 0:
            # The only way I could find to create a shape key was to temporarily
            # parent mesh to an object and use obj.shape_key_add.
            tmp_ob = None
            try:
                tmp_ob = bpy.data.objects.new('##gltf-import:tmp-object##', mesh)
                tmp_ob.shape_key_add(name='Basis')
                mesh.shape_keys.name = mesh.name
                for layer_name in bme.verts.layers.shape.keys():
                    tmp_ob.shape_key_add(name=layer_name)
                    key_block = mesh.shape_keys.key_blocks[layer_name]
                    layer = bme.verts.layers.shape[layer_name]

                    for i, v in enumerate(bme.verts):
                        key_block.data[i].co = v[layer]
            finally:
                if tmp_ob:
                    bpy.data.objects.remove(tmp_ob)

        # Normals
        mesh.update()

        if gltf.import_settings['import_shading'] == "NORMALS":
            mesh.create_normals_split()

        use_smooths = []  # whether to smooth for each poly
        face_idx = 0
        for prim in pymesh.primitives:
            if gltf.import_settings['import_shading'] == "FLAT" or \
                    'NORMAL' not in prim.attributes:
                use_smooths += [False] * prim.num_faces
            elif gltf.import_settings['import_shading'] == "SMOOTH":
                use_smooths += [True] * prim.num_faces
            elif gltf.import_settings['import_shading'] == "NORMALS":
                mesh_loops = mesh.loops
                for fi in range(face_idx, face_idx + prim.num_faces):
                    poly = mesh.polygons[fi]
                    # "Flat normals" are when all the vertices in poly have the
                    # poly's normal. Otherwise, smooth the poly.
                    for loop_idx in range(poly.loop_start, poly.loop_start + poly.loop_total):
                        vi = mesh_loops[loop_idx].vertex_index
                        if poly.normal.dot(bme.verts[vi].normal) <= 0.9999999:
                            use_smooths.append(True)
                            break
                    else:
                        use_smooths.append(False)
            else:
                # shouldn't happen
                assert False

            face_idx += prim.num_faces
        mesh.polygons.foreach_set('use_smooth', use_smooths)

        # Custom normals, now that every update is done
        if gltf.import_settings['import_shading'] == "NORMALS":
            custom_normals = [v.normal for v in bme.verts]
            mesh.normals_split_custom_set_from_vertices(custom_normals)
            mesh.use_auto_smooth = True