How do I make the Persistent Level become current after adding a sublevel with python?

Ultimately, all I really want to do is make a function that will add a sublevel to a given level. However, after adding the sublevel, I can’t find a way to make the Persistent Level current so I can save out the changes – the added sublevel remains current, and the level to which it belongs remains unsaved.


def AddSubLevelToLevel(parent_asset_path=None, sublevel_asset_path=None):
     if parent_asset_path is None or sublevel_asset_path is None:
          print("Fail")
          return

     if unreal.EditorAssetLibrary.does_asset_exist(parent_asset_path) is False:
          unreal.EditorLevelLibrary.new_level(parent_asset_path)

     else:
          unreal.EditorLevelLibrary.load_level(parent_level_asset_path)

     #make the sublevel
     sublevel = unreal.EditorLevelUtils.create_new_streaming_level(unreal.LevelStreamingDynamic, new_level_path=sub_level_asset_path)

     # At this point, sublevel is selected as current in the Levels panel.  If I call unreal.EditorLevelLibrary.save_current_level(),
     # the sublevel will be saved.  How can I get Persistent Level be set to current in the levels panel so I can
     # save it?  The next time this function is called, I need the sublevel to be added to Persistent Level and not the sublevel I just added.

I’ve been going round and round in this python API trying to find the right way to do it. I know that I can gain access to StreamingLevelPersistent if I’m making a new one, but I’m not – I need the one that is loaded and exists so I can call make_level_current() on it. Can anyone out there help me get what I’m looking for? Thanks in advance for the help.

Alright, I found something that works. I don’t necessarily like what I need to do to make it work, but it works. Continuing from the code above:


#Save all open levels -- this will get the persistent level to save
unreal.EditorLevelLibrary.save_all_dirty_levels();

#Reload parent_asset_path -- this makes it so that Persistent Level is the one set to be current again.
unreal.EditorLevelLibrary.load_level(parent_asset_path)

So, each time I call the function now, a new sublevel is added to the Peristent Level. If someone knows of a cleaner way to do it, lemme know.

1 Like

Ok, so even though I’ve solved my problem, I have a new one: Although all of the actors in my sublevel appear in the world outliner, none of them are visible in the viewport. I changed the sublevel from being unreal.LevelStreamingDynamic to unreal.LevelStreamingAlwaysLoaded thinking that may be the issue, but it had no effect. I can double-click the actors in the outline, and my camera moves to positions in the viewport, and I get axis handles, but nothing is visible. Even from the Levels panel, I can’t change the visibility. Only at runtime does any of the geometry show up. Why does none of it show up in the editor? Is there something I specifically I need to do that I’m missing in order to add my sublevels AND make its content visible in the editor?

If I open up the level asset for the sublevel on its own, all of the geometry is visible. It’s only when I open the level that has this level as its sublevel that its content doesn’t appear. What am I missing?

It seems like you have nested sublevels, which are not supported. So if you have 2 levels deep of sublevels, they will be ignored.

Nope, no nested sublevels. I have the Persistent Level with one sublevel under it. That was my test, and none of the objects in that sublevel are visible.

I tried this and it seems to work for me, is this what you are looking for ?

makeCurLev = unreal.EditorLevelLibrary.set_current_level_by_name(‘empty_Pee_p’)

‘empty_Pee_p’ is the name of my persistent level

makeCurLev = unreal.EditorLevelLibrary.set_current_level_by_name(‘rig2’)

‘rig2’ is another level/map that drag/dropped in from the content browser , under the ‘empty_Pee_p’ level