/
ExportObj.cs
132 lines (108 loc) · 4.61 KB
/
ExportObj.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using UnityEngine;
using UnityEditor;
using UnityEditor.ProBuilder.UI;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using UnityEngine.ProBuilder;
namespace UnityEditor.ProBuilder.Actions
{
sealed class ExportObj : MenuAction
{
public override ToolbarGroup group { get { return ToolbarGroup.Export; } }
public override string iconPath => string.Empty;
public override Texture2D icon => null;
public override TooltipContent tooltip { get { return _tooltip; } }
static readonly TooltipContent _tooltip = new TooltipContent
(
"Export Obj",
"Export a Wavefront OBJ file."
);
public override bool hidden
{
get { return true; }
}
public override bool enabled
{
get { return MeshSelection.selectedObjectCount > 0; }
}
protected override ActionResult PerformActionImplementation()
{
string res = ExportWithFileDialog(MeshSelection.topInternal);
if (string.IsNullOrEmpty(res))
return new ActionResult(ActionResult.Status.Canceled, "User Canceled");
Export.PingExportedModel(res);
return new ActionResult(ActionResult.Status.Success, "Export OBJ");
}
// Prompt user for a save file location and export meshes as Obj.
public static string ExportWithFileDialog(IEnumerable<ProBuilderMesh> meshes, bool asGroup = true, bool allowQuads = true, ObjOptions options = null)
{
if (meshes == null || !meshes.Any())
return null;
IEnumerable<Model> models = allowQuads
? meshes.Select(x => new Model(x.gameObject.name, x))
: meshes.Select(x => new Model(x.gameObject.name, x.mesh, x.GetComponent<MeshRenderer>().sharedMaterials, x.transform.localToWorldMatrix));
string path = null, res = null;
if (asGroup || models.Count() < 2)
{
ProBuilderMesh first = meshes.FirstOrDefault();
string name = first != null ? first.name : "ProBuilderModel";
path = UnityEditor.EditorUtility.SaveFilePanel("Export to Obj", "Assets", name, "obj");
if (string.IsNullOrEmpty(path))
return null;
res = DoExport(path, models, options);
}
else
{
path = UnityEditor.EditorUtility.SaveFolderPanel("Export to Obj", "Assets", "");
if (string.IsNullOrEmpty(path) || !Directory.Exists(path))
return null;
foreach (Model model in models)
res = DoExport(string.Format("{0}/{1}.obj", path, model.name), new List<Model>() { model }, options);
}
return res;
}
internal static string DoExport(string path, IEnumerable<Model> models, ObjOptions options)
{
string name = Path.GetFileNameWithoutExtension(path);
string directory = Path.GetDirectoryName(path);
List<string> textures = null;
string obj, mat;
if (ObjExporter.Export(name, models, out obj, out mat, out textures, options))
{
try
{
FileUtility.WriteAllText(string.Format("{0}/{1}.obj", directory, name), obj);
FileUtility.WriteAllText(string.Format("{0}/{1}.mtl", directory, name.Replace(" ", "_")), mat);
CopyTextures(textures, directory);
}
catch (System.Exception e)
{
Debug.LogWarning(string.Format("Failed writing obj to path: {0}\n{1}", string.Format("{0}/{1}.obj", path, name), e.ToString()));
return null;
}
}
else
{
Debug.LogWarning("No meshes selected.");
return null;
}
return path;
}
// Copy files from their path to a destination directory.
static void CopyTextures(List<string> textures, string destination)
{
foreach (string path in textures)
{
string dest = string.Format("{0}/{1}", destination, Path.GetFileName(path));
if (!File.Exists(path))
{
Log.Warning("OBJ Export: Could not find texture \"" + path + ",\" it will not be copied.");
continue;
}
if (!File.Exists(dest))
File.Copy(path, dest);
}
}
}
}