@staticmethod
def bmesh_to_mesh(gltf, pymesh, bme, mesh):
    bme.to_mesh(mesh)
    if (len(bme.verts.layers.shape) != 0):
        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)
    mesh.update()
    if (gltf.import_settings['import_shading'] == 'NORMALS'):
        mesh.create_normals_split()
    use_smooths = []
    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]
                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:
            assert False
        face_idx += prim.num_faces
    mesh.polygons.foreach_set('use_smooth', use_smooths)
    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
@staticmethod
def bmesh_to_mesh(gltf, pymesh, bme, mesh):
    bme.to_mesh(mesh)
    if (len(bme.verts.layers.shape) != 0):
        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)
    mesh.update()
    if (gltf.import_settings['import_shading'] == 'NORMALS'):
        mesh.create_normals_split()
    use_smooths = []
    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]
                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:
            assert False
        face_idx += prim.num_faces
    mesh.polygons.foreach_set('use_smooth', use_smooths)
    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