Announcement

Collapse
No announcement yet.

How to get locations of foliage instances?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

    How to get locations of foliage instances?

    I'm trying to figure out how to get world locations of foliage instances that have been painted onto a StaticMeshActor using the foliage brush, but not having much luck.

    I've created a C++ class derived from AStaticMeshActor. In the editor I can assign it a mesh (for example a plane) and then paint foliage instances on it in the editor. I then want to be able to get an array of all the instance types and locations. Here is what I am trying (this code is in the BeginPlay for the AStaticMeshActor-derived class, which now has the foliage instances painted on it:

    Code:
    for (TActorIterator<AInstancedFoliageActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
        {
            AInstancedFoliageActor* FoliageMesh = *ActorItr;
    
            UE_LOG(LogTemp, Warning, TEXT("InstancedFoliageActor found!"));
    
            UStaticMeshComponent* CurrentStaticMeshComponent = this->GetStaticMeshComponent();
    
            TMap < UFoliageType *, TArray < const FFoliageInstancePlacementInfo * > > FoliageInstances = FoliageMesh->GetInstancesForComponent(CurrentStaticMeshComponent);
        }
    The code is finding the InstancedFoliageActor, but maybe I'm misunderstanding what I'm supposed to pass to the 'GetInstancesForComponent' function, because it is returning an empty TMap.

    Or perhaps there is a different/better way to access the FFoliageInstancePlacementInfo data for all the instances?

    #2
    Trying to dig a little deeper into this, it seems the 'GetInstancesForComponent' function is failing on this part (line 1979 of InstancedFoliage.cpp):

    Code:
    const auto* InstanceSet = MeshInfo.ComponentHash.Find(BaseId);
    The BaseId always seems to be '1' but ComponentHash is just empty, which means the InstanceSet is false so the arrays never get built beyond that.

    What's weird is that MeshInfo.Instances is also empty, even though there are definitely instances that have been placed, and MeshInfo.Component.SortedInstances shows Num=7 (which is the correct number of foliage instances that I have painted in the scene).

    Still no closer to figuring out this works and why it's going wrong though...

    Comment


      #3
      I figured out how to get the information I need. The code below will output the mesh name, translation, rotation, and scale for each foliage instance in the scene:

      Code:
      for (TActorIterator<AInstancedFoliageActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
          {
              AInstancedFoliageActor* FoliageMesh = *ActorItr;
      
              for (auto& MeshPair : FoliageMesh->FoliageMeshes)
              {
                  const FFoliageMeshInfo& MeshInfo = *MeshPair.Value;
                  UHierarchicalInstancedStaticMeshComponent* MeshComponent = MeshInfo.Component;
                  TArray<FInstancedStaticMeshInstanceData> MeshDataArray = MeshComponent->PerInstanceSMData;
                  FString MeshName = MeshComponent->GetStaticMesh()->GetName();
      
                  for (auto& MeshMatrix : MeshDataArray)
                  {
                      FTransform MeshTransform = FTransform(MeshMatrix.Transform);
      
                      UE_LOG(LogTemp, Warning, TEXT("%s, %f, %f, %f, %f, %f, %f, %f, %f, %f,"),
                          *MeshName,
                          MeshTransform.GetLocation().X,
                          MeshTransform.GetLocation().Y,
                          MeshTransform.GetLocation().Z,
                          MeshTransform.GetRotation().X,
                          MeshTransform.GetRotation().Y,
                          MeshTransform.GetRotation().Z,
                          MeshTransform.GetScale3D().X,
                          MeshTransform.GetScale3D().Y, 
                          MeshTransform.GetScale3D().Z);
                  }
              }
          }
      No idea if this is helpful to anyone else, but there it is!

      Requires these includes:

      Code:
      #include "EngineUtils.h"
      #include "Runtime/Foliage/Public/InstancedFoliageActor.h"

      Comment


        #4
        Originally posted by localstarlight View Post
        I figured out how to get the information I need. The code below will output the mesh name, translation, rotation, and scale for each foliage instance in the scene:

        Code:
        for (TActorIterator<AInstancedFoliageActor> ActorItr(GetWorld()); ActorItr; ++ActorItr)
        {
        AInstancedFoliageActor* FoliageMesh = *ActorItr;
        
        for (auto& MeshPair : FoliageMesh->FoliageMeshes)
        {
        const FFoliageMeshInfo& MeshInfo = *MeshPair.Value;
        UHierarchicalInstancedStaticMeshComponent* MeshComponent = MeshInfo.Component;
        TArray<FInstancedStaticMeshInstanceData> MeshDataArray = MeshComponent->PerInstanceSMData;
        FString MeshName = MeshComponent->GetStaticMesh()->GetName();
        
        for (auto& MeshMatrix : MeshDataArray)
        {
        FTransform MeshTransform = FTransform(MeshMatrix.Transform);
        
        UE_LOG(LogTemp, Warning, TEXT("%s, %f, %f, %f, %f, %f, %f, %f, %f, %f,"),
        *MeshName,
        MeshTransform.GetLocation().X,
        MeshTransform.GetLocation().Y,
        MeshTransform.GetLocation().Z,
        MeshTransform.GetRotation().X,
        MeshTransform.GetRotation().Y,
        MeshTransform.GetRotation().Z,
        MeshTransform.GetScale3D().X,
        MeshTransform.GetScale3D().Y,
        MeshTransform.GetScale3D().Z);
        }
        }
        }
        No idea if this is helpful to anyone else, but there it is!

        Requires these includes:

        Code:
        #include "EngineUtils.h"
        #include "Runtime/Foliage/Public/InstancedFoliageActor.h"

        It works amazing, localstarlight, THANK YOU!

        Comment

        Working...
        X