转载: UE4编辑器Python编程1——C++与Python的互调

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Jingsongmaru/article/details/104571111/

 

简而言之就是 C++ 函数调用python文件, python 文件里调用C++函数,两个互逆操作主要是因为:

1. C++没有python这么方便可以直接脚本运行, 所以一些简单的UI功能的插件可以用Python来写然后用C++调用

2.python库的API还不全, 有的时候还是需要运用C++ 

 

 

使用Python调用C++函数:

这是我们要调用的函数:

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "ZFunctions.generated.h"

UCLASS()
class TEMP_SCRIPT_API UZFunctions : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()
    
public:

    UFUNCTION(BlueprintCallable)
    static void CallFromPython(FString inputString) 
    {
        UE_LOG(LogTemp, Error, TEXT("%S"), *inputString);
    }
};

在pyhton中调用:

# coding: utf-8

import unreal

unreal.ZFunctions.call_from_python('xx')# 注意格式!!!

 

使用C++执行Python脚本:

C++代码:

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "ZFunctions.generated.h"

UCLASS()
class TEMP_SCRIPT_API UZFunctions : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:
    UFUNCTION(BlueprintCallable, Category = "Unreal Python")
        static void ExecutePythonScript(FString PythonScript);
};

Cpp文件:

#include "ZFunctions.h"
// 注意,需要在模块的C#文件中添加对"PythonScriptPlugin"和"Python"模块的依赖
#include "../Plugins/Experimental/PythonScriptPlugin/Source/PythonScriptPlugin/Private/PythonScriptPlugin.h"

void UZFunctions::ExecutePythonScript(FString PythonScript)
{
    FPythonScriptPlugin::Get()->ExecPythonCommand(*PythonScript);
}

 

posted @ 2020-10-22 10:58  Cullchestar  阅读(1297)  评论(0编辑  收藏  举报