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

How can I change the .blend->Unity FBX importer settings?

Discussion in 'Asset Importing & Exporting' started by MattRix, Sep 10, 2014.

  1. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    I need to change some of the Blender FBX export settings (setting -Z forward, and "Apply Transform"). This works fine when exporting a .fbx, but when I use Unity's .blend importer (which AFAIK uses Blender's FBX exporter behind the scenes), it uses the default settings.

    So here's my question: how can I change what the FBX export settings Unity uses when importing a .blend file?
     
  2. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    Ok well I didn't find much help on this... so instead I decided to learn Python and rewrite the export script so it actually works properly :D

    The script I wrote now actually gives better results than exporting an FBX from Blender, because now you get proper transforms AND the new coordinate space baked into the mesh (yes, that means no more objects getting imported with weird default transform values!). Now any Y value you have in Blender will be the same Z value in Unity, and vice versa. Simple.

    This works because I discovered that Unity's importer was still using the old ASCII FBX exporter behind the scenes. I switched it to the new binary FBX exporter, which gives it the option to bake transforms. Then I just had to fiddle around to figure out the right values for the global_matrix, axis_up, and axis_forward, and presto, it worked! :)

    If you're on Windows, you can find the export script to replace in `Program Files\Unity\Editor\Data\Tools\Unity-BlenderToFBX.py`, and if you're on a Mac, go to the Unity app, right click, "Show Package Contents" and then find `Content/Tools/Unity-BlenderToFBX.py`

    Here comes the code! I can't guarantee this will work for all kinds of animations + rigs, etc, but it works great for everything I need it for. I replaced the entire contents of that .py file with this:

    _____OLD CODE REMOVED_____

    Here's the newest, actually working version. It no longer uses the binary exporter, so it should be compatible with slightly older versions of Blender than 2.71

    https://gist.github.com/MattRix/122f258ba6eaa23dd9c5

    Enjoy! If anyone else finds this useful, let me know :)
     
    Last edited: Sep 12, 2014
  3. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    nice thx i will try this. i always hate those default animation files
     
  4. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    Yeah if you tweak those arguments in that kwargs dictionary, you can turn on and off what actually gets imported (ex animations). Also worth noting is the "object_types={}" thing, because you can add different values to that to change what gets imported (ex armature, lights, camera, etc). For my purposes, I only needed empties and meshes.
     
  5. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    which blender version do you use?
     
  6. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    2.71, the newest. If you have a much older version of blender, it might not work properly, because they changed the blender python naming conventions a bit or something.
     
  7. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    Just realized that the FBX binary exporter I'm using was only introduced to Blender in 2.71 (see http://code.blender.org/index.php/2014/06/supporting-game-developers-with-blender-2-71/) - so you'll need Blender 2.71 to use this script.

    Also I've discovered that Blender's FBX exporter is pretty buggy, which is causing a bunch of issues. Specifically, it doesn't bake the global matrix into any meshes *that are children*, and it doesn't bake it into empties.

    So this means that if you use my script, if you have meshes that are children (not at the root of the scene), they won't have the global transform baked into the mesh. This isn't the end of the world (everything will be in the right places still), but for those objects, it basically puts you back to the current state of the Unity Blender importer (ie. it "works", but it's not really correct).
     
  8. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    967
    You can also take a look at this that was mentioned here.
     
  9. TRuoss

    TRuoss

    Joined:
    Dec 5, 2012
    Posts:
    85
    I use 2.70. I will try to test it with 2.71. I did not update cause of import problems.

    But it´s good to know that i can modify the unity fbx importer
     
  10. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    Wow, this is great! Pretty much all the same things I've been trying to do :) Very helpful, I think I should be able to make that work.
     
  11. MattRix

    MattRix

    Joined:
    Aug 23, 2011
    Posts:
    121
    Ok I took those scripts and fixed it! This is back to using the ASCII exporter (the binary one is too buggy), so this should work with earlier versions of Blender fine.

    The only change I've made is defaults to having Blender Y equal to Z in Unity, instead of Blender Y = -Z, which is what that thread was using. You can switch it to that matrix by looking for the "matPatch" variable and uncommenting the other settings.

    Gist is here: https://gist.github.com/MattRix/122f258ba6eaa23dd9c5

    Also worth noting that with that script you can put a custom property on any Blender object called UNITY_EXPORT and set it to 0 and it won't be imported. Very handy! :)
     
    Last edited: Sep 12, 2014