Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Showcase How to write a simple package that extends the FBX Exporter's capabilites

Discussion in 'Scripting' started by dan-levi, Dec 2, 2023.

  1. dan-levi

    dan-levi

    Joined:
    Apr 11, 2019
    Posts:
    1
    Hi,

    I was searching for a way to mass export GameObjects from my scene to FBX files. Basically i had a parent GameObject with 250 child GameObjects and did not want to export one by one.

    Note that the code i am sharing relies on the FBX Exporter package that you can add from the Package Manager.

    1. First i made a folder in the Packages folder in my project folder.
    2. Then i made a com.danlevi.batchexportfbx folder
    3. Inside that i made a Editor folder.
    4. In the Editor folder i made a BatchExportFBS.cs file and wrote a simple C# script like this:

    Code (CSharp):
    1.  
    2. using System.IO;
    3. using UnityEditor;
    4. using UnityEditor.Formats.Fbx.Exporter;
    5. using UnityEngine;
    6.  
    7. public sealed class BatchExportFBX
    8. {
    9.     static readonly string folderPath = "H:\\Dev\\UnityProjects\\XXXXX\\Assets\\FBXEXPORT";
    10.  
    11.     [MenuItem("GameObject/Batch export selected to FBX...", false, 30)]
    12.     internal static void OnContextItem(MenuCommand command)
    13.     {
    14.         GameObject selectedGameObject = Selection.activeGameObject;
    15.         if(selectedGameObject != null)
    16.         {
    17.             int childCount = selectedGameObject.transform.childCount;
    18.  
    19.             if(childCount > 0)
    20.             {
    21.                 for (int i = 0; i < childCount; i++)
    22.                 {
    23.                     GameObject obj = selectedGameObject.transform.GetChild(i).gameObject;
    24.                     Debug.Log("Exporting: " + obj.name);
    25.                     string objectName = selectedGameObject.transform.GetChild(i).gameObject.name;
    26.                     ModelExporter.ExportObject(Path.Combine(folderPath, objectName + ".fbx"), obj);
    27.                 }
    28.             }
    29.         }
    30.     }
    31. }
    Please note that you need to change the path of the folderPath to a folder on your computer for this to work.


    How it works is that it exports the gameobjects that is a direct child of the gameobject selected in the hierarchy to the specified folder in the code.


    I could have done much more with this but i needed something quick and easy.

    Hope you get use of this.

    Cheers!
     
    Nad_B likes this.