1
$\begingroup$

I want to export fbx for unity based on manual way in this video I know there is an option as !!EXPERIMENTAL!!, I don't want to use it because sometimes it doesn't work.

enter image description here

Manual Way

manual way works fine I just want to do manual way by code. because I have to repeat these steps on every 3d models!!!

suppose we have a fish model and we want to export it to untiy

enter image description here

1.Manually rotate the object by -90 degree around X-axis (if you don't see this panel - press N key)

enter image description here

2.press Ctrl + A and Apply => Rotation

enter image description here

3.and set X rotation to 90 degrees (do not apply this time)

enter image description here

$\endgroup$
1
$\begingroup$

you can make steps automatic

1.rotate the object by -90 degree around X-axis

obj.rotation_euler = (math.radians(-90),0,0)

2.press Ctrl + A and Apply => Rotation

bpy.ops.object.transform_apply(location=False, rotation=True, scale=False)

3.and set X rotation to 90 degrees (do not apply this time)

obj.rotation_euler = (math.radians(-90),0,0)

also, you can export it by unit scale

bl_info = {
    "name": "Unity Fix",
    "author": "Kamali",
    "version": (1, 0),
    "blender": (2, 80, 0),
    "location": "File > Export > Export For Unity",
    "description": "Fix Pivot ,Location , Rotation , Scale ",
    "warning": "",
    "doc_url": "",
    "category": "",
}


import bpy
import math

def write_some_data(context, filepath):
    bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS', center='MEDIAN')
    obj= context.object
    
    # reset location to the center
    obj.location = (0,0,0)
    
    # reset everything to make sure it won't change
    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    # set rotation to -90
    
    obj.rotation_euler = (math.radians(-90),0,0)
    # reset everything
    bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
    
    # rotate 90 degree along x axis
    obj.rotation_euler = (math.radians(90),0,0)
    
    
    # apply unit scaling
    bpy.ops.export_scene.fbx(filepath=filepath, use_selection=True,apply_scale_options='FBX_SCALE_UNITS',apply_unit_scale=True)
    return {'FINISHED'}

    


from bpy_extras.io_utils import ExportHelper
from bpy.props import StringProperty, BoolProperty, EnumProperty
from bpy.types import Operator


class ExportSomeData(Operator, ExportHelper):
    """Export For Unity"""
    bl_idname = "export_test.some_data"  
    bl_label = "Export For Unity"


    filename_ext = ".fbx"

    filter_glob: StringProperty(
        default="*.fbx",
        options={'HIDDEN'},
        maxlen=255, 
    )


    def execute(self, context):
        return write_some_data(context, self.filepath)


# Only needed if you want to add into a dynamic menu
def menu_func_export(self, context):
    self.layout.operator(ExportSomeData.bl_idname, text="Export For Unity")


def register():
    bpy.utils.register_class(ExportSomeData)
    bpy.types.TOPBAR_MT_file_export.append(menu_func_export)


def unregister():
    bpy.utils.unregister_class(ExportSomeData)
    bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)


if __name__ == "__main__":
    register()

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.