You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Python脚本以字符串形式传入UE5远程执行函数?

从外部Python向UE远程执行完整脚本的解决方案

问题背景

已有一段从外部Python(如Blender Python)向Unreal Engine编辑器发送单条命令的代码,可正常运行:

import sys
sys.path.append(r'C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python')
import remote_execution as remote

def executeCommand(command):
    remote_exec = remote.RemoteExecution()
    remote_exec.start()
    remote_exec.open_command_connection(remote_exec.remote_nodes)
    exec_mode = 'EvaluateStatement'
    rec = remote_exec.run_command(command, exec_mode=exec_mode)
    return rec

command = "print('hello')"
result = executeCommand(command)

这段代码会在UE编辑器日志中输出hello。现在需要将完整的Python脚本传入executeCommand()函数,但该函数仅接收字符串而非.py文件。尝试切换到ExecuteFile执行模式后,出现以下错误:

Python: Traceback (most recent call last):
  File "\test_remote_import.py", line 19, in <module>
    result = executeCommand(command)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "\test_remote_import.py", line 11, in executeCommand
    rec = remote_exec.run_command(command, exec_mode=exec_mode)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python\remote_execution.py", line 124, in run_command
    data = self._command_connection.run_command(command, unattended, exec_mode)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python\remote_execution.py", line 429, in run_command
    self._send_message(_RemoteExecutionMessage(_TYPE_COMMAND, self._node_id, self._remote_node_id, {
  File "C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python\remote_execution.py", line 444, in _send_message
    self._command_channel_socket.sendall(message.to_json_bytes())
                                         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python\remote_execution.py", line 551, in to_json_bytes
    json_str = self.to_json()
               ^^^^^^^^^^^^^^
  File "C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python\remote_execution.py", line 542, in to_json
    return _json.dumps(json_obj, ensure_ascii=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.2\4.2\python\Lib\json\__init__.py", line 238, in dumps
    **kw).encode(obj)
          ^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.2\4.2\python\Lib\json\encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.2\4.2\python\Lib\json\encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Blender Foundation\Blender 4.2\4.2\python\Lib\json\encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type module is not JSON serializable

出错的代码是将导入的模块对象直接传给函数:

import sys
sys.path.append(r'C:UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python')
import remote_execution as remote
import test_print as test

def executeCommand(command):
    remote_exec = remote.RemoteExecution()
    remote_exec.start()
    remote_exec.open_command_connection(remote_exec.remote_nodes)
    exec_mode = 'ExecuteFile' 
    rec = remote_exec.run_command(command, exec_mode=exec_mode)
    return rec

command = test
result = executeCommand(command)

test_print.py内容仅为:

print("success")

错误原因

run_command()函数要求command参数为字符串,但你传入了导入后的模块对象,而模块无法被JSON序列化,导致发送请求时失败。根据ExecuteFile模式的文档说明,该模式支持两种字符串输入:

  • 完整的Python脚本代码字符串
  • UE可访问的脚本文件绝对路径字符串

正确解决方案

方法1:读取本地脚本内容作为字符串传递

直接读取.py文件的全部内容,转成字符串后传入函数:

import sys
sys.path.append(r'C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python')
import remote_execution as remote

def executeCommand(command):
    remote_exec = remote.RemoteExecution()
    remote_exec.start()
    remote_exec.open_command_connection(remote_exec.remote_nodes)
    exec_mode = 'ExecuteFile' 
    rec = remote_exec.run_command(command, exec_mode=exec_mode)
    return rec

# 读取本地脚本文件内容
with open("test_print.py", "r", encoding="utf-8") as f:
    command = f.read()

result = executeCommand(command)

方法2:传递UE可访问的脚本文件绝对路径

如果test_print.py位于UE的Python搜索路径(如项目Content/Python目录)或UE能直接访问的路径,可直接传递文件路径字符串:

import sys
sys.path.append(r'C:\UnrealEngine5\Engine\Plugins\Experimental\PythonScriptPlugin\Content\Python')
import remote_execution as remote

def executeCommand(command):
    remote_exec = remote.RemoteExecution()
    remote_exec.start()
    remote_exec.open_command_connection(remote_exec.remote_nodes)
    exec_mode = 'ExecuteFile' 
    rec = remote_exec.run_command(command, exec_mode=exec_mode)
    return rec

# 传入脚本的绝对路径,注意用原始字符串或转义反斜杠
command = r"D:\YourUEProject\Content\Python\test_print.py"
result = executeCommand(command)

验证结果

两种方法均可让UE编辑器日志中输出success,实现完整脚本的远程执行。

内容的提问来源于stack exchange,提问作者Tireless123

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 16:24:50