Get Static Mesh Bounds (Size)

I know that there is a lovely function that you can use to get the bounds of an actor as long as you have an instance of it, but what if I wanted to get the bounds of a static mesh before I added it? How would I go about that?

Thanks,

  • Alex.

Deep in the recesses of the mind lies everything you have seen in the last minute. To access that information, you would have to know where in your mind it is.

Yes… I’ve seen Inside Out. It’s a good movie.

What you can do is spawn the static mesh into the game world, visibility off, in some remote place far, far away and still get its size. Then destroy it. All in a fraction of a second. It only has to exist as long as needed to get its size and whatever other information you want to gather from it. Unless its some insanely huge thing, I doubt you would even know it happened since it is invisible and fast.

That’s one way you could go about it…

I did think about doing that, but I mean… it’s not really a nice way to go about it is it? xD It is a one time thing though, so it wouldn’t be so bad. I am doing this in the construction script, though.

Im curious,. In which use case would that make sense?
Really, this has been asked before by some people and I still cant see a usefull scenario for that where you need to know sizes beforehand…

I’m generating a room in construction script. I need to be able to place walls on all four sides and then a roof on the top. It is possible if I had each room have it’s own width, height and length variables that I could probably go by them to place everything, but even so I’ve needed this in the past for other things, so it would be useful to have.

I once made something like this and what I did was:
I spawned the floor, retrieved its size. Then spawned a wall (still at the BP origin), retrieved the size of the wall and moved it accordingly.
Then I spawned a roof, got its size and moved it in place…

A bit simmilar what i do here (especially the stairs): https://forums.unrealengine.com/showthread.php?74914-Default-Wall-Size&p=323433#post323433
If you dont scale things its the flow as if “NativeDims” would be always true…

So its no problem of using the size after you create the mesh. Knowing beforehand is really not neccessary…

I’ve also needed to get the size of a static mesh when doing some procedural layout. As far as I could tell, this isn’t exposed to Blueprint. In the C++ you would use [FONT=courier new]Mesh->GetBounds().GetBox().GetSize() assuming [FONT=courier new]Mesh is a [FONT=courier new]UStaticMesh. Fortunately, it’s not that complicated to create a Blueprint callable function that does this for you. Here is the relevant code snippet for you:

In YourGameBlueprintLibrary.h:



UCLASS(MinimalAPI)
class UYourGameBlueprintLibrary: public UBlueprintFunctionLibrary
{
    GENERATED_BODY()

public:

    UFUNCTION(BlueprintPure, Category = "YourGame")
    static FVector GetStaticMeshSize(class UStaticMesh* Mesh);
};


In YourGameBlueprintLibrary.cpp:



FVector UYourGameBlueprintLibrary::GetStaticMeshSize(class UStaticMesh* Mesh)
{
  if (!Mesh)
    return FVector::ZeroVector;


  return Mesh->GetBounds().GetBox().GetSize();
}


Hopefully that’ll be enough to get you started.

Im still curious to hear about a use case where this is necessary, (knowing the size before spawing)…

Except for the GetLocalBounds function…

Hmm, I had overlooked [FONT=courier new]StaticMeshComponent’s [FONT=courier new]GetLocalBounds function. Not sure how I missed that, but oh well. That’s probably the function that Aoredon is looking for. Thank you for pointing that out.

As for the use case … the issue isn’t knowing the size before spawning, it’s knowing the size *without *spawning. I see this mostly happening with procedural generation that has optional content. For instance, let’s say you have a blueprint that creates a dungeon room and its decorations. The level designer places the blueprint in the world and sets the size of the room. The blueprint takes care of filling the room with tables, torches, and other decorations. Before the blueprint can place a decoration in the room it has to know if it can fit (due to the room’s size and other decorations it already spawned). It could randomly pick a decoration, spawn it, get it’s size, and then possibly delete it if it doesn’t fit. But, it would be better to avoid the spawning if the object isn’t going to be used.

But, I agree with you, this isn’t a typical use case.

I don’t know. It seems like it takes in an instance when I need to be able to pass the class which is what your C++ solution seems to do. I’ll try it out now and get back to you. :slight_smile:

Ok. Valid case. I would solve it this way:

Initially I assume that at least one decoration item of all available will fit into the room.
So the the room BP adds a static mesh component. This spawns the (empty) component.
Then I would set the static mesh property of the component to a decoration item and see if it fits.
If it does not, I would try to scale it down (torches can come in different sizes). If that still does not work becase the room is too small or scaling would be too excessive, then try the next one.
Only when no decoration item fits, not even by scaling, then I would remove the static mesh component again.

So, no matter how many meshes you try out, there is only one component involved.
You are not constantly spawning and destroying static mesh components.
Only in rooms that remain empty because nothing fits you create and destroy a component “for nothing”…

Sorry I’m late to get back. I had a lot of issues with Visual Studio which confused me. Turns out there was an issues with the project settings which didn’t link the libraries and as such caused compiler errors.

Lee, your solution worked perfectly. While it does use C++, your solution essentially required zero effort on my end and allows me to continue using Blueprint. Essentially I can still continue on without using C++. I actually learnt quite a bit and I appreciate you putting the time and effort in to help me out.

Thank you. :slight_smile:

I found this post looking for a similar solution. I was able to use “Get Bounding Box” from the Static Mesh attached to BP’s InstancedStaticMeshComponent. It has min/max x/y/z

1 Like

i didnt know it contained all 3 dimensions, thanks alot. i almost skipped that node entirely

Drag off your mesh variable and “Get Bounds” then break the struct and multiply the Box Extent by 2 (because it’s using a sphere radius which is half sized).

2 Likes


1 Like