Skip to content

Commit

Permalink
Add texture embedding option
Browse files Browse the repository at this point in the history
  • Loading branch information
pumachen committed Apr 18, 2023
1 parent d7e5f65 commit 2c5c752
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Editor/API/HDAProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static void ProcessHDAAsync(string hda, IEnumerable<HouParm> parms, Actio
}

public static IEnumerator ProcessHDARoutine(string hda, IEnumerable<HouParm> parms, Action<ZipArchive> completed,
Action failed = null, int timeout = 120)
Action failed = null, int timeout = 300)
{
Uri uri = GetUri(hda);
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
Expand Down
31 changes: 7 additions & 24 deletions Editor/Parm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ public class StringParm : HouParm
[SerializeField]
public Texture2D texture;
[SerializeField]
public bool embedTexture;
[SerializeField]
public GameObject model;

public string value;
Expand All @@ -145,6 +147,7 @@ public override void GUILayout()
case FileType.Usd:
{
model = EditorGUILayout.ObjectField(template.label, model, typeof(GameObject), true) as GameObject;
embedTexture = EditorGUILayout.Toggle("Embed Texture", embedTexture);
value = "";
break;
}
Expand Down Expand Up @@ -197,43 +200,23 @@ byte[] rawModel
}
string path = Path.Combine(Application.temporaryCachePath,
$"{template.name}_{model.GetInstanceID()}.fbx");
ExportFBX(path, model);
ExportFBX(path, model, embedTexture);
var rawFBX = File.ReadAllBytes(path);
File.WriteAllBytes(Path.Combine(Application.temporaryCachePath, "TMP.fbx"), rawFBX);
return rawFBX;
}
}

private static void ExportFBX(string path, GameObject model)
private static void ExportFBX(string path, GameObject model, bool embedTexture)
{
model = GameObject.Instantiate(model);
ExportModelOptions exportSettings = new ExportModelOptions();
exportSettings.ExportFormat = ExportFormat.Binary;
exportSettings.EmbedTextures = true;
exportSettings.EmbedTextures = embedTexture;
ModelExporter.ExportObject(path, model, exportSettings);
Object.DestroyImmediate(model);
}

private static void ExportBinaryFBX (string filePath, UnityEngine.Object singleObject)
{
// Find relevant internal types in Unity.Formats.Fbx.Editor assembly
Type[] types = AppDomain.CurrentDomain.GetAssemblies().First(x => x.FullName == "Unity.Formats.Fbx.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null").GetTypes();
Type optionsInterfaceType = types.First(x => x.Name == "IExportOptions");
Type optionsType = types.First(x => x.Name == "ExportOptionsSettingsSerializeBase");

// Instantiate a settings object instance
MethodInfo optionsProperty = typeof(ModelExporter).GetProperty("DefaultOptions", BindingFlags.Static | BindingFlags.NonPublic).GetGetMethod(true);
object optionsInstance = optionsProperty.Invoke(null, null);

// Change the export setting from ASCII to binary
FieldInfo exportFormatField = optionsType.GetField("exportFormat", BindingFlags.Instance | BindingFlags.NonPublic);
exportFormatField.SetValue(optionsInstance, 1);

// Invoke the ExportObject method with the settings param
MethodInfo exportObjectMethod = typeof(ModelExporter).GetMethod("ExportObject", BindingFlags.Static | BindingFlags.NonPublic, Type.DefaultBinder, new Type[] { typeof(string), typeof(UnityEngine.Object), optionsInterfaceType }, null);
exportObjectMethod.Invoke(null, new object[] { filePath, singleObject, optionsInstance });
}

public override IMultipartFormSection formSection
{
get
Expand All @@ -248,7 +231,7 @@ public override IMultipartFormSection formSection
&& template.fileType == FileType.Geometry
&& model != null)
{
return new MultipartFormFileSection(template.name, rawModel, $"{template.name}_{model.GetInstanceID()}.fbx", "image/aces");
return new MultipartFormFileSection(template.name, rawModel, $"{template.name}_{model.GetInstanceID()}.fbx", "multipart/form-data");
}
return new MultipartFormDataSection(template.name, JsonConvert.SerializeObject(value));
}
Expand Down

0 comments on commit 2c5c752

Please sign in to comment.