allenai / ai2thor

An open-source platform for Visual AI.
http://ai2thor.allenai.org
Apache License 2.0
1.09k stars 210 forks source link

How to save scene into .FBX or .OBJ files in local-built Unity3D? #1138

Closed YandanYang closed 1 year ago

YandanYang commented 1 year ago

Hi. I am trying to build a local unity3D with invoke local_build. I want to save export/save scene into .FBX/.obj files, so I add the following code in the file named ProceduralTools.cs:

string path = "scene.obj";
 // Save the active scene to a temporary file
string tempScenePath = "~/workspace/ai2thor/unity/Assets/TempScene.unity";
EditorSceneManager.SaveScene(EditorSceneManager.GetActiveScene(), tempScenePath);
 // Export the temporary scene to an obj file
AssetDatabase.ExportPackage(tempScenePath, path, ExportPackageOptions.IncludeDependencies | ExportPackageOptions.Recurse);

but when I add using UnityEditor, the building process will fail. Some online materials say using UnityEditor can only be used when we run the code in the Unity Editor but not the executable compiled version. Is this right? And is there any other way to export the scene into .FBX or .OBJ ? Thanks.

winthos commented 1 year ago

This is expected behavior. Certain Unity functions are Editor Only functions that only work from the Unity application while in edit mode, and cannot be executed at runtime from a build, which is why this is likely failing.

If you want to export a scene as an fbx, you will have to do something like use the Unity Editor itself to export a scene after selecting game objects. Take a look at the [Unity Documentation for this here.](https://docs.unity3d.com/Packages/com.unity.formats.fbx@2.0/manual/exporting.html#:~:text=Use%20Export%20To%20FBX%20(menu,to%20a%20single%20FBX%20file.)

YandanYang commented 1 year ago

Ok. Thanks for this.