Search Unity

Question How does Unity import .blend files? [C++ source code]

Discussion in 'Asset Importing & Exporting' started by TheNullReference, Sep 29, 2022.

  1. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    268
    I'm trying to make a custom importer for Blender so that I can import collections, materials, textures etc.

    I have already written the python code and managed to overwrite .blend default behavior with a Scripted Importer, as well as running a python process inside of Unity.

    The problem is, I still want to use Unity's method of importing a blend file as an FBX, so I can use all of Unity's nice FBX import features (generate lightmap uvs, animation clips etc) as opposed to importing a blend file directly.

    I just don't really know how to do this, or how Unity does it? My guess is....

    1. Run a blender process and export the fbx back into the project.
    2. Have the script 'waiting' for this FBX from your main blend importer.
    3. AssetPreProccess the FBX model importer with the model importer settings for the blend importer.
    4. Wait for the FBX to finishing importing.
    5. Add the FBX as your main asset to the blend file.
    6. Add in the materials / textures as needed.

    It feels a little ugly, is there a way to import and fbx into unity in a more controlled manor? where it's not going to create and asset?

    Any help or ideas would be greatly appreciated.
     
  2. bastien_humeau

    bastien_humeau

    Unity Technologies

    Joined:
    Jun 14, 2017
    Posts:
    191
    Hi there,

    What we do when importing a .blend file is:
    1- Run a blender process in the background that exports the .blend file into a temporary .fbx (I think somewhere in the Temp folder)
    2- Open the FBX SDK and load this temporary fbx file to be processed instead of the actual imported file.
    3- process with the import as if it was a regular fbx file.

    The issue for you is that we don't expose any of this mechanism to c#, which makes re-using the FBXImporter behaviour pretty much impossible from a ScriptedImporter...

    What I've seen being done in the past though, was to create your own export script from blender, that would output both an .fbx file and a .json file.
    In Unity, let the fbx file be imported as usual, and create a ScriptedImporter for your json file.
    The output of the json file import can then be a copy of the ModelImporter result but augmented with whatever data you have in this json file that Unity would not understand from the fbx perspective.

    Edit: Ideally, don't rely on .blend files format in Unity. Because the import only works when blender is installed on the machine, and the result depends on which blender version is installed, it makes for very unstable imports and project sharing, often breaks cloud builds or other CI integrations, and globally makes using your project and plugins way harder than it could.