Is there a way to invoke Python from C++ with UObject parameters in UE5?

I’d like to be able to invoke Python from a C++ plugin and pass in a UObject so the Python script can then call back into the plugin.

Is there a recommended way to do that?

What I can see now is that:

  • IPythonScriptPlugin has a string based interface: so that’s not exactly what I want.
  • UPythonScriptLibrary also has the CustomThunk ExecutePythonScript call which I know can pass object references but I’m not sure if it’s reasonable for me to try and use that (it’s intended for BP calls).

Have a try this (UE5.1+):

       UMyObject* MyObject = NewObject<UMyObject>();

        // Create Python command
        FString PythonCode = FString::Printf(TEXT(
            "import unreal\n"

            "my_object = unreal.load_object(None, '%s')\n"  // get the object instance

        ), *MyObject->GetPathName());

	FPythonCommandEx PythonCommand;
	PythonCommand.Command = PythonCode;
	PythonCommand.ExecutionMode = EPythonCommandExecutionMode::ExecuteFile;

	// Execute the Python command
	bool bSuccess = IPythonScriptPlugin::Get()->ExecPythonCommandEx(PythonCommand);