Skip to content

Instantly share code, notes, and snippets.

@builder-main
Last active March 3, 2023 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save builder-main/e3ab93e057f441c3ca8b9e077d0ddbf1 to your computer and use it in GitHub Desktop.
Save builder-main/e3ab93e057f441c3ca8b9e077d0ddbf1 to your computer and use it in GitHub Desktop.
Blender Script unity Installer
[InitializeOnLoad]
public static class BlenderInstallScript
{
//change this to your own import script
private static string blenderScriptRepo = "https://github.com/builder-main/unity-blender-better-import.git";
private static string blenderScripFileName = "Unity-BlenderToFBX.py";
private static string blenderScriptInstallPath = @"Data\Tools";
private static string backupSuffix = ".back";
static BlenderInstallScript()
{
CheckBlenderEnhancedImporterInstallation();
}
[MenuItem("Indus/"+nameof(CheckBlenderEnhancedImporterInstallation))]
public static void CheckBlenderEnhancedImporterInstallation()
{
EditorUtility.DisplayProgressBar("Loading Blender Enhanced Importer Repo", "", 0);
var repoPath = Path.Combine(Path.GetTempPath(), "BlenderEnhancedImporter");
if(Directory.Exists(repoPath).Not()) Repository.Clone(blenderScriptRepo, repoPath, new CloneOptions(){});
EditorUtility.DisplayProgressBar("Loading Blender Enhanced Importer Repo", "", 50);
var repo = new Repository(repoPath);
Commands.Pull(repo, new Signature("TMP", "TMP", DateTimeOffset.Now), new PullOptions());
var repoFileName = Path.Combine(repoPath, blenderScripFileName);
try
{
var unityEditorPath = Path.GetDirectoryName(EditorApplication.applicationPath);
var installFileName = Path.Combine(unityEditorPath, blenderScriptInstallPath, blenderScripFileName);
var installedFile = File.ReadAllText(installFileName);
var repoFile = File.ReadAllText(repoFileName);
if (installedFile.Equals(repoFile))
{
Debug.Log(
$"{installFileName} already match <{blenderScriptRepo}/{blenderScripFileName}> no need to reinstall");
return;
}
var backupFileName = installFileName + backupSuffix;
EditorUtility.ClearProgressBar();
var valid = EditorUtility.DisplayDialog(
"Blender Enhanced Importer Install",
$"{blenderScriptInstallPath} differs from {blenderScriptRepo}/{blenderScripFileName}, would you like to update ? Won't delete previous backup",
"Install and Backup", "Cancel");
if (!valid) return;
Debug.Log($"Copy <{repoFileName}> to <{installFileName}>, backup if does not exist to <{backupFileName}>");
if (File.Exists(backupFileName).Not()) //do not erase backup
{
File.Copy(installFileName, backupFileName, false);
}
File.Copy(repoFileName, installFileName, true);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
finally
{
EditorUtility.ClearProgressBar();
repo.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment