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 Converting positions and rotations from Blender within an internal tool

Discussion in 'Editor & General Support' 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. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    I don't think Unity does that step of things.

    Unity just runs the
    Unity-BlenderToFBX.py
    through Blender.

    That file is part of the Unity installation so you can go looking for it in your installation.

    This script makes Blender emit a temporary FBX, and Unity then imports that FBX.

    Look into what happens when Blender exports an FBX.
     
  3. Drtsprtn

    Drtsprtn

    Joined:
    May 31, 2016
    Posts:
    10
    To clarify, I'm importing FBX files that just happen to use Blender's coordinate system.

    I need to shift positions and rotations for placements from one coordinate system to the other, not the model itself.
     
  4. 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.
     
  5. 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.     }
     
    Kurt-Dekker likes this.