获取对象可在编辑器中编辑的属性,也需要用C++实现,然后使用Python调用C++:
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 TArray<FString> GetAllProperties(UClass* Class)
{
TArray<FString> Ret;
if (Class != nullptr)
{
// 使用迭代器遍历属性
for (TFieldIterator<UProperty> It(Class); It; ++It)
{
UProperty* Property = *It;
if (Property->HasAnyPropertyFlags(EPropertyFlags::CPF_Edit))// 筛选出暴露到编辑器的属性
{
Ret.Add(Property->GetName());
}
}
}
return Ret;
}
};
Python:
# coding: utf-8
import unreal
def getAllProperties(object_class):
''' 获取属性名 '''
return unreal.ZFunctions.get_all_properties(object_class)
def printAllProperties():
obj = unreal.Actor()
object_class = obj.get_class()
for x in getAllProperties(object_class):
y = x
while len(y) < 50:
y = ' ' + y
print y + ' : ' + str(obj.get_editor_property(x))# 获取属性值