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 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.

4 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.

1 Like

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

2 Likes

I think that is the solution…

Awesome, thanks for sharing this!
I understand there’s a learning curve as a new-ish user (3-ish years), but I keep saying it: Unreal’s biggest issue feature discoverability…

It’s still so frustrating that in 3 years of use I’m unable to find this one node when using search terms like: “camera,” “get camera,” “find camera,” “view,” “get view,” “viewport,” etc. --It’s SO frustrating.

Okay can anyone give me a step by step guide how to acces this Blueprint node? Because its not showing up for me.

I did add “UnrealEd” to my public dependency modules in the Build.cs file and rebuilt the project, but the node is still not showing up… What else do I have to do?

Thanks in advance

Is “Utility Blueprints” a plugin or something? Can you provide a link? I’ve been searching for this function node but haven’t had any luck and nothing described in this post is working, help is appreciated.

Have you tried unchecking “context sensitive”? Version 5.4.2 here

image

That seems to be it. It’s likely a feature from 5.3 or 5.4, I’m on 5.2.1 and the node is not present even with context sensitive ticked off.

I tested this on version 4.27
Utility Blueprints are blueprints for creating tools for the editor, they are available by default. They changed in versions 5.0+, I checked, the screenshot shows the working version for 5.4.
You can get the camera coordinates in the editor if you create an Editor Utility Widget, and run it in the editor.
This should work for the Editor Utility Blueprint too, but for custom events called from other Utility Blueprints.