Editor menu glue for exporting VMDL models to OBJ. Adds an EditorApp Tools entry that opens a VMDL file picker and resolves the file to a project asset, plus an Asset Browser context menu option that exports each selected .vmdl. Actual export is delegated to VmdlObjExporter.
namespace Sandbox;
/// <summary>
/// Tools menu entry. Opens the VMDL → OBJ file picker.
/// </summary>
[EditorApp( "Model Exporter", "file_download", "Export VMDL models as OBJ" )]
public sealed class ModelExporterApp : Widget
{
public ModelExporterApp()
{
Visible = false;
}
public override void Show()
{
VmdlObjExportMenu.ExportViaFilePicker();
Destroy();
}
}
/// <summary>
/// Asset Browser entry points for VMDL → OBJ export.
/// </summary>
public static class VmdlObjExportMenu
{
public static void ExportViaFilePicker()
{
var path = EditorUtility.OpenFileDialog( "Select VMDL", "vmdl", GetDefaultModelsFolder() );
if ( string.IsNullOrWhiteSpace( path ) )
return;
var asset = AssetSystem.FindByPath( path )
?? AssetSystem.All.FirstOrDefault( a =>
string.Equals( a.AbsolutePath, path, StringComparison.OrdinalIgnoreCase )
|| string.Equals( a.GetSourceFile( true ), path, StringComparison.OrdinalIgnoreCase ) );
if ( asset is null )
{
// Fall back to a project-relative path if the dialog returned an absolute path under Assets/.
var relative = TryMakeAssetRelativePath( path );
if ( relative is not null )
asset = AssetSystem.FindByPath( relative );
}
if ( asset is null )
{
EditorUtility.DisplayDialog( "Model Exporter",
"Could not resolve that file as a project asset.\nOpen it from the Asset Browser and use Export as OBJ…",
"OK", "", null );
return;
}
VmdlObjExporter.TryExportFromAsset( asset );
}
[Event( "asset.contextmenu" )]
public static void OnAssetContextMenu( AssetContextMenu context )
{
if ( context.SelectedList is null || context.SelectedList.Count == 0 )
return;
if ( !context.SelectedList.All( x =>
x.Asset is not null && x.Asset.Path is not null
&& x.Asset.Path.EndsWith( ".vmdl", StringComparison.OrdinalIgnoreCase ) ) )
return;
context.Menu.AddOption( "Export as OBJ…", "file_download", () =>
{
foreach ( var entry in context.SelectedList )
{
var asset = entry.Asset;
if ( asset is null )
continue;
VmdlObjExporter.TryExportFromAsset( asset );
}
} );
}
static string GetDefaultModelsFolder()
{
try
{
var project = Project.Current;
if ( project is null )
return null;
var assets = project.GetAssetsPath();
if ( string.IsNullOrWhiteSpace( assets ) )
return null;
var models = Path.Combine( assets, "models" );
return Directory.Exists( models ) ? models : assets;
}
catch
{
return null;
}
}
static string TryMakeAssetRelativePath( string absolutePath )
{
try
{
var project = Project.Current;
if ( project is null )
return null;
var assetsRoot = project.GetAssetsPath();
if ( string.IsNullOrWhiteSpace( assetsRoot ) )
return null;
var full = Path.GetFullPath( absolutePath );
var root = Path.GetFullPath( assetsRoot );
if ( !full.StartsWith( root, StringComparison.OrdinalIgnoreCase ) )
return null;
var relative = full[root.Length..].TrimStart( Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar );
return relative.Replace( '\\', '/' );
}
catch
{
return null;
}
}
}