Merge pull request #97 from davidebbo/return-exec-output

Returns execute_code stdout
This commit is contained in:
ahujasid 2025-04-11 11:06:23 -07:00 committed by GitHub
commit bcc1ba1864
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,6 +12,8 @@ import traceback
import os import os
import shutil import shutil
from bpy.props import StringProperty, IntProperty, BoolProperty, EnumProperty from bpy.props import StringProperty, IntProperty, BoolProperty, EnumProperty
import io
from contextlib import redirect_stdout
bl_info = { bl_info = {
"name": "Blender MCP", "name": "Blender MCP",
@ -334,8 +336,14 @@ class BlenderMCPServer:
try: try:
# Create a local namespace for execution # Create a local namespace for execution
namespace = {"bpy": bpy} namespace = {"bpy": bpy}
# Capture stdout during execution, and return it as result
capture_buffer = io.StringIO()
with redirect_stdout(capture_buffer):
exec(code, namespace) exec(code, namespace)
return {"executed": True}
captured_output = capture_buffer.getvalue()
return {"executed": True, "result": captured_output}
except Exception as e: except Exception as e:
raise Exception(f"Code execution error: {str(e)}") raise Exception(f"Code execution error: {str(e)}")