[UE5.4.2]UE 5.4.2 – Python を使用して C++ 関数を呼び出す方法

BlancではC++フォルダができなかった。

ThirdPersonでやった。

親クラスに Blueprint Function Libraryを選択

Nameに適当にZFunctionsと入れCreate Class。

VisualStudioへ移動し

ZFunctions.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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

/**
 * 
 */
UCLASS()
class UE542PYTHONCALLCPP1_API UZFunctions : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
//ここから追加////////
public:
	UFUNCTION(BlueprintCallable)
	static void CalledFromPython(FString InputString);
//ここまで追加//////
};

ZFunctions.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "ZFunctions.h"
//ここから追加////////
void UZFunctions::CalledFromPython(FString InputString) {
	UE_LOG(LogTemp, Error, TEXT("%s"), *InputString);
}
//ここまで追加//////

SolutionExplorer > your Project Name (right click)>Build

実行する。(Local Windows Debagger)

Outputlogで実行する unrealのクラス一覧

import unreal
for x in sorted(dir(unreal)):
   print(x)

めっちゃ重い

ZFunction が出る

クラスを使ってみる unreal.ZFunctionsのメソッド一覧

import unreal
for x in sorted(dir(unreal.ZFunctions)):
   print(x)

called_from_python が出てくる。

呼んでみる

unreal.ZFunctions.called_from_python('my test string')

作った関数によりエラーのメッセージが返ってきた。

参考URL

[UE4.21]UE 4.21 – Python を使用して C++ 関数を呼び出す方法

コンテンツブラウザを右クリックしてNew C++ Class

親クラスに Blueprint Function Libraryを選択

Nameに適当にZFunctionsと入れCreate Class。

VisualStudioが起動し

ZFunctions.h

public:
UFUNCTION(BlueprintCallable)
static void CalledFromPython(FString InputString);

を追加してこうなる。

//.Fill out your copyright notice in the Description page of Project Settings.

#pragma once

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



UCLASS()
class UNREALPYTHONPROJECT_API UZFunctions : public UBlueprintFunctionLibrary
{
GENERATED BODY()

public:
   UFUNCTION(BlueprintCallable)
      static void CalledFromPython(FString InputString);
};

ZFunctions.cpp


//.Fill out your copyright notice in the Description page of Project Settings.

#include "ZFunctions.h"

void UZFunctions::CalledFromPython(FString InputString) {
UE_LOG(LogTemp, Error, TEXT("%s"), *InputString);
}

実行する。(Local Windows Debagger)

Outputlogで実行する

import unreal
for x in sorted(dir(unreal)):
   print(x)

ZFunction が出る

import unreal
for x in sorted(dir(unreal.ZFunctions)):
   print(x)

called_from_python が出てくる。

呼んでみる

unreal.ZFunctions.called_from_python('my test string')

呼ばれて作ったエラーメッセージが出た

参考URL

[UE5][C++]Python からブループリント関数を呼び出すことはできますか?

現時点では、Python はブループリント関数にアクセスできませんが、C++ メソッドにはアクセスできます。したがって
、解決策としては、C++ でブループリント関数を として宣言しBlueprintImplementableEvent、ブループリントでそれをオーバーライドします。

BlueprintImplementableEvent先に進む前に、を C++ から呼び出して、それが機能するかどうかを確認することをお勧めします。
次に、 を呼び出すための 2 番目の C++ 関数を作成しますBlueprintImplementableEvent。この関数は、Python に公開されるものであるため、
として実装する必要があります。BlueprintCallable

最後に、C++ とブループリントがコンパイルされたら、Python から呼び出し元関数を呼び出すだけです。
C++ クラスは、次のように通常の C++ モジュールとして表示されます。unreal.YourClassName.yourCallerFunctionName(yourClassInstance, args)
引数と戻り値は、期待どおりに処理されます。
関数を実行するクラス インスタンスを関数に渡す必要があります。特定の AActor インスタンスを操作するときに非常に便利です。


最後にもう 1 つ注意点があります。関数を、私が行ったようにブループリントイベントではなくブループリント関数として実装する必要がある場合は、関数を定数にするか、戻り値 (使用しない場合でも) を指定する必要があります。そうしないと、ブループリントのオーバーライドにイベントとして表示されます。

私のユースケースからの画面。明らかに、ブループリント関数の実装の詳細は関係ありません。
ブループリント関数

.h

.cpp

.py

参考

https://forums.unrealengine.com/t/is-it-possible-to-call-a-blueprint-function-from-python/447078/3