Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Calculating position and rotation conversion from Blender to Unity

Discussion in 'Scripting' started by Drtsprtn, Apr 13, 2021.

  1. Drtsprtn

    Drtsprtn

    Joined:
    May 31, 2016
    Posts:
    10
    I have a custom asset database tool setup for working with an external CAD database. A big part of the tool runs within blender and creates reusable meshes that are then imported into Unity once and placed multiple times across different scenes by script.

    I'm running into an issue with converting those placements from within Blender's coordinate system to Unity's coordinate system.

    I understand there's some kind of automatic conversion taking place, like all rotations being shifted by -90 degrees on the x axis and I have gotten the rotations to work more or less correctly with this tweak and shifting some axes, but oddly enough there are still objects that somehow get rotated incorrectly.

    If I just put a bunch of those objects into a Blender scene and export that into Unity they seem to come out looking just fine with their default transforms, this however, is not an option because it doesn't fit my workflow.

    I guess my questions would be - how do I convert a XYZ Blender euler rotation to a ZXY Unity rotation for an .fbx imported object? Or, how do I convert a Blender quaternion to a Unity quaternion?

    What rules does Unity use to shift the object's coordinate system/position and rotation on import?
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Whenever I export something to Unity (from Max), I have a Maxscript I made years ago that rotates the X alignment either -90 or 90 (I can't remember) so it exports into Unity correctly, and then saves it under it's object name. I found messing with import settings in Unity (like scale) is less efficient than doing it inside the editor.
    I'd personally love to see Y-up and not Z-up unified for every 3D package, I don't know what the longterm ramifications are though, it's just been a longterm pain now as not everyone's export settings are the same. ;)
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    This is my process from blender that works for me:

    In Blender I pretend that the Y and Z axes are reversed. e.g. I treat Blender's green Y axis as "forward" and blue Z axis as "up".

    Then when I export the model I use these settings:
    upload_2021-4-13_11-49-38.png

    Works every time.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  5. Drtsprtn

    Drtsprtn

    Joined:
    May 31, 2016
    Posts:
    10
    Wasn't sure where to post it so I posted it to both forums.

    I need to convert raw positions and rotations, not the model itself.
     
  6. Drtsprtn

    Drtsprtn

    Joined:
    May 31, 2016
    Posts:
    10
    I have a position and rotation saved in the database, these are added from within Blender so they use Blender's coordinate system.

    If I negate the x axis and y axis and switch z and y the position is correct in Unity.
    I'm looking for a way to do this for rotations but I can't find the right solution.
     
  7. Drtsprtn

    Drtsprtn

    Joined:
    May 31, 2016
    Posts:
    10
    Ok so I think I managed to fix it myself.
    This gives me correct rotations within Unity.
    Code (CSharp):
    1.     public void SetRotation(PlacementData data) // data.rotation is a Blender WXYZ quaternion
    2.     {
    3.         Quaternion baseRot = Quaternion.Euler(-90f, 0, 0);
    4.         Quaternion rot = baseRot * new Quaternion(data.rotation.x, -data.rotation.y, -data.rotation.z, data.rotation.w);
    5.         transform.rotation = rot;
    6.     }