def __gather_gltf(exporter, export_settings):
    scenes, animations = gltf2_blender_gather.gather_gltf2(export_settings)

    if export_settings['gltf_draco_mesh_compression']:
        gltf2_io_draco_compression_extension.compress_scene_primitives(
            scenes, export_settings)
        exporter.add_draco_extension()

    for scene in scenes:
        exporter.add_scene(scene)
    for animation in animations:
        exporter.add_animation(animation)
Пример #2
0
def __gather_gltf(exporter, export_settings):
    active_scene_idx, scenes, animations = gltf2_blender_gather.gather_gltf2(
        export_settings)

    if export_settings['gltf_draco_mesh_compression']:
        gltf2_io_draco_compression_extension.encode_scene_primitives(
            scenes, export_settings)
        exporter.add_draco_extension()

    for idx, scene in enumerate(scenes):
        exporter.add_scene(scene, idx == active_scene_idx)
    for animation in animations:
        exporter.add_animation(animation)
Пример #3
0
def __gather_gltf(exporter, export_settings):
    active_scene_idx, scenes, animations = gltf2_blender_gather.gather_gltf2(export_settings)

    unused_skins = export_settings['vtree'].get_unused_skins()

    if export_settings['gltf_draco_mesh_compression']:
        gltf2_io_draco_compression_extension.encode_scene_primitives(scenes, export_settings)
        exporter.add_draco_extension()

    export_user_extensions('gather_gltf_hook', export_settings, active_scene_idx, scenes, animations)

    for idx, scene in enumerate(scenes):
        exporter.add_scene(scene, idx==active_scene_idx)
    for animation in animations:
        exporter.add_animation(animation)
    exporter.traverse_unused_skins(unused_skins)
Пример #4
0
def __gather_gltf(exporter, export_settings):
    active_scene_idx, scenes, animations = gltf2_blender_gather.gather_gltf2(
        export_settings)

    plan = {
        'active_scene_idx': active_scene_idx,
        'scenes': scenes,
        'animations': animations
    }
    export_user_extensions('gather_gltf_hook', export_settings, plan)
    active_scene_idx, scenes, animations = plan['active_scene_idx'], plan[
        'scenes'], plan['animations']

    if export_settings['gltf_draco_mesh_compression']:
        gltf2_io_draco_compression_extension.encode_scene_primitives(
            scenes, export_settings)
        exporter.add_draco_extension()

    for idx, scene in enumerate(scenes):
        exporter.add_scene(scene, idx == active_scene_idx)
    for animation in animations:
        exporter.add_animation(animation)
def __add_root_objects(exporter, export_settings):
    scenes, animations = gltf2_blender_gather.gather_gltf2(export_settings)
    for scene in scenes:
        exporter.add_scene(scene)
    for animation in animations:
        exporter.add_animation(animation)
Пример #6
0
def export(blender_scene, selected, hubs_config, registered_hubs_components):
    if bpy.data.filepath == '':
        raise RuntimeError("Save project before exporting")

    filepath = bpy.data.filepath.replace('.blend', '')
    filename_ext = '.glb'

    export_settings = {}
    export_settings['timestamp'] = datetime.datetime.now()
    export_settings['gltf_filepath'] = bpy.path.ensure_ext(
        filepath, filename_ext)

    if os.path.exists(export_settings['gltf_filepath']):
        os.remove(export_settings['gltf_filepath'])

    export_settings['gltf_filedirectory'] = os.path.dirname(
        export_settings['gltf_filepath']) + '/'
    export_settings['gltf_format'] = 'GLB'
    export_settings['gltf_image_format'] = 'NAME'
    export_settings['gltf_copyright'] = ''
    export_settings['gltf_texcoords'] = True
    export_settings['gltf_normals'] = True
    export_settings['gltf_tangents'] = False
    export_settings['gltf_draco_mesh_compression'] = False
    export_settings['gltf_materials'] = True
    export_settings['gltf_colors'] = True
    export_settings['gltf_cameras'] = False
    export_settings['gltf_selected'] = selected
    export_settings['gltf_layers'] = True
    export_settings['gltf_extras'] = False
    export_settings['gltf_yup'] = True
    export_settings['gltf_apply'] = False
    export_settings['gltf_current_frame'] = False
    export_settings['gltf_animations'] = False
    export_settings['gltf_frame_range'] = False
    export_settings['gltf_move_keyframes'] = False
    export_settings['gltf_force_sampling'] = False
    export_settings['gltf_skins'] = False
    export_settings['gltf_all_vertex_influences'] = False
    export_settings['gltf_frame_step'] = 1
    export_settings['gltf_morph'] = False
    export_settings['gltf_morph_normal'] = False
    export_settings['gltf_morph_tangent'] = False
    export_settings['gltf_lights'] = False
    export_settings['gltf_displacement'] = False
    export_settings['gltf_binary'] = bytearray()
    export_settings['gltf_binaryfilename'] = os.path.splitext(
        os.path.basename(bpy.path.ensure_ext(filepath,
                                             filename_ext)))[0] + '.bin'

    export_settings['hubs_config'] = hubs_config
    export_settings['registered_hubs_components'] = registered_hubs_components

    gltf2_blender_gather_nodes.__gather_extensions = patched_gather_extensions
    gltf2_blender_gather_materials.__gather_extensions = patched_gather_extensions

    # In most recent version this function will return active_scene
    # as the first value for a total of 3 return values
    scenes, _animations = gltf2_blender_gather.gather_gltf2(export_settings)

    for gltf_scene in scenes:
        gltf_scene.extensions = patched_gather_extensions(
            blender_scene, export_settings)

    exporter = GlTF2Exporter(export_settings['gltf_copyright'])
    exporter.add_scene(scenes[0], True)

    buffer = exporter.finalize_buffer(export_settings['gltf_filedirectory'],
                                      is_glb=True)
    exporter.finalize_images(export_settings['gltf_filedirectory'])

    gltf_json = fix_json(exporter.glTF.to_dict())

    extension_name = hubs_config["gltfExtensionName"]

    if 'extensionsRequired' in gltf_json:
        gltf_json['extensionsRequired'].remove(extension_name)
        if not gltf_json['extensionsRequired']:
            del gltf_json['extensionsRequired']

    if 'extensionsUsed' not in gltf_json:
        gltf_json['extensionsUsed'] = [extension_name]
    elif extension_name not in gltf_json['extensionsUsed']:
        gltf_json['extensionsUsed'].append(extension_name)

    if 'extensions' not in gltf_json:
        gltf_json['extensions'] = {}

    gltf_json['extensions'][extension_name] = {
        "version": hubs_config["gltfExtensionVersion"]
    }

    gltf2_io_export.save_gltf(gltf_json, export_settings,
                              gltf2_blender_json.BlenderJSONEncoder, buffer)

    gltf2_blender_gather_nodes.__gather_extensions = original_gather_extensions

    return export_settings['gltf_filepath']
Пример #7
0
def save(operator, context, export_settings):
    """
    Starts the glTF 2.0 export and saves to content either to a .gltf or .glb file.
    """

    print_console('INFO', 'Starting glTF 2.0 export')
    bpy.context.window_manager.progress_begin(0, 100)
    bpy.context.window_manager.progress_update(0)

    #

    prepare(export_settings)

    #

    glTF = Gltf(accessors=[],
                animations=[],
                asset=None,
                buffers=[],
                buffer_views=[],
                cameras=[],
                extensions={},
                extensions_required=[],
                extensions_used=[],
                extras=None,
                images=[],
                materials=[],
                meshes=[],
                nodes=[],
                samplers=[],
                scene=-1,
                scenes=[],
                skins=[],
                textures=[])

    if export_settings['gltf_experimental']:
        scenes, animations = gltf2_blender_gather.gather_gltf2(export_settings)
        if not export_settings['gltf_copyright']:
            export_settings['gltf_copyright'] = None
        exporter = gltf2_blender_gltf2_exporter.GlTF2Exporter(
            copyright=export_settings['gltf_copyright'])
        for scene in scenes:
            exporter.add_scene(scene)
        for animation in animations:
            exporter.add_animation(animation)

        if export_settings['gltf_format'] == 'ASCII':
            exporter.finalize_buffer(export_settings['gltf_filedirectory'],
                                     export_settings['gltf_binaryfilename'])
        else:
            exporter.finalize_buffer(export_settings['gltf_filedirectory'])
        exporter.finalize_images(export_settings['gltf_filedirectory'])
        glTF = exporter.glTF
    else:
        generate_glTF(operator, context, export_settings, glTF)

    #

    # TODO: move to custom JSON encoder
    def dict_strip(obj):
        o = obj
        if isinstance(obj, dict):
            o = {}
            for k, v in obj.items():
                if v is None:
                    continue
                elif isinstance(v, list) and len(v) == 0:
                    continue
                o[k] = dict_strip(v)
        elif isinstance(obj, list):
            o = []
            for v in obj:
                o.append(dict_strip(v))
        elif isinstance(obj, float):
            # force floats to int, if they are integers (prevent INTEGER_WRITTEN_AS_FLOAT validator warnings)
            if int(obj) == obj:
                return int(obj)
        return o

    import sys
    import traceback

    try:
        save_gltf(dict_strip(glTF.to_dict()), export_settings,
                  gltf2_blender_json.BlenderJSONEncoder)
    except AssertionError as e:
        _, _, tb = sys.exc_info()
        traceback.print_tb(tb)  # Fixed format
        tb_info = traceback.extract_tb(tb)
        for tbi in tb_info:
            filename, line, func, text = tbi
            print_console(
                'ERROR', 'An error occurred on line {} in statement {}'.format(
                    line, text))
        print_console('ERROR', str(e))
        raise e

    #

    finish(export_settings)

    #

    print_console('INFO', 'Finished glTF 2.0 export')
    bpy.context.window_manager.progress_end()
    print_newline()

    return {'FINISHED'}