Generating .XNB Files From FBX Files Using CommandLine

Hey,
So I’m currently using WPF and Monogame together to make a sort of rendering software for myself. What I’m trying to do right now is make FBX models loadable directly through my software. I have MGCB.exe imported along with all the .dlls, and it says that the build has been started. Interestingly, I can’t find outputted .xnb files anywhere. I’ve scoured my whole harddrive to no avail.

Here’s a picture of the console when the program is run:

Here’s the code handling all the converting:

//A correct file was found and the path is valid.
            if (_filePath != null)
            {
                //Go to current domain and then go to premade directory with pipeline.
                var fn = AppDomain.CurrentDomain.BaseDirectory + "\\FBX Converter\\MGCB.exe";

                var process = new Process();
                process.StartInfo.FileName = $"\"{fn}\"";
                process.StartInfo.WorkingDirectory = "D:\\Downloads\\bigboss";
                process.StartInfo.Arguments =
                    "/outputDir:bin /intermediateDir:obj /platform:Windows /config: /profile:Reach /compress:False " +
                    "/importer:FbxImporter /processor:ModelProcessor /processorParam:ColorKeyColor=0,0,0,0 " +
                    "/processorParam:ColorKeyEnabled=True /processorParam:DefaultEffect=BasicEffect " +
                    "/processorParam:GenerateMipmaps=True /processorParam:GenerateTangentFrames=False " +
                    "/processorParam:PremultiplyTextureAlpha=True /processorParam:PremultiplyVertexColors=True " +
                    "/processorParam:ResizeTexturesToPowerOfTwo=False /processorParam:RotationX=0 " +
                    "/processorParam:RotationY=0 /processorParam:RotationZ=0 /processorParam:Scale=1 " +
                    "/processorParam:SwapWindingOrder=False " +
                    "/processorParam:TextureFormat=Compressed /build:VertexColorTree.fbx";

                process.Start();
            }

And here’s the entire method if you happen to need it (some code is irrelevant to this);

private void ButtonOpen_OnClick(object sender, RoutedEventArgs e)
        {
            string _filePath = null;

            // Create OpenFileDialog 
            Microsoft.Win32.OpenFileDialog _dlg = new Microsoft.Win32.OpenFileDialog();

            // Set filter for file extension and default file extension 
            _dlg.DefaultExt = ".fbx";
            _dlg.Filter = "FBX Files (*.fbx)|*.fbx"; //FORMAT: "Description Shown In Editor|.AllowedExtension"


            // Display OpenFileDialog by calling ShowDialog method 
            Nullable<bool> _result = _dlg.ShowDialog();


            // Get the selected file name and display in a TextBox 
            if (_result.HasValue && _result.Value)
            {
                // Get document path. 
                _filePath = _dlg.FileName;
            }

            //A correct file was found and the path is valid.
            if (_filePath != null)
            {
                //Go to current domain and then go to premade directory with pipeline.
                var fn = AppDomain.CurrentDomain.BaseDirectory + "\\FBX Converter\\MGCB.exe";

                var process = new Process();
                process.StartInfo.FileName = $"\"{fn}\"";
                process.StartInfo.WorkingDirectory = "D:\\Downloads\\bigboss";
                process.StartInfo.Arguments =
                    "/outputDir:bin /intermediateDir:obj /platform:Windows /config: /profile:Reach /compress:False " +
                    "/importer:FbxImporter /processor:ModelProcessor /processorParam:ColorKeyColor=0,0,0,0 " +
                    "/processorParam:ColorKeyEnabled=True /processorParam:DefaultEffect=BasicEffect " +
                    "/processorParam:GenerateMipmaps=True /processorParam:GenerateTangentFrames=False " +
                    "/processorParam:PremultiplyTextureAlpha=True /processorParam:PremultiplyVertexColors=True " +
                    "/processorParam:ResizeTexturesToPowerOfTwo=False /processorParam:RotationX=0 " +
                    "/processorParam:RotationY=0 /processorParam:RotationZ=0 /processorParam:Scale=1 " +
                    "/processorParam:SwapWindingOrder=False " +
                    "/processorParam:TextureFormat=Compressed /build:VertexColorTree.fbx";

                process.Start();
            }
        }

DLLs and all that jazz:

Why isn’t the builder exporting an .xnb file as it should? I’m trying to build a file called VertexColorTree.fbx if that helps.

You don’t want to change the working directory here. Instead you should pass the absolute path to the model. Relative paths like the outputDir are resolved by combining them with the working directory, so I’d expect the result to be in D:\Downloads\bigboss\bin, which is probably not what you want.

Unfortunately that didn’t work. I still can’t find an additional .xnb file :confused:

Please try to unset the working directory and place the “VertexColorTree.fbx” file in the same directory where the MGCB.exe is and see if it generates the XNB inside the /bin folder (will be created in the same place of the MGCB.exe).

The console output should also show you if a file was successfully built.

1 Like

I tried to do what you described.

Modified code:

//A correct file was found and the path is valid.
            if (_filePath != null)
            {
                //Go to current domain and then go to premade directory with pipeline.
                var fn = AppDomain.CurrentDomain.BaseDirectory + "\\FBX Converter\\MGCB.exe";

                var process = new Process();
                process.StartInfo.FileName = $"\"{fn}\"";
                process.StartInfo.Arguments =
                    "/outputDir:bin /intermediateDir:obj /platform:Windows /config: /profile:Reach /compress:False " +
                    "/importer:FbxImporter /processor:ModelProcessor /processorParam:ColorKeyColor=0,0,0,0 " +
                    "/processorParam:ColorKeyEnabled=True /processorParam:DefaultEffect=BasicEffect " +
                    "/processorParam:GenerateMipmaps=True /processorParam:GenerateTangentFrames=False " +
                    "/processorParam:PremultiplyTextureAlpha=True /processorParam:PremultiplyVertexColors=True " +
                    "/processorParam:ResizeTexturesToPowerOfTwo=False /processorParam:RotationX=0 " +
                    "/processorParam:RotationY=0 /processorParam:RotationZ=0 /processorParam:Scale=1 " +
                    "/processorParam:SwapWindingOrder=False " +
                    "/processorParam:TextureFormat=Compressed /build:VertexColorTree.fbx";

                process.Start();
            }

It does the usual ‘build started’ shenanigans but doesn’t output a .xnb file. No bin folder was created either.

Here’s the source, I’m sure it’ll be helpful.

I may found your problem by trying to load a custom FBX file with the MGCB Tool.

I had two version of the same FBX file; one gets loaded and the other one failed with the exception

Assimp.AssimpException: Error importing file: FBX-Parser (TOK_DATA, line 319, col 7)
unexpected token, expected TOK_KEY
Assimp.AssimpContext.ImportFile(String file, PostProcessSteps postProcessFlags)
Microsoft.Xna.Framework.Content.Pipeline.OpenAssetImporter.Import(String filename, ContentImporterContext context) Microsoft.Xna.Framework.Content.Pipeline.ContentImporter`1.Microsoft.Xna.Framework.Content.Pipeline.IContentImporter.Import(String filename, ContentImporterContext context) MonoGame.Framework.Content.Pipeline.Builder.PipelineManager.ProcessContent(PipelineBuildEvent pipelineEvent)


MonoGame uses assimp for model import and you can test your model by downloading Open3DMod, which is the official assimp model viewer.

In my case it told me that the model had an outdated format. Then I simply converted it by using Paint 3D (just opened an exported the model as FBX).

After that it was possible for me to load the model inside Open3DMod and as guessed it was possible to compile the new FBX to XNB using the MGCB Tool.


Just give it a shot. This is maybe your problem too.

Unfortunately it seems not :frowning: The viewer opened my .fbx model with no problem. At this point I’m a bit desperate. Do you mind pasting the code you use to load a custom FBX file along with how the directories are set up? If it works and generates an XNB, maybe I can replicate it in some way.

Yes, of course. It’s quite simple.
I personally prefer to work with content respond files like this:

  • 1: Create a .txt file and call it Content.txt
  • 2: Write in this file:

/outputDir:output
/intermediateDir:obj
/platform:Windows
/config:
/profile:Reach
/compress:False

#begin input/VertexColorTree.fbx
/importer:FbxImporter
/processor:ModelProcessor
/build:input/VertexColorTree.fbx

  • 3: Create a .txt file and call it buildContent
  • 4: Write in this file:

MGCB.exe /@:Content.txt >CON 2>errorLog.txt

echo.

pause

  • 5: Save this file as a .bat file (it should be a windows batch file)
  • 6: Put the MGCB.exe and all needed libraries for building content in the folder where the newly created files are
  • 7: Create a new folder and call it input
  • 8: Put your VertexColorTree.fbx file in this folder
  • 9: Double Click / Run buildContent.bat
  • 10: See the console output.

If all went successful then you will have your new XNB file here: output/input/VertexColorTree.xnb

I recommend reading the Official Documentation about the MonoGame Content Builder, when working with it, as it is very helpful.

1 Like

Thank you so much! Using this technique I was able to find out that I was missing a .dll necessary to build .fbx files. It was the CppNet.dll :smiley:

If anyone wants info on how I did it, Here’s a link to the file containing the method that does all the conversion stuff. The method is called ‘ButtonOpen_OnClick’.

1 Like