Skip to content

Instantly share code, notes, and snippets.

@richy486
Created January 10, 2017 18:03
Show Gist options
  • Save richy486/57edb832752e1298adeaa4a24eaf66f7 to your computer and use it in GitHub Desktop.
Save richy486/57edb832752e1298adeaa4a24eaf66f7 to your computer and use it in GitHub Desktop.
Fix for Blender .blend files being loaded in Unity3D.
Fix for Blender .blend files being loaded in Unity3D.
Fix script by Mirgar (also attached)
https://forum.unity3d.com/threads/blender-unity-rotation-fix.181870/
Put in in blender.app/Contents/Resources/[version]/scripts/addons/
In Blender press ctrl-opt-U to get to Blender User Preferences
The plugin is under Categories->Object, it's called 'Unity Tools'
Click the checkmark to enable it
The add on should be available in the Misc 'shelf' in 3D mode, that is the bar on the left under the Misc tab when in 3D mode.
The forum says it's under Tools but I could only find it under Misc
Use the Simple Rotation Fix and save the .blend file which will be imported into Unity.
import bpy
bl_info = {
"name": "Unity Tools",
"author": "Karol \"Mirgar\" Głażewski",
"version": (1, 0, 2),
"blender": (2, 6, 5),
"location": "3D View > Tool Shelf > Unity Tools",
"description": "Tools to ease workflow with Unity Engine",
"warning": "",
"wiki_url": "",
"tracker_url": "",
"category": "Object"}
# fixes rotation on X axis, +X is -X in Unity compared to Blender
class UnityRotationFixerX(bpy.types.Operator):
bl_description = "Fixes rotation of object so it will not \"lay on its face\" in Unity, +X axis is -X compared to Unity"
bl_label = "Simple rotation fix"
bl_idname = "object.unity_rotation_fix_x"
bl_options = {'REGISTER', 'UNDO'}
def FixRotationForUnity3D(self):
bpy.ops.object.transform_apply(rotation = True)
bpy.ops.transform.rotate(value = -1.5708, axis = (1, 0, 0), constraint_axis = (True, False, False), constraint_orientation = 'GLOBAL')
bpy.ops.object.transform_apply(rotation = True)
bpy.ops.transform.rotate(value = 1.5708, axis = (1, 0, 0), constraint_axis = (True, False, False), constraint_orientation = 'GLOBAL')
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT' and context.area.type == 'VIEW_3D'
def execute(self, context):
self.FixRotationForUnity3D()
return {'FINISHED'}
# fixes rotation on X and Z axis, front is now +Y
class UnityRotationFixerXZ(bpy.types.Operator):
bl_description = "Fixes rotation of object, +Y is now front"
bl_label = "Full rotation fix"
bl_idname = "object.unity_rotation_fix_xz"
bl_options = {'REGISTER', 'UNDO'}
def FixRotationForUnity3D(self):
bpy.ops.object.transform_apply(rotation = True)
bpy.ops.transform.rotate(value = -1.5708, axis = (1, 0, 0), constraint_axis = (True, False, False), constraint_orientation = 'GLOBAL')
bpy.ops.transform.rotate(value = -3.1416, axis = (0, 1, 0), constraint_axis = (False, True, False), constraint_orientation = 'GLOBAL')
bpy.ops.object.transform_apply(rotation = True)
bpy.ops.transform.rotate(value = 1.5708, axis = (1, 0, 0), constraint_axis = (True, False, False), constraint_orientation = 'GLOBAL')
bpy.ops.transform.rotate(value = 3.1416, axis = (0, 0, 1), constraint_axis = (False, False, True), constraint_orientation = 'GLOBAL')
@classmethod
def poll(cls, context):
return context.mode == 'OBJECT' and context.area.type == 'VIEW_3D'
def execute(self, context):
self.FixRotationForUnity3D()
return {'FINISHED'}
class UnityPanel(bpy.types.Panel):
bl_idname = "OBJECT_PT_unity_tools"
bl_label = "Unity Tools"
bl_space_type = 'VIEW_3D'
bl_region_type = 'TOOLS'
bl_context = "objectmode"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
col = layout.column(align=True)
col.label(text="Rotation:")
col.operator("object.unity_rotation_fix_x")
col.operator("object.unity_rotation_fix_xz")
#registers
def register():
bpy.utils.register_class(UnityRotationFixerX)
bpy.utils.register_class(UnityRotationFixerXZ)
bpy.utils.register_class(UnityPanel)
def unregister():
bpy.utils.unregister_class(UnityRotationFixerX)
bpy.utils.unregister_class(UnityRotationFixerXZ)
bpy.utils.unregister_class(UnityPanel)
if __name__ == "__main__":
register()
@ReVDiZ
Copy link

ReVDiZ commented Jul 26, 2020

Here is my fix for Blender 2.83 :) Doing EXACTLY the same thing, just upgrade some version issues...

import bpy

bl_info = {
 "name": "Unity Tools",
 "author": "Karol \"Mirgar\" Głażewski",
 "version": (1, 0, 2),
 "blender": (2, 80, 3),
 "location": "3D View > Tool Shelf > Unity Tools",
 "description": "Tools to ease workflow with Unity Engine",
 "warning": "",
 "wiki_url": "",
 "tracker_url": "",
 "category": "Object"}

# fixes rotation on X axis, +X is -X in Unity compared to Blender
class UnityRotationFixerX(bpy.types.Operator):

    bl_description = "Fixes rotation of object so it will not \"lay on its face\" in Unity, +X axis is -X compared to Unity"
    bl_label = "Simple rotation fix"
    bl_idname = "object.unity_rotation_fix_x"
    bl_options = {'REGISTER', 'UNDO'}
    
    def FixRotationForUnity3D(self):
        bpy.ops.object.transform_apply(rotation = True)
        bpy.ops.transform.rotate(value = -1.5708, orient_axis = 'X', constraint_axis = (True, False, False), orient_type = 'GLOBAL')
        
        bpy.ops.object.transform_apply(rotation = True)
        bpy.ops.transform.rotate(value = 1.5708, orient_axis = 'X', constraint_axis = (True, False, False), orient_type = 'GLOBAL')
        
    @classmethod
    def poll(cls, context):
        return context.mode == 'OBJECT' and context.area.type == 'VIEW_3D'
        
    def execute(self, context):
        self.FixRotationForUnity3D()
        return {'FINISHED'}
        
# fixes rotation on X axis, +X is -X in Unity compared to Blender
class UnityRotationFixerXInverse(bpy.types.Operator):
    
    bl_description = "Fixes rotation of object so it will not \"lay on its face\" in Unity, +X axis is -X compared to Unity"
    bl_label = "Simple rotation fix inverse"
    bl_idname = "object.unity_rotation_fix_x_inverse"
    bl_options = {'REGISTER', 'UNDO'}
    
    
    def FixRotationForUnity3D(self):
        bpy.ops.object.transform_apply(rotation = True)
        bpy.ops.transform.rotate(value = 1.5708, orient_axis = 'X', constraint_axis = (True, False, False), orient_type = 'GLOBAL')
        
        bpy.ops.object.transform_apply(rotation = True)
        bpy.ops.transform.rotate(value = -1.5708, orient_axis = 'X', constraint_axis = (True, False, False), orient_type = 'GLOBAL')
    
    @classmethod
    def poll(cls, context):
        return context.mode == 'OBJECT' and context.area.type == 'VIEW_3D'
        
    def execute(self, context):
        self.FixRotationForUnity3D()
        return {'FINISHED'}
        
# fixes rotation on X and Z axis, front is now +Y
class UnityRotationFixerXZ(bpy.types.Operator):
    
    bl_description = "Fixes rotation of object, +Y is now front"
    bl_label = "Full rotation fix"
    bl_idname = "object.unity_rotation_fix_xz"
    bl_options = {'REGISTER', 'UNDO'}

    def FixRotationForUnity3D(self):
        bpy.ops.object.transform_apply(rotation = True)

        bpy.ops.transform.rotate(value = -1.5708, orient_axis = 'X', constraint_axis = (True, False, False), orient_type = 'GLOBAL')
        bpy.ops.transform.rotate(value = -3.1416, orient_axis = 'Y', constraint_axis = (False, True, False), orient_type = 'GLOBAL')

        bpy.ops.object.transform_apply(rotation = True)

        bpy.ops.transform.rotate(value = 1.5708, orient_axis = 'X', constraint_axis = (True, False, False), orient_type = 'GLOBAL')
        bpy.ops.transform.rotate(value = 3.1416, orient_axis = 'Y', constraint_axis = (False, True, False), orient_type = 'GLOBAL')

    @classmethod
    def poll(cls, context):
        return context.mode == 'OBJECT' and context.area.type == 'VIEW_3D'
    
    def execute(self, context):
        self.FixRotationForUnity3D()
        return {'FINISHED'}
    
class UnityPanel(bpy.types.Panel):
    
    bl_idname = "OBJECT_PT_unity_tools"
    bl_label = "Unity Tools"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'TOOLS'
    bl_context = "objectmode"
    bl_options = {'DEFAULT_CLOSED'}
    
    def draw(self, context):
    
        layout = self.layout
        
        col = layout.column(align=True)
        col.label(text="Rotation:")
        col.operator("object.unity_rotation_fix_x")
        col.operator("object.unity_rotation_fix_x_inverse")
        col.operator("object.unity_rotation_fix_xz")
    
#registers

classes = (
    UnityRotationFixerX,
    UnityRotationFixerXInverse,
    UnityRotationFixerXZ,
    UnityPanel
)
    
register, unregister = bpy.utils.register_classes_factory(classes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment