Importing FBX file by script

Sorry if this is a dumb question, I’m still a newbie when it comes to Unity. I’m working on a project in which I would like to import a new asset (an FBX file not previously imported into the project) by using a script only, i.e. I do not want to manually choose the file or drag and drop the file into Unity. Can this be done? If so, does anyone have any example script that does this or something similar that I can look at? Thanks in advance for any help!

If it hasn’t been previously imported, then you would have to read it at runtime, and what about the setup? scale, textures, etc? You would have to do a search for loading fbx files at runtime in unity and see what comes up, I guess.

Optimally, I would like to do it during runtime, but this has proven quite difficult, so I’ve scrapped that idea. I was probably a bit unclear in this question. In the Unity Editor, instead of manually selecting an asset for importing (or drag-and-dropping it into the project), I would like to run a script that imports the asset at a specified location. I plan to run this script at the launch of the Unity Editor by using the command line arguments -batchMode and -executeMethod.

I have tried the following short example from the AssetDatabase API:

using UnityEngine;
using UnityEditor;
public class TestScript {
   [MenuItem ("AssetDatabase/ImportExample")]
   static void ImportExample ()
   {
     AssetDatabase.ImportAsset("Assets/TestingAssets/testmodel.fbx", ImportAssetOptions.Default);
   }
}

…and then clicking this new menu item, but unless the asset is already imported, it does not seem to do anything (however I get no errors). If the asset is already imported, it seems to re-import the asset.

I realize Unity automatically imports any files placed in the Asset folder during startup, but since I want to use this newly imported asset and place it in a scene by the -executeMethod startup script, I’m not sure if the that automatic import interferes with that. If the automatic import is 100% finished when the -executeMethod starting script is run, then that would work. However, I would prefer not having to place the new asset to be imported in the asset folder of the project prior to Unity Editor launch.

1 Like

If the object you want is not too crazy, you could try exporting it in wavefront .OBJ file format (see below for sample Blender3D cube exported in .OBJ format) and write your own parser in C# to trivially build up the geometry for it. The format could almost not be easier for text-parsing into geometry fabrication at runtime.

Blender v2.71 (sub 0) OBJ File: ‘cube2x2x2.blend’

www.blender.org

mtllib cube2x2x2.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
vt 0.666467 0.333133
vt 0.333533 0.333134
vt 0.333533 0.000200
vt 0.666467 0.000200
vt 0.333134 0.333133
vt 0.000200 0.333134
vt 0.000200 0.000200
vt 0.333133 0.000200
vt 0.999800 0.000200
vt 0.999800 0.333133
vt 0.666866 0.333133
vt 0.666867 0.000200
vt 0.666467 0.333533
vt 0.666467 0.666467
vt 0.333533 0.666467
vt 0.333533 0.333533
vt 0.000200 0.999800
vt 0.000200 0.666866
vt 0.333134 0.666866
vt 0.333134 0.999800
vt 0.333134 0.333533
vt 0.333134 0.666467
vt 0.000200 0.666467
vt 0.000200 0.333533
usemtl Material
s off
f 1/1 2/2 3/3 4/4
f 5/5 8/6 7/7 6/8
f 1/9 5/10 6/11 2/12
f 2/13 6/14 7/15 3/16
f 3/17 7/18 8/19 4/20
f 5/21 1/22 4/23 8/24

Unfortunately it is rather heavy files (CAD models). Dynamic OBJ imports does seems easy compared to dynamic FBX imports, but if I can get this script working I shouldn’t need to bother with dynamic/runtime imports at all (which would be nice!). Thanks for the suggestion though!

I experimented a bit with Unity’s automatic import of new files in the Asset folder, and it seems this import is indeed completed before any -batchMode -executeMethod script is run. So in worst case, I could create a Windows script which copies the FBX file in question to the Asset folder before running Unity in batch mode. Then I would need some way to get the name of the newly imported FBX asset so I can actually place it in a scene before building, but that’s another issue.

You can keep it in the resources folder and then use Resources.Load I think.