mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-09-11 21:38:56 -05:00
implement AnimationClip extract
This commit is contained in:
@@ -1,16 +1,63 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEditor.Experimental.AssetImporters;
|
||||
using UnityEngine;
|
||||
using VRMShaders;
|
||||
|
||||
namespace UniGLTF
|
||||
{
|
||||
public static class EditorAnimation
|
||||
{
|
||||
public static void OnGUIAnimation(GltfParser parser)
|
||||
public static void OnGUIAnimation(ScriptedImporter importer, GltfParser parser)
|
||||
{
|
||||
for (int i = 0; i < parser.GLTF.animations.Count; ++i)
|
||||
var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is AnimationClip);
|
||||
using (new EditorGUI.DisabledScope(hasExternal))
|
||||
{
|
||||
var a = parser.GLTF.animations[i];
|
||||
GUILayout.Label($"{i}: {a.name}");
|
||||
if (GUILayout.Button("Extract Animation ..."))
|
||||
{
|
||||
Extract(importer, parser);
|
||||
}
|
||||
}
|
||||
|
||||
importer.DrawRemapGUI<AnimationClip>(parser.GLTF.animations.Select(x => new SubAssetKey(typeof(AnimationClip), x.name)));
|
||||
|
||||
if (GUILayout.Button("Clear"))
|
||||
{
|
||||
importer.ClearExternalObjects(
|
||||
typeof(UnityEngine.AnimationClip)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static string GetAndCreateFolder(string assetPath, string suffix)
|
||||
{
|
||||
var path = $"{Path.GetDirectoryName(assetPath)}/{Path.GetFileNameWithoutExtension(assetPath)}{suffix}";
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(path);
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
public static void Extract(ScriptedImporter importer, GltfParser parser)
|
||||
{
|
||||
if (string.IsNullOrEmpty(importer.assetPath))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
var path = GetAndCreateFolder(importer.assetPath, ".Animations");
|
||||
foreach (var (key, asset) in importer.GetSubAssets<AnimationClip>(importer.assetPath))
|
||||
{
|
||||
asset.ExtractSubAsset($"{path}/{asset.name}.asset", false);
|
||||
}
|
||||
}
|
||||
|
||||
AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace UniGLTF
|
||||
public static void OnGUI(ScriptedImporter importer, GltfParser parser, ITextureDescriptorGenerator textureDescriptorGenerator, Func<string, string> textureDir, Func<string, string> materialDir)
|
||||
{
|
||||
var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D);
|
||||
using (new EditorGUI.DisabledGroupScope(!hasExternal))
|
||||
using (new EditorGUI.DisabledScope(hasExternal))
|
||||
{
|
||||
if (GUILayout.Button("Extract Materials And Textures ..."))
|
||||
{
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace UniGLTF
|
||||
break;
|
||||
|
||||
case Tabs.Animation:
|
||||
EditorAnimation.OnGUIAnimation(m_parser);
|
||||
EditorAnimation.OnGUIAnimation(m_importer, m_parser);
|
||||
break;
|
||||
|
||||
case Tabs.Materials:
|
||||
|
||||
@@ -45,7 +45,7 @@ namespace UniGLTF
|
||||
break;
|
||||
|
||||
case Tabs.Animation:
|
||||
EditorAnimation.OnGUIAnimation(m_parser);
|
||||
EditorAnimation.OnGUIAnimation(m_importer, m_parser);
|
||||
break;
|
||||
|
||||
case Tabs.Materials:
|
||||
|
||||
@@ -130,13 +130,14 @@ namespace UniGLTF
|
||||
RestoreOlderVersionValues();
|
||||
|
||||
FixMeshNameUnique();
|
||||
foreach(var image in GLTF.images)
|
||||
foreach (var image in GLTF.images)
|
||||
{
|
||||
image.uri = PrepareUri(image.uri);
|
||||
}
|
||||
FixTextureNameUnique();
|
||||
FixMaterialNameUnique();
|
||||
FixNodeName();
|
||||
FixAnimationNameUnique();
|
||||
|
||||
// parepare byte buffer
|
||||
//GLTF.baseDir = System.IO.Path.GetDirectoryName(Path);
|
||||
@@ -308,6 +309,34 @@ namespace UniGLTF
|
||||
}
|
||||
}
|
||||
|
||||
void FixAnimationNameUnique()
|
||||
{
|
||||
var used = new HashSet<string>();
|
||||
for (int i = 0; i < GLTF.animations.Count; ++i)
|
||||
{
|
||||
var animation = GLTF.animations[i];
|
||||
var originalName = animation.name;
|
||||
int j = 2;
|
||||
|
||||
if (string.IsNullOrEmpty(animation.name))
|
||||
{
|
||||
animation.name = $"animation_{i}";
|
||||
}
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (used.Add(animation.name))
|
||||
{
|
||||
#if VRM_DEVELOP
|
||||
// Debug.Log($"Material: {material.name}");
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
animation.name = string.Format("{0}({1})", originalName, j++);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RestoreOlderVersionValues()
|
||||
{
|
||||
var parsed = UniJSON.JsonParser.Parse(Json);
|
||||
|
||||
@@ -69,7 +69,7 @@ namespace UniVRM10
|
||||
public override void OnInspectorGUI()
|
||||
{
|
||||
// select sub editor
|
||||
using (new EditorGUI.DisabledGroupScope(false))
|
||||
using (new EditorGUI.DisabledScope(false))
|
||||
{
|
||||
_tab = (Tabs)EditorGUILayout.EnumPopup("Select GUI", _tab);
|
||||
EditorGUILayout.Separator();
|
||||
|
||||
@@ -173,7 +173,7 @@ namespace UniVRM10
|
||||
}
|
||||
}
|
||||
|
||||
using (new EditorGUI.DisabledGroupScope(true))
|
||||
using (new EditorGUI.DisabledScope(true))
|
||||
{
|
||||
if (m_constraints != null)
|
||||
{
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace UniVRM10
|
||||
public static void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm)
|
||||
{
|
||||
var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is VRM10MetaObject || x.Value is VRM10ExpressionAvatar || x.Value is VRM10Expression);
|
||||
using (new EditorGUI.DisabledScope(!hasExternal))
|
||||
using (new EditorGUI.DisabledScope(hasExternal))
|
||||
{
|
||||
if (GUILayout.Button("Extract Meta And Expressions ..."))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user