Skip to content

Instantly share code, notes, and snippets.

@FleshMobProductions
Last active June 21, 2024 18:10
Show Gist options
  • Save FleshMobProductions/f598096b705f6a9c96beb58e284303f1 to your computer and use it in GitHub Desktop.
Save FleshMobProductions/f598096b705f6a9c96beb58e284303f1 to your computer and use it in GitHub Desktop.
Double click a FBX file inside Unity and open it inside Blender properly (Windows only. User needs to edit blenderPath to point to their installation)
// Place inside an "Editor" folder
#if PLATFORM_STANDALONE_WIN
using System;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public static class FbxBlenderOpeningProcessor
{
// Set this variable to your installation path.
// If the path was added to your "PATH" system environment variable, it should be
// possible to just use "blender" as path
private static readonly string blenderPath = @"D:\Applications\Blender_3.0\blender.exe";
[OnOpenAsset]
public static bool OnOpenAsset(int instanceId, int line)
{
UnityEngine.Object obj = EditorUtility.InstanceIDToObject(instanceId);
string assetPath = AssetDatabase.GetAssetPath(instanceId);
if (string.Equals(Path.GetExtension(assetPath), ".fbx", StringComparison.OrdinalIgnoreCase)
&& obj is GameObject)
{
Debug.Log($"Open FBX file \"{assetPath}\" in Blender");
OpenAsFbxInBlender(assetPath);
return true; // Prevent Unity from further processing the opening task
}
return false; // Continue builtin handling
}
private static void OpenAsFbxInBlender(string assetPath)
{
string assetPathPythonFull = Path.GetFullPath(assetPath).Replace("\\", "/");
// Delete default cube and import fbx:
string[] instructions = new string[]
{
"import bpy",
"bpy.data.objects['Cube'].select_set(True)",
"bpy.ops.object.delete()",
$"bpy.ops.import_scene.fbx( filepath = '{assetPathPythonFull}' )"
};
string pythonLoadFbxArgument = CreatePythonExpressionArgument(instructions);
StartBlenderWithArguments(pythonLoadFbxArgument);
}
private static string CreatePythonExpressionArgument(string[] pythonLines)
{
// Multi line instructions possible using instruction separation with ;
return $"--python-expr \"{string.Join(";", pythonLines)}\"";
}
private static void StartBlenderWithArguments(string arguments)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
FileName = blenderPath,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
Arguments = arguments
};
process.StartInfo = startInfo;
process.Start();
}
}
#endif
Further resources and links for those interested.
Official Blender command line docs: https://docs.blender.org/manual/en/latest/advanced/command_line/arguments.html
Run command line from C#: https://stackoverflow.com/questions/1469764/run-command-prompt-commands
Delete the Default Cube using Blender Python: https://www.youtube.com/watch?v=flP4csFnIOc
Blender Python: Delete all scene objects and import FBX: https://wtools.io/paste-code/y3B
Pass command line arguents to a blender python script:
https://b3d.interplanety.org/en/how-to-pass-command-line-arguments-to-a-blender-python-script-or-add-on/
Execute multiple lines of python with --python-expr in cmd:
https://stackoverflow.com/questions/49783426/running-blender-from-command-prompt-beginner
Command for importing an FBX file through Python in Blender::
https://blender.stackexchange.com/questions/65815/how-do-i-import-a-fbx-file-into-blender-from-command-line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment