UE4编辑器Python编程7——操纵视口

操纵视口,即设置视口的位置和选择,也需要使用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 void SetViewportLocationAndRotation(int ViewportIndex, FVector Location, FRotator Rotation);

	UFUNCTION(BlueprintCallable, Category = "Unreal Python")
		static int GetActiveViewportIndex();
};

C++源文件:

#include "ZFunctions.h"
#include "Editor/UnrealEd/Public/Editor.h"// 需要"UnrealEd"模块
#include "Editor/UnrealEd/Public/LevelEditorViewport.h"

// ……

void UZFunctions::SetViewportLocationAndRotation(int ViewportIndex, FVector Location, FRotator Rotation)
{
	if (GEditor && ViewportIndex < GEditor->GetLevelViewportClients().Num())
	{
		FLevelEditorViewportClient* LevelViewportClient = GEditor->GetLevelViewportClients()[ViewportIndex];
		if (LevelViewportClient)
		{
			LevelViewportClient->SetViewLocation(Location);
			LevelViewportClient->SetViewRotation(Rotation);
		}
	}
}

int UZFunctions::GetActiveViewportIndex()
{
	int Index = 1;
	if (GEditor&&GCurrentLevelEditingViewportClient != nullptr)
	{
		GEditor->GetLevelViewportClients().Find(GCurrentLevelEditingViewportClient, Index);
	}
	return Index;
}

Python:

# coding: utf-8

import unreal

def getActiveViewportIndex():
    return unreal.ZFunctions.get_active_viewport_index()

def setViewportLocationAndRotation():
    unreal.ZFunctions.set_viewport_location_and_rotation(
        getActiveViewportIndex(),
        unreal.Vector(),
        unreal.Rotator()
    )

你可能感兴趣的:(UE4)