Accessing the position of the editor viewport camera

I created a foliage system that uses a basic streaming system depending on the player position. At the moment this only works inside the game but I also want to make it work directly inside the editor (when the game is not running), therefore I need to access the position of the camera in the main viewport and use this for the streaming.

Does anyone know how to get the position of the player in the editor or the position of the camera directly?

And another question also related to editor programming stuff, is there a way to check inside an actor class whether the editor is currently in edit mode or the game is running?

After a few hours of trial and error I finally found a working solution:

auto world = GetWorld();
if(world == nullptr)
     return;

auto viewLocations = world->ViewLocationsRenderedLastFrame;
if(viewLocations.Num() == 0)
     return;

FVector camLocation = viewLocations[0];
3 Likes

This is something I’ve been looking for over the course of the last week. I just wanted to say I really appreciate that you came back to leave your answer here after finding it.

How the F*CK is it possible that it is so hard to get the camera position in unreal?! this is ridiculous !

Thanks great answer! If you need both location and rotation i found this useful (https://answers.unrealengine.com/questions/5155/getting-editor-viewport-camera.html)

FViewport* activeViewport = GEditor->GetActiveViewport();
FEditorViewportClient* editorViewClient = (activeViewport != nullptr) ? (FEditorViewportClient*)activeViewport->GetClient() : nullptr;
if( editorViewClient )
{
	viewPos = editorViewClient->GetViewLocation();
	viewRot = editorViewClient->GetViewRotation();
}
5 Likes

A workaround for simplier needs could be drop a cube in the level, place your camera where you want the coordinates, right-click on cube → snap object to view. Now you have the cube coordinates to copy and use on set actor location etc

Digging out an old topic, but I made a plugin that shows the camera position inside a Level Viewport. It can also set this position to a desire location :slight_smile:

The actual code checking the stuff is

if (GCurrentLevelEditingViewportClient)
{
		FViewportCameraTransform& ViewTransform = GCurrentLevelEditingViewportClient->GetViewTransform();

		viewPos = ViewTransform.GetLocation();
}
1 Like

I don’t know if this is still relevant for anyone but the viewport camera is accessible in blueprint also. “Get Level Viewport Camera Info”
With that you can access the world space position and get the forward vector from the rotation.

3 Likes

I could not find that node, is it still available?

I couldn’t find that blueprint neider. How to access it? Thank you!

To access this node you have to get UnrealEditorSubsystem first.

Ahh, fantastic, thanks a ton for this! :sparkles: works like a charm!

For anyone else:
To build this, I had to add “UnrealEd” to my PublicDependencyModuleNames in [ProjectName].Build.cs

In Blueprints you can use the “Get Level Viewport Camera Info” function with Utility Blueprints.
UE4Editor_2024-03-04_16-31-39

1 Like

I think that is the solution…