Skip to content

Commit

Permalink
Merge pull request #12 from soupday/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
soupday authored Aug 23, 2021
2 parents 4e910d8 + 7d49978 commit a1a52ee
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 218 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

### v 0.1.7
- Reduced memory use for asset searches and character discovery.
- Prevented Import Tool window from holding on to character object references and hogging all the memory.
- Right Click menu "Import Character" now opens Import Tool window *only* for that character.
- Added refresh button on Import Tool window to rebuild the character list for when characters are added or removed.

### v 0.1.6
- Added custom diffusion profiles.

Expand Down
82 changes: 48 additions & 34 deletions Editor/CharacterInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,76 +25,83 @@ namespace Reallusion.Import
public class CharacterInfo
{
public enum ProcessingType { None, Basic, HighQuality }

public GameObject fbx;

public string guid;
public string path;
public string infoPath;
public string jsonPath;
public string name;
public string folder;
public TextAsset infoAsset;
public string folder;
public ProcessingType logType = ProcessingType.None;
public bool qualRefractiveEyes = true;
public bool bakeIsBaked = false;
public bool bakeCustomShaders = true;
private QuickJSON jsonData;
private BaseGeneration generation;

public CharacterInfo(GameObject obj)
{
path = AssetDatabase.GetAssetPath(obj);
fbx = obj;
name = Path.GetFileNameWithoutExtension(path);
folder = Path.GetDirectoryName(path);
infoPath = Path.Combine(folder, name + "_ImportInfo.txt");
infoAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(infoPath);
if (infoAsset)
Read();
else
Write();
}
private BaseGeneration generation;
private GameObject fbx;
private QuickJSON jsonData;

public CharacterInfo(string guid)
{
this.guid = guid;
path = AssetDatabase.GUIDToAssetPath(this.guid);
fbx = AssetDatabase.LoadAssetAtPath<GameObject>(path);
name = Path.GetFileNameWithoutExtension(path);
folder = Path.GetDirectoryName(path);
infoPath = Path.Combine(folder, name + "_ImportInfo.txt");
infoAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(infoPath);
if (infoAsset)
jsonPath = Path.Combine(folder, name + ".json");
if (File.Exists(infoPath))
Read();
else
Write();
}
}

public QuickJSON JsonData
public GameObject Fbx
{
get
{
if (jsonData == null)
if (fbx == null)
{
TextAsset jsonAsset = Util.GetJSONAsset(name, new string[] { folder });
jsonData = new QuickJSON(jsonAsset.text);
fbx = AssetDatabase.LoadAssetAtPath<GameObject>(path);
Util.LogInfo("CharInfo: " + name + " FBX Loaded");
}
return fbx;
}
}

public QuickJSON JsonData
{
get
{
if (jsonData == null)
{
jsonData = Util.GetJsonData(jsonPath);
Util.LogInfo("CharInfo: " + name + " JsonData Fetched");
}
return jsonData;
}
}


public BaseGeneration Generation

public BaseGeneration Generation
{
get
{
if (generation == BaseGeneration.None)
{
generation = RL.GetCharacterGeneration(fbx, name, JsonData);
string gen = Util.GetJsonGenerationString(jsonPath);
generation = RL.GetCharacterGeneration(Fbx, gen);
Util.LogInfo("CharInfo: " + name + " Generation " + generation.ToString());
Write();
}

return generation;
}
}

public void Release()
{
jsonData = null;
fbx = null;
Util.LogInfo("CharInfo: " + name + " Data Released!");
}

public bool CanHaveHighQualityMaterials
Expand All @@ -117,6 +124,10 @@ public bool CanHaveHighQualityMaterials

public void Read()
{
TextAsset infoAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(infoPath);

generation = BaseGeneration.None;

string[] lineEndings = new string[] { "\r\n", "\r", "\n" };
char[] propertySplit = new char[] { '=' };
string[] lines = infoAsset.text.Split(lineEndings, System.StringSplitOptions.None);
Expand Down Expand Up @@ -148,7 +159,10 @@ public void Read()
case "bakeCustomShaders":
if (value == "true") bakeCustomShaders = true;
else bakeCustomShaders = false;
break;
break;
case "generation":
generation = (BaseGeneration)System.Enum.Parse(typeof(BaseGeneration), value);
break;
}
}
}
Expand All @@ -157,12 +171,12 @@ public void Write()
{
StreamWriter writer = new StreamWriter(infoPath, false);
writer.WriteLine("logType=" + logType.ToString());
writer.WriteLine("generation=" + generation.ToString());
writer.WriteLine("qualRefractiveEyes=" + (qualRefractiveEyes ? "true" : "false"));
writer.WriteLine("bakeIsBaked=" + (bakeIsBaked ? "true" : "false"));
writer.WriteLine("bakeCustomShaders=" + (bakeCustomShaders ? "true" : "false"));
writer.Close();
AssetDatabase.ImportAsset(infoPath);
infoAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(infoPath);
AssetDatabase.ImportAsset(infoPath);
}
}

Expand Down
38 changes: 19 additions & 19 deletions Editor/Importer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ public class Importer

public Importer(CharacterInfo info)
{
ImporterWindow.LogReport("Initializing character import.");
Util.LogInfo("Initializing character import.");

// fetch all the asset details for this character fbx object.
characterInfo = info;
fbx = info.fbx;
fbx = info.Fbx;
id = fbx.GetInstanceID();
fbxPath = info.path;
importer = (ModelImporter)AssetImporter.GetAtPath(fbxPath);
Expand All @@ -78,14 +78,14 @@ public Importer(CharacterInfo info)
texFolder = Path.Combine(fbxFolder, "textures", characterName);
textureFolders = new List<string>() { fbmFolder, texFolder };

ImporterWindow.LogReport("Using texture folders:");
ImporterWindow.LogReport(" " + fbmFolder);
ImporterWindow.LogReport(" " + texFolder);
Util.LogInfo("Using texture folders:");
Util.LogInfo(" " + fbmFolder);
Util.LogInfo(" " + texFolder);

// find or create the materials folder for the character import.
string parentMaterialsFolder = Util.CreateFolder(fbxFolder, MATERIALS_FOLDER);
materialsFolder = Util.CreateFolder(parentMaterialsFolder, characterName);
ImporterWindow.LogReport("Using material folder: " + materialsFolder);
Util.LogInfo("Using material folder: " + materialsFolder);

// fetch the character json export data.
jsonData = info.JsonData;
Expand All @@ -98,7 +98,7 @@ public Importer(CharacterInfo info)

string jsonVersion = jsonData?.GetStringValue(characterName + "/Version");
if (!string.IsNullOrEmpty(jsonVersion))
ImporterWindow.LogReport("JSON version: " + jsonVersion);
Util.LogInfo("JSON version: " + jsonVersion);

generation = info.Generation;

Expand Down Expand Up @@ -138,7 +138,7 @@ public bool Import()
// only if we need to...
if (!AssetDatabase.IsValidFolder(fbmFolder))
{
ImporterWindow.LogReport("Extracting embedded textures to: " + fbmFolder);
Util.LogInfo("Extracting embedded textures to: " + fbmFolder);
Util.CreateFolder(fbxPath, characterName + ".fbm");
importer.ExtractTextures(fbmFolder);
}
Expand All @@ -159,7 +159,7 @@ public bool Import()
// if nescessary write changes and reimport so that the fbx is populated with mappable material names:
if (reimport)
{
ImporterWindow.LogReport("Resetting import settings for correct material generation and reimporting.");
Util.LogInfo("Resetting import settings for correct material generation and reimporting.");
AssetDatabase.WriteImportSettingsIfDirty(fbxPath);
AssetDatabase.ImportAsset(fbxPath, ImportAssetOptions.ForceUpdate);
}
Expand All @@ -170,9 +170,9 @@ public bool Import()
CacheBakedMaps();
}

ProcessObjectTree(fbx);
ProcessObjectTree(fbx);

ImporterWindow.LogReport("Writing changes to asset database.");
Util.LogInfo("Writing changes to asset database.");

// set humanoid animation type
RL.HumanoidImportSettings(fbx, importer, characterName, generation, jsonData);
Expand All @@ -184,9 +184,9 @@ public bool Import()
AssetDatabase.Refresh();

// create prefab
RL.CreatePrefabFromFbx(characterInfo);
RL.CreatePrefabFromFbx(characterInfo, fbx);

ImporterWindow.LogReport("Done!");
Util.LogInfo("Done!");

Selection.activeObject = fbx;

Expand Down Expand Up @@ -219,7 +219,7 @@ private void ProcessObject(GameObject obj)

if (renderer)
{
ImporterWindow.LogReport("Processing sub-object: " + obj.name);
Util.LogInfo("Processing sub-object: " + obj.name);

foreach (Material sharedMat in renderer.sharedMaterials)
{
Expand All @@ -241,7 +241,7 @@ private void ProcessObject(GameObject obj)
// determine the material type, this dictates the shader and template material.
MaterialType materialType = GetMaterialType(obj, sharedMat, sourceName, matJson);

ImporterWindow.LogReport(" Material name: " + sourceName + ", type:" + materialType.ToString());
Util.LogInfo(" Material name: " + sourceName + ", type:" + materialType.ToString());

// re-use or create the material.
Material mat = CreateRemapMaterial(materialType, sharedMat, sourceName);
Expand All @@ -253,7 +253,7 @@ private void ProcessObject(GameObject obj)
}
else
{
ImporterWindow.LogReport(" Material name: " + sourceName + " already processed.");
Util.LogInfo(" Material name: " + sourceName + " already processed.");
}
}
}
Expand Down Expand Up @@ -380,7 +380,7 @@ private Material CreateRemapMaterial(MaterialType materialType, Material sharedM
// save the material to the asset database.
AssetDatabase.CreateAsset(remapMaterial, matPath);

ImporterWindow.LogReport(" Created new material: " + remapMaterial.name);
Util.LogInfo(" Created new material: " + remapMaterial.name);

// add the new remapped material to the importer remaps.
importer.AddRemap(new AssetImporter.SourceAssetIdentifier(typeof(Material), sourceName), remapMaterial);
Expand All @@ -389,7 +389,7 @@ private Material CreateRemapMaterial(MaterialType materialType, Material sharedM
// copy the template material properties to the remapped material.
if (templateMaterial)
{
ImporterWindow.LogReport(" Using template material: " + templateMaterial.name);
Util.LogInfo(" Using template material: " + templateMaterial.name);
//Debug.Log("Copying from Material template: " + templateMaterial.name);
if (templateMaterial.shader && templateMaterial.shader != remapMaterial.shader)
remapMaterial.shader = templateMaterial.shader;
Expand Down Expand Up @@ -1297,7 +1297,7 @@ private bool ConnectTextureTo(string materialName, Material mat, string shaderRe
mat.SetTextureOffset(shaderRef, offset);
mat.SetTextureScale(shaderRef, tiling);

ImporterWindow.LogReport(" Connected texture: " + tex.name);
Util.LogInfo(" Connected texture: " + tex.name);

SetTextureImport(tex, name, flags);
}
Expand Down
5 changes: 2 additions & 3 deletions Editor/ImporterMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ public class ImporterMenu : Editor
[MenuItem("CC3/Import Characters", priority = 1)]
public static void InitCC3ImportGUI()
{
ImporterWindow.Init();
ImporterWindow.Init(ImporterWindow.Mode.multi, Selection.activeObject);
}

[MenuItem("Assets/CC3/Import Character", priority = 2000)]
public static void InitAssetCC3ImportGUI()
{
ImporterWindow window = ImporterWindow.Init();
window.SetActiveCharacter(Selection.activeObject);
ImporterWindow.Init(ImporterWindow.Mode.single, Selection.activeObject);
}

[MenuItem("Assets/CC3/Import Character", true)]
Expand Down
Loading

0 comments on commit a1a52ee

Please sign in to comment.