Skip to content
Snippets Groups Projects
export_fbx_bin.py 97.5 KiB
Newer Older
  • Learn to ignore specific revisions
  •     props = elem_properties(global_settings)
        up_axis, front_axis, coord_axis = RIGHT_HAND_AXES[scene_data.settings.to_axes]
        elem_props_set(props, "p_integer", b"UpAxis", up_axis[0])
        elem_props_set(props, "p_integer", b"UpAxisSign", up_axis[1])
        elem_props_set(props, "p_integer", b"FrontAxis", front_axis[0])
        elem_props_set(props, "p_integer", b"FrontAxisSign", front_axis[1])
        elem_props_set(props, "p_integer", b"CoordAxis", coord_axis[0])
        elem_props_set(props, "p_integer", b"CoordAxisSign", coord_axis[1])
        elem_props_set(props, "p_number", b"UnitScaleFactor", 1.0)
        elem_props_set(props, "p_color_rgb", b"AmbientColor", (0.0, 0.0, 0.0))
        elem_props_set(props, "p_string", b"DefaultCamera", "Producer Perspective")
        # XXX Those time stuff is taken from a file, have no (complete) idea what it means!
        elem_props_set(props, "p_enum", b"TimeMode", 11)
        elem_props_set(props, "p_timestamp", b"TimeSpanStart", 0)
        elem_props_set(props, "p_timestamp", b"TimeSpanStop", 46186158000)  # XXX One second!
    
        ##### End of GlobalSettings element.
    
    
    def fbx_documents_elements(root, scene_data):
        """
        Write 'Document' part of FBX root.
        Seems like FBX support multiple documents, but until I find examples of such, we'll stick to single doc!
        time is expected to be a datetime.datetime object, or None (using now() in this case).
        """
        name = scene_data.scene.name
    
        ##### Start of Documents element.
        docs = elem_empty(root, b"Documents")
    
        elem_data_single_int32(docs, b"Count", 1)
    
        doc_uid = get_fbxuid_from_key("__FBX_Document__" + name)
        doc = elem_data_single_int64(docs, b"Document", doc_uid)
        doc.add_string(b"")
        doc.add_string_unicode(name)
    
        props = elem_properties(doc)
        elem_props_set(props, "p_object", b"SourceObject")
        elem_props_set(props, "p_string", b"ActiveAnimStackName", "")
    
        # XXX Some kind of ID? Offset?
        #     Anyway, as long as we have only one doc, probably not an issue.
        elem_data_single_int64(doc, b"RootNode", 0)
    
    
    def fbx_references_elements(root, scene_data):
        """
        Have no idea what references are in FBX currently... Just writing empty element.
        """
        docs = elem_empty(root, b"References")
    
    
    def fbx_definitions_elements(root, scene_data):
        """
        Templates definitions. Only used by Objects data afaik (apart from dummy GlobalSettings one).
        """
        definitions = elem_empty(root, b"Definitions")
    
        elem_data_single_int32(definitions, b"Version", FBX_TEMPLATES_VERSION)
        elem_data_single_int32(definitions, b"Count", scene_data.templates_users)
    
        fbx_templates_generate(definitions, scene_data.templates)
    
    
    def fbx_objects_elements(root, scene_data):
        """
        Data (objects, geometry, material, textures, armatures, etc.
        """
        objects = elem_empty(root, b"Objects")
    
        for lamp in scene_data.data_lamps.keys():
            fbx_data_lamp_elements(objects, lamp, scene_data)
    
        for cam in scene_data.data_cameras.keys():
            fbx_data_camera_elements(objects, cam, scene_data)
    
    
        for mesh in scene_data.data_meshes.keys():
    
            fbx_data_mesh_elements(objects, mesh, scene_data)
    
        for obj in scene_data.objects.keys():
            fbx_data_object_elements(objects, obj, scene_data)
    
        for obj in scene_data.objects.keys():
    
            if not isinstance(obj, Object) or obj.type not in {'ARMATURE'}:
    
                continue
            fbx_data_armature_elements(objects, obj, scene_data)
    
        for mat in scene_data.data_materials.keys():
            fbx_data_material_elements(objects, mat, scene_data)
    
        for tex in scene_data.data_textures.keys():
            fbx_data_texture_file_elements(objects, tex, scene_data)
    
        for vid in scene_data.data_videos.keys():
            fbx_data_video_elements(objects, vid, scene_data)
    
    
    def fbx_connections_elements(root, scene_data):
        """
        Relations between Objects (which material uses which texture, and so on).
        """
        connections = elem_empty(root, b"Connections")
    
        for c in scene_data.connections:
            elem_connection(connections, *c)
    
    
    def fbx_takes_elements(root, scene_data):
        """
        Animations. Have yet to check how this work...
        """
        takes = elem_empty(root, b"Takes")
        elem_data_single_string(takes, b"Current", b"")
    
    
    ##### "Main" functions. #####
    FBXSettingsMedia = namedtuple("FBXSettingsMedia", (
        "path_mode", "base_src", "base_dst", "subdir",
        "embed_textures", "copy_set",
    ))
    FBXSettings = namedtuple("FBXSettings", (
    
        "to_axes", "global_matrix", "global_scale",
        "bake_space_transform", "global_matrix_inv", "global_matrix_inv_trans",
        "context_objects", "object_types", "use_mesh_modifiers",
    
        "mesh_smooth_type", "use_mesh_edges", "use_tspace", "use_armature_deform_only",
        "use_anim", "use_anim_optimize", "anim_optimize_precision", "use_anim_action_all", "use_default_take",
        "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',
                    use_armature_deform_only=False,
                    use_anim=True,
                    use_anim_optimize=True,
                    anim_optimize_precision=6,
                    use_anim_action_all=False,
                    use_metadata=True,
                    path_mode='AUTO',
                    use_mesh_edges=True,
                    use_tspace=True,
                    use_default_take=True,
                    embed_textures=False,
                    use_custom_properties=False,
    
                    **kwargs
                    ):
    
        if object_types is None:
            object_types = {'EMPTY', 'CAMERA', 'LAMP', 'ARMATURE', 'MESH'}
    
        global_scale = global_matrix.median_scale
    
        global_matrix_inv = global_matrix.inverted()
        global_matrix_inv_trans = global_matrix_inv.transposed().to_3x3().to_4x4()  # For transforming mesh normals.
    
    
        # 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(
    
            (axis_up, axis_forward), global_matrix, global_scale,
            bake_space_transform, global_matrix_inv, global_matrix_inv_trans,
            context_objects, object_types, use_mesh_modifiers,
    
            mesh_smooth_type, use_mesh_edges, use_tspace, use_armature_deform_only,
            use_anim, use_anim_optimize, anim_optimize_precision, use_anim_action_all, use_default_take,
            use_metadata, 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,
            "use_anim": True,
            "use_anim_optimize": False,
            "use_anim_action_all": True,
            "batch_mode": 'OFF',
            "use_default_take": True,
    
            # 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