Last active
November 28, 2022 04:45
-
-
Save FriendSea/09c434104e8216e35a2a4bf8172047f4 to your computer and use it in GitHub Desktop.
.blend のインポート時のFBXエクスポート処理を上書き太郎
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEditor; | |
using System.IO; | |
using System.Diagnostics; | |
public class BenderImportMod | |
{ | |
static readonly string fileName = "Unity-BlenderToFBX.py"; | |
static readonly string tempDir = "Temp"; | |
static string OriginalDir => Path.Combine(EditorApplication.applicationContentsPath, "Tools"); | |
static string OrignalPath => Path.Combine(OriginalDir, fileName); | |
static string TempFilePath => Path.Combine(tempDir, fileName); | |
[InitializeOnLoadMethod] | |
public static void OverrideWhenInitialize() | |
{ | |
if (File.Exists(TempFilePath)) return; | |
OverrideScript(); | |
} | |
[MenuItem("Tools/Overrite Blender Importer")] | |
public static void OverrideScript() | |
{ | |
//if (File.Exists(TempFilePath)) return; | |
var allText = File.ReadAllText(OrignalPath); | |
File.WriteAllText(TempFilePath, allText); | |
var modText = allText | |
.Replace(" bake_anim_use_nla_strips=False", " bake_anim_use_nla_strips=True") | |
.Replace(" bake_anim_use_all_actions=False", " bake_anim_use_all_actions=True") | |
.Replace(" use_active_collection=False", " use_active_collection=True"); | |
if (modText == allText) return; | |
var modfilePath = Path.Combine(tempDir, $"mod_{fileName}"); | |
File.WriteAllText(modfilePath, modText); | |
CopyAsAdmin(Path.GetFullPath(modfilePath), OrignalPath); | |
} | |
static void CopyAsAdmin(string src, string dst, bool debug = false) | |
{ | |
#if UNITY_EDITOR_OSX | |
File.Copy(src, dst, true); | |
#else | |
var info = new ProcessStartInfo() | |
{ | |
FileName = "PowerShell.exe", | |
UseShellExecute = !debug, | |
Arguments = @$"cp \""{src}\"" \""{dst}\""", | |
Verb = "runas", | |
WindowStyle = ProcessWindowStyle.Normal, | |
CreateNoWindow = false, | |
}; | |
if (debug) | |
{ | |
info.RedirectStandardError = true; | |
info.RedirectStandardOutput = true; | |
info.StandardOutputEncoding = System.Text.Encoding.GetEncoding("Shift_JIS"); | |
info.StandardErrorEncoding = System.Text.Encoding.GetEncoding("Shift_JIS"); | |
} | |
var proc = Process.Start(info); | |
proc.WaitForExit(); | |
if (debug) | |
{ | |
UnityEngine.Debug.Log(proc.StandardOutput.ReadToEnd()); | |
UnityEngine.Debug.Log(proc.StandardError.ReadToEnd()); | |
} | |
#endif | |
} | |
// if you need restore the Python file for other projects | |
#if false | |
[InitializeOnLoadMethod] | |
static void ResisterRestore() | |
{ | |
EditorApplication.quitting += () => { | |
CopyAsAdmin(TempFilePath, OrignalPath); | |
}; | |
} | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment