Getting and adding foliage components, UE4-23

I have a task to programmatically and randomly change half of a large number of foliage instances to another foliage instance to give variation to our levels. Below the code gives some hope of doing such a task.

w = unreal.EditorLevelLibrary.get_editor_world()
foliages = unreal.GameplayStatics.get_all_actors_of_class(w, unreal.InstancedFoliageActor)
fol = foliages[0]
comps = fol.get_components_by_class(unreal.HierarchicalInstancedStaticMeshComponent)
inst_id = 0
c = comps[0] # first component
xform = c.get_instance_transform(inst_id)
c.remove_instance(inst_id) # remove instance zero
comps[1].add_instance(xform) # replace component with another

The above code I can iterate on foliage systems and its components, in addition Move/Add/Remove. There are several foliage actors in a level. In the Foliage pane/browser I can’t find what components are associated with what Foliage Actor Instances in the World. Additionally when adding foliage manually to the system via Add Foliage Type this creates and Foliage Actor Instance in the level. Not sure if that is desired and not sure where to control that.

Programmatically, I am unable to do the following:

  • Add new Foliage Type to the foliage system. (Need strawman code if its even possible)
  • Get the name of the component, the c.get_name() gets FoliageInstancedStaticMeshComponent_9 vs the foliage type name that is in the content browser. This would help me figure out which assets to replace/shuffle.
  • Not sure which foliage layer is active. (Need strawman code if its even possible)
  • Get components that are associated with a Foliage Actor Instance (Need strawman code if its even possible)
  • Get components that are active. e.g. the little number in the thumb. (This could be a way to tell they are active)
    Thanks for any help in advance.
  • Add new Foliage Type to the foliage system.
    Only possible if the following is exposed InstancedFoliage_AddMesh in c++

  • Get the name of the component, the c.get_name() gets FoliageInstancedStaticMeshComponent_9 vs the foliage type name that is in the content browser. This would help me figure out which assets to replace/shuffle.
    Only possible if some of the private reverse look up methods are exposed in c++

  • Get components that are associated with a Foliage Actor Instance

fols = unreal.GameplayStatics.get_all_actors_of_class(unreal.EditorLevelLibrary.get_editor_world(), unreal.InstancedFoliageActor)
comps = fols[0].get_components_by_class(unreal.HierarchicalInstancedStaticMeshComponent)
comps[0].get_path_name()
# returns '/Game/Map/Lvl_Foliage.Lvl_Foliage:PersistentLevel.InstancedFoliageActor_0.FoliageInstancedStaticMeshComponent_9'

from there parse the string

  • Get components that are active. e.g. the little number in the thumb.
from pathlib import Path
levels = unreal.EditorLevelUtils.get_levels(unreal.EditorLevelLibrary.get_editor_world())
name = Path(levels[2].get_path_name()).with_suffix('').name
# set the foliage level to be active
unreal.EditorLevelLibrary.set_current_level_by_name(name)

Basically activating the level in Level window.