Python Editor Scripting: Set viewport orientation?

Hey, I hope to find a solution for the issue with viewport reset: https://answers.unrealengine.com/questions/858079/viewport-reset-after-map-load-how-to-avoid-this.html

… So I thought that maybe there is some way to control viewport orientation through Python script on map load. In BP I’ve found the SetLevelViewportCameraInfo in PlacedEditorUtilityBase, but it controls only one ‘main’ pane and there is no way to specify other panes, when using multi-pane layouts.

Basically I need to set view location/orientation on each pane separately, on map load. Do you know if something like that is possible via Python?
I’ve found the ‘set_view_location’ method in Python API in unreal.Viewport, but can I somehow specify the view (in multi view layout) that I want to control?

I would be grateful for any info, I’m still new to the UE Python Scripting.

Hello,

I looked at this a little and I did not find any way to do it via Python only.
But there is probably a way with Python and C++.

The solution I have in mind uses the pilot actor functionality, but it require to have an actor in the scene located and oriented the way you want to have the viewport. (Not tested yet)
Is it something that could work for you?

Hello, thanks for looking into this!

In my case I was hoping for non-C++ solution because it’s for my next pure blueprint project for Marketplace, but any solution is appreciated, maybe it will be useful for others in future.

Right now I use the camera piloting as a workaround - so I can set viewport pane transform via the Perspective dropdown menu -> click placed Camera Actor in each pane, but it needs to be done manually every time a new map is opened. Basically it’s an extra 6 clicks (9 if we count the un-pilot button click) on every map load with 3-pane layout.
So yes, in my case I already have actors (Camera Actors) placed in desired locations and actor piloting sounds good, but unfortunately I’m limited to Blueprints/Python.

I’ve also checked the Camera Bookmarks, but they set view on all panes, not individually.

BTW. Maybe I should make a feature request for that…? So each map would save last viewport pane transforms and restore it on load instead of resetting it every time…

Hi again,
Here is my C++ solution. It is not amazing, but I hope it helps.

If you find any Python solution somewhere, let me know. I’m interested :slight_smile:

Also, I agree with the idea of a feature request. Not sure about the priority for them, but it could be a good ‘nice to have’.

.h



#pragma once
#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "CppLib.generated.h"

UCLASS()
class UNREALPYTHONPROJECT_API UCppLib : public UBlueprintFunctionLibrary {
    GENERATED_BODY()
public:
    UFUNCTION(BlueprintCallable, Category = "Category")
        static void SnapViewportToActor(int ViewportIndex, AActor* Actor);
};


.cpp



#include "CppLib.h"
#include "Editor/UnrealEd/Public/LevelEditorViewport.h" // PublicDependencyModuleNames-> "UnrealEd"
// ViewportIndex is affected by the spawning order and not the viewport number.
//    e.g. Viewport 4 can be the first one if the user spawned it first.
//         And can become the last one if the user open the other ones and then close and re-open Viewport 4.
//    Also, the indexes are confusing.
// 1st Spawned Viewport : Index = 1
// 2nd Spawned Viewport : Index = 5
// 3rd Spawned Viewport : Index = 9
// 4th Spawned Viewport : Index = 13
void UCppLib::SnapViewportToActor(int ViewportIndex, AActor* Actor) {
    if (Actor != nullptr && GEditor != nullptr && ViewportIndex < GEditor->LevelViewportClients.Num()) {
        FLevelEditorViewportClient* LevelViewportClient = GEditor->LevelViewportClients[ViewportIndex];
        if (LevelViewportClient != nullptr) {
            LevelViewportClient->SetViewLocation(Actor->GetActorLocation());
            LevelViewportClient->SetViewRotation(Actor->GetActorRotation());
            LevelViewportClient->Invalidate();
        }
    }
}


Thank you, I’ll give this a try when I switch to C++ project. I hope that it will also help someone with a similar problem.
In the meantime, I’ll probably make a feature request for that.