Blender to Unity Python Export Script

Here is a small Python script that I wrote for Blender that automatically exports an FBX directly to the folder containing my Unity project. The script uses the name of the file to create an appropriate folder and file name in the Unity project and only exports two objects in the scene (Model and Rig), thus ignoring all the other junk I put in the scene file.

import bpy
import sys
import time
import mathutils

def export_current_scene_to_unity():
    namo = bpy.path.display_name_from_filepath( bpy.data.filepath )

    bpy.ops.object.mode_set(mode = 'OBJECT')
    bpy.ops.object.select_name(name='Rig')
    bpy.ops.object.select_name(name='Model',extend=True)
    bpy.ops.object.select_hierarchy(direction='CHILD',extend=True)

    bpy.ops.export_scene.fbx(filepath="/Users/carl/Unity/TheGame/Assets/Art/" + namo + "/" + namo + ".fbx", 
        check_existing=False,
        use_selection=True, 
        global_scale=1.0, 
        axis_forward='-Z', 
        axis_up='Y', 
        object_types={'ARMATURE', 'MESH', 'EMPTY'}, 
        use_mesh_modifiers=True, 
        mesh_smooth_type='OFF', 
        use_mesh_edges=False, 
        use_anim=True, 
        use_anim_action_all=True, 
        use_default_take=False,
        use_anim_optimize=True, 
        anim_optimize_precision=6.0, 
        path_mode='AUTO', 
        use_rotate_workaround=False, 
        xna_validate=False, 
        batch_mode='OFF', 
        use_batch_own_dir=True, 
        use_metadata=True)

class UnityExporter(bpy.types.Operator):
    bl_idname = "object.unity_export"
    bl_label = "Unity Export"

    def execute(self, context):
        export_current_scene_to_unity()
        print( "Export Finished!" )
        return {'FINISHED'}

def register():
    bpy.utils.register_class(UnityExporter)

def unregister():
    bpy.utils.unregister_class(UnityExporter)

if __name__ == "__main__":
    register()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Advertisements
%d bloggers like this: