Wanted to use Unity Cloud Build but all my Blender models disappeared – solution

While Monster Couch slowly picks up velocity I’m still finishing up a side-gig for a 3D mobile game port. On my journey I was excited to use the cloud build to automate the process (and so that I could use the mac mini less :p). I happily set everything up but in he first cloud build something strange happened – all of my Blender models were missing.

I went on to search for the cause only to find out in the Support FAQ that:

“Unity Cloud Build supports “Exported 3d files”. We don’t support “Proprietary 3D files” at this time. A list of which types of files are which can be found here: HOWTO-importObject

That’s a real pain but the rationale is quite clear: on local machine it’s simple: the user is responsible for providing the licensed software to work with the proprietary formats. It gets slightly more complicated with the hosted build service. It’s doable with Blender since it’s open source and free – the script I’ll be showing you in a minute could be integrated into the service. I guess they will work it out sometime in the future but in the meantime…

Ouch, so how do I export over 150 models without destroying my prefabs?

Luckily Unity uses a python script to import .blend files and turns them internally into .fbx files. If you want to check out the script it can be found at *\Unity\Editor\Data\Tools\Unity-BlenderToFBX.py .

We can use that as a base for our batch exporter – we will be sure that nothing weird happens to our models. Here’s the script updated to accept command line arguments for input and output files. Originally it also depended on the Blender file being already opened so I’ve added a line to make sure it loads the contents of the .blender file.

Export .fbx from .blend

Search the project for every .blend model

We will also need a script that traverses our project recursively and runs the above script for every .blend model. We will also need to rename the .blend.meta files so that we don’t loose any references and finally clean up the .blend files since we no longer need them for our prefabs and scenes. Backup them somewhere before you proceed!

Sample usage

Summary

The script exports multiple files from Blender into fbx models. It searches the path recursively and creates .fbx files next to the .blend files, then it renames corresponding Unity *.blend.meta files to *.fbx.meta to keep editor references, then it deletes the .blend files (make your own backup somewhere outside of the project path).

This script uses custom version of Unity-BlenderToFBX.py that Unity uses internally for importing .blend files. It has been changed to allow passing arguments to it and to load the necessary .blend file. You can find the original file at [Unity path]\Editor\Data\Tools\Unity-BlenderToFBX.py

You need Blender to be installed on your machine. You need to specify the following command line arguments: Blender’s directory, path to args-Unity-BlenderToFBX.py, input directory (with blend files)

I have compiled it into a small github repo: blendToFbxExporterForUnity

Edit June 2016

Here is how you can use Blender’s command line:  https://www.blender.org/manual/render/workflows/command_line.html

Thanks for reading!

If you have found the post useful please visit us at fb.com/MonsterCouchGames and leave us a like :)

9 Comments

  1. Luis

    Hello,

    Excellent script, thanks for this!

    I had troubles running the script with OSx El Capitan, python 3.5, blender 2.77.

    I fixed this adding “shell=True” on line 43 of file blendToFbxExporter.py:

    status = subprocess.call(pathToBlender + ‘ –background –python ‘ + pathToBlenderToFBX + ‘ — “‘ + infile + ‘” “‘ + outfilename + ‘”‘, shell=True);

    Reply
  2. Roberto Gomes

    Hi there,

    I’m an illustrator, about programming… I only know a little of AS3, plus I’m new to Unity, so please bear with me.

    As far as I can understand, I should open Mac’s Terminal, go to the directory where Blender is installed and then paste this and hit return:
    blender –background –python “/Users/robertogomes/Google\ Drive/VR\ Dev/blendToFbxExporterForUnity-master/blendToFbxExporter.py” — “/Users/robertogomes/Google\ Drive/VR\ Dev/blendToFbxExporterForUnity-master/args-Unity-BlenderToFBX.py” “/Users/robertogomes/Google\ Drive/VR\ Dev/Unity/ExportFromBlenderTest/Assets”

    Right? Unfortunately this is what I get: “-bash: blender: command not found”

    I’m at it for several hours already, trying to understand what am I doing wrong, reading different posts about running blender from command lines but no success. I also need this to work so I can use Cloud Build because my Mac Mini can’t handle these builds anymore. Can you shed some light on this? Thanks. Keep up the good work.

    Reply
    1. Krzysiek (Post author)

      You could also create an alias for the blender executable:

      echo "alias blender=/Applications/Blender/blender.app/Contents/MacOS/blender" >> ~/.profile

      Make sure you have the correct path. You can do that by typing ‘ls’ in the terminal and checking if this lists “blender” there?
      On Mac this is not next to the app but kind of “inside” the app. This is because on mac apps are hierarchies of files and you need to find the right file inside.

      Reply
      1. Roberto Gomes

        Hi Krzysiek,

        Thanks for replying.

        Yes when I type “ls” it lists “blender” but I still get the same error…

        Roberto:MacOS robertogomes$ ls
        blender
        Roberto:MacOS robertogomes$ blender –background –python “/Users/robertogomes/Google\ Drive/VR\ Dev/blendToFbxExporterForUnity-master/blendToFbxExporter.py” — “/Users/robertogomes/Google\ Drive/VR\ Dev/blendToFbxExporterForUnity-master/args-Unity-BlenderToFBX.py” “/Users/robertogomes/Google\ Drive/VR\ Dev/Unity/ExportFromBlenderTest/Assets”
        -bash: blender: command not found
        Roberto:MacOS robertogomes$

        I must be doing something wrong… I got the correct paths by dragging them into the terminal and then I’ve put everything together on a note pad before pasting them back in the Terminal.

        But when I type “open blender” Blender opens…. grrrr driving me mad

        Reply
        1. Krzysiek (Post author)

          Hm, I don’t know Mac that well, but what happens when you do ‘cd blender’ in terminal there?

          Reply
          1. Roberto Gomes

            For example, I’m in MacOS directory (where “blender” is) and I type “cd blender” I get this:

            -bash: cd: blender: Not a directory

            So now basically I’m typing the complete path to blender plus –background –python “/Users/robertogomes/Desktop/blendToFbxExporter.py” — “/Users/robertogomes/Desktop/args-Unity-BlenderToFBX.py” “/Users/robertogomes/Google\ Drive/VR\ Dev/Unity/BlendToFBX/Assets/”

            and now it is trying to do something but ends up not converting the .blend files

        2. Roberto Gomes

          OK, I got something now. I still can’t create the Alias for Blender so I’m typping the complete path instead but now I get have this message:

          Read new prefs: /Users/robertogomes/Library/Application Support/Blender/2.76/config/userpref.blend
          found bundled python: /Applications/Blender 2.76/blender.app/Contents/MacOS/../Resources/2.76/python
          Processed files:
          0
          Skipped files (.fbx already exists at the locaton):
          0

          Blender quit
          Roberto:~ robertogomes$

          Blender didn’t process any files it seems… the .blend files are still in the Assets folder and there is no trace of .fbx files.

          Reply
          1. Krzysiek (Post author)

            Is it my blog converting the characters — (double dash) into — (single dash)?

            Make sure you have double dash in the command.

            Another thing – could possibly spaces in the path mess something up?

            Edit. Yeah it is blog converting characters not marked in code tags.
            --

  3. Andrew

    Hi!
    I make a *bat file:

    cd G:\Steam\steamapps\common\Blender
    blender
    –background
    –python
    “G:\blendToFbxExporterForUnity-master\blendToFbxExporter.py”
    –“G:\blendToFbxExporterForUnity-master\args-Unity-BlenderToFBX.py”
    “G:\blendToFbxExporterForUnity-master\Test”

    And I got this:

    AL lib: (EE) UpdateDeviceParams: Failed to set 44100hz, got 48000hz instead
    found bundled python: G:\Steam\steamapps\common\Blender\2.77\python

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *