Skip to content
Snippets Groups Projects
export_fbx_bin.py 140 KiB
Newer Older
  • Learn to ignore specific revisions
  •     "bake_anim", "bake_anim_use_nla_strips", "bake_anim_use_all_actions", "bake_anim_step", "bake_anim_simplify_factor",
    
        "use_metadata", "media_settings", "use_custom_properties",
    ))
    
    
    # This func can be called with just the filepath
    def save_single(operator, scene, filepath="",
                    global_matrix=Matrix(),
                    axis_up="Z",
                    axis_forward="Y",
                    context_objects=None,
                    object_types=None,
                    use_mesh_modifiers=True,
                    mesh_smooth_type='FACE',
    
                    bake_anim=True,
    
                    bake_anim_use_all_actions=True,
    
                    bake_anim_step=1.0,
                    bake_anim_simplify_factor=1.0,
    
                    use_metadata=True,
                    path_mode='AUTO',
                    use_mesh_edges=True,
                    use_tspace=True,
                    embed_textures=False,
                    use_custom_properties=False,
    
                    **kwargs
                    ):
    
        if object_types is None:
    
            object_types = {'EMPTY', 'CAMERA', 'LAMP', 'ARMATURE', 'MESH', 'OTHER'}
    
        if 'OTHER' in object_types:
            object_types |= BLENDER_OTHER_OBJECT_TYPES
    
    
        global_scale = global_matrix.median_scale
    
        global_matrix_inv = global_matrix.inverted()
    
        # For transforming mesh normals.
        global_matrix_inv_transposed = global_matrix_inv.transposed()
    
    
        # Only embed textures in COPY mode!
        if embed_textures and path_mode != 'COPY':
            embed_textures = False
    
        media_settings = FBXSettingsMedia(
            path_mode,
            os.path.dirname(bpy.data.filepath),  # base_src
            os.path.dirname(filepath),  # base_dst
            # Local dir where to put images (medias), using FBX conventions.
            os.path.splitext(os.path.basename(filepath))[0] + ".fbm",  # subdir
            embed_textures,
            set(),  # copy_set
        )
    
        settings = FBXSettings(
    
            operator.report, (axis_up, axis_forward), global_matrix, global_scale,
    
            bake_space_transform, global_matrix_inv, global_matrix_inv_transposed,
    
            context_objects, object_types, use_mesh_modifiers,
    
            mesh_smooth_type, use_mesh_edges, use_tspace, False,
    
            bake_anim, bake_anim_use_nla_strips, bake_anim_use_all_actions, bake_anim_step, bake_anim_simplify_factor,
    
            False, media_settings, use_custom_properties,
    
        )
    
        import bpy_extras.io_utils
    
        print('\nFBX export starting... %r' % filepath)
        start_time = time.process_time()
    
        # Generate some data about exported scene...
        scene_data = fbx_data_from_scene(scene, settings)
    
        root = elem_empty(None, b"")  # Root element has no id, as it is not saved per se!
    
        # Mostly FBXHeaderExtension and GlobalSettings.
        fbx_header_elements(root, scene_data)
    
        # Documents and References are pretty much void currently.
        fbx_documents_elements(root, scene_data)
        fbx_references_elements(root, scene_data)
    
        # Templates definitions.
        fbx_definitions_elements(root, scene_data)
    
        # Actual data.
        fbx_objects_elements(root, scene_data)
    
        # How data are inter-connected.
        fbx_connections_elements(root, scene_data)
    
        # Animation.
        fbx_takes_elements(root, scene_data)
    
    
        # And we are down, we can write the whole thing!
        encode_bin.write(filepath, root, FBX_VERSION)
    
        # copy all collected files, if we did not embed them.
        if not media_settings.embed_textures:
            bpy_extras.io_utils.path_reference_copy(media_settings.copy_set)
    
        print('export finished in %.4f sec.' % (time.process_time() - start_time))
        return {'FINISHED'}
    
    
    # defaults for applications, currently only unity but could add others.
    def defaults_unity3d():
        return {
            "global_matrix": Matrix.Rotation(-math.pi / 2.0, 4, 'X'),
            "use_selection": False,
            "object_types": {'ARMATURE', 'EMPTY', 'MESH'},
            "use_mesh_modifiers": True,
    
            #"use_armature_deform_only": True,
            "bake_anim": True,
            #"use_anim_optimize": False,
            #"use_anim_action_all": True,
    
            "batch_mode": 'OFF',
    
            # Should really be True, but it can cause problems if a model is already in a scene or prefab
            # with the old transforms.
            "bake_space_transform": False,
    
        }
    
    
    def save(operator, context,
             filepath="",
             use_selection=False,
             batch_mode='OFF',
             use_batch_own_dir=False,
             **kwargs
             ):
        """
        This is a wrapper around save_single, which handles multi-scenes (or groups) cases, when batch-exporting a whole
        .blend file.
        """
    
        ret = None
    
        org_mode = None
        if context.active_object and context.active_object.mode != 'OBJECT' and bpy.ops.object.mode_set.poll():
            org_mode = context.active_object.mode
            bpy.ops.object.mode_set(mode='OBJECT')
    
        if batch_mode == 'OFF':
            kwargs_mod = kwargs.copy()
            if use_selection:
                kwargs_mod["context_objects"] = context.selected_objects
            else:
                kwargs_mod["context_objects"] = context.scene.objects
    
            ret = save_single(operator, context.scene, filepath, **kwargs_mod)
        else:
            fbxpath = filepath
    
            prefix = os.path.basename(fbxpath)
            if prefix:
                fbxpath = os.path.dirname(fbxpath)
    
            if batch_mode == 'GROUP':
                data_seq = bpy.data.groups
            else:
                data_seq = bpy.data.scenes
    
            # call this function within a loop with BATCH_ENABLE == False
            # no scene switching done at the moment.
            # orig_sce = context.scene
    
            new_fbxpath = fbxpath  # own dir option modifies, we need to keep an original
            for data in data_seq:  # scene or group
                newname = "_".join((prefix, bpy.path.clean_name(data.name)))
    
                if use_batch_own_dir:
                    new_fbxpath = os.path.join(fbxpath, newname)
                    # path may already exist
                    # TODO - might exist but be a file. unlikely but should probably account for it.
    
                    if not os.path.exists(new_fbxpath):
                        os.makedirs(new_fbxpath)
    
                filepath = os.path.join(new_fbxpath, newname + '.fbx')
    
                print('\nBatch exporting %s as...\n\t%r' % (data, filepath))
    
                if batch_mode == 'GROUP':  # group
                    # group, so objects update properly, add a dummy scene.
                    scene = bpy.data.scenes.new(name="FBX_Temp")
                    scene.layers = [True] * 20
                    # bpy.data.scenes.active = scene # XXX, cant switch
                    for ob_base in data.objects:
                        scene.objects.link(ob_base)
    
                    scene.update()
                    # TODO - BUMMER! Armatures not in the group wont animate the mesh
                else:
                    scene = data
    
                kwargs_batch = kwargs.copy()
                kwargs_batch["context_objects"] = data.objects
    
                save_single(operator, scene, filepath, **kwargs_batch)
    
                if batch_mode == 'GROUP':
                    # remove temp group scene
                    bpy.data.scenes.remove(scene)
    
            # no active scene changing!
            # bpy.data.scenes.active = orig_sce
    
            ret = {'FINISHED'}  # so the script wont run after we have batch exported.
    
        if context.active_object and org_mode and bpy.ops.object.mode_set.poll():
            bpy.ops.object.mode_set(mode=org_mode)
    
        return ret