Create Level

How to create Level with Python Code? Thanks alot

Simple example:

levelName = ‘test1’
levelLibrary = unreal.EditorLevelLibrary()
success = levelLibrary.new_level(’/Game/Levels/{}’.format(levelName))

Thanks alot. It works very good. Doy you know how can i load this level if i am in another level? Is there tutorial for python with script?

thanks alot i ot it:
import unreal
levelName = ‘test1’
levelLibrary = unreal.EditorLevelLibrary()
success = levelLibrary.load_level(’/Game/Levels/{}’.format(levelName))

hi! did you try to load then another level (f.e. several selected levels in CB in a loop). In my case - i’m experiencing constant crashes on loading the second item. The first level loads successfully, but the second attempt - crashes…

load_level is a class method, so you don’t need to create an instance to call it and can just do unreal.EditorLevelLibrary.load_level(…)

What does your log say the crash is? I fixed something for 4.22 where Python references to objects in a sub-level weren’t being cleared when switching worlds, which caused a fatal GC validation error.

Hello, I’m using 4.21. For first maps selection all works well, but if you try then run this script on another set of maps - then engine crashes.



import unreal  
## selecting levels from CB
@unreal.uclass()
class MyEditorUtility(unreal.GlobalEditorUtilityBase):
      pass  

editor_util = MyEditorUtility()
evels = editor_util.get_selected_assets()

for level in levels:
      level_path_name = level.get_path_name().split(':')[0].split('.')[0]
      unreal.EditorLevelLibrary.load_level(level.get_path_name())
      unreal.EditorLoadingAndSavingUtils.save_map(level.get_world(), level_path_name)  


And here is log.

Access violation - code c0000005 (first/second chance not available)

UE4Editor_CoreUObject!UClass::GetDefaultObject() [d:\build++ue4\sync\engine\source\runtime\coreuobject\public\uobject\class.h:2435]
UE4Editor_HotReload!FHotReloadClassReinstancer::SetupNewClassReinstancing() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreloadclassreinstancer.cpp:21]
UE4Editor_HotReload!FHotReloadClassReinstancer::FHotReloadClassReinstancer() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreloadclassreinstancer.cpp:308]
UE4Editor_HotReload!FHotReloadClassReinstancer::Create() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreloadclassreinstancer.h:120]
UE4Editor_HotReload!FHotReloadModule::ReinstanceClass() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreload.cpp:1255]
UE4Editor_HotReload!FHotReloadModule::ReinstanceClasses() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreload.cpp:1242]
UE4Editor_HotReload!TBaseRawMethodDelegateInstance<0,FHotReloadModule,void __cdecl(void)>::ExecuteIfSafe() [d:\build++ue4\sync\engine\source\runtime\core\public\delegates\delegateinstancesimpl.h:518]
UE4Editor_PythonScriptPlugin!FPyWrapperTypeReinstancer::ProcessPending() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pywrappertyperegistry.cpp:375]
UE4Editor_PythonScriptPlugin!FPythonScriptPlugin::RunFile() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pythonscriptplugin.cpp:874]
UE4Editor_PythonScriptPlugin!FPythonScriptPlugin::HandlePythonExecCommand() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pythonscriptplugin.cpp:736]
UE4Editor_PythonScriptPlugin!FPythonScriptPlugin::Exec() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pythonscriptplugin.cpp:413]
UE4Editor_Core!FSelfRegisteringExec::StaticExec() [d:\build++ue4\sync\engine\source\runtime\core\private\misc\coremisc.cpp:55]
UE4Editor_CoreUObject!StaticExec() [d:\build++ue4\sync\engine\source\runtime\coreuobject\private\uobject\obj.cpp:4161]
UE4Editor_Engine!UEngine::Exec() [d:\build++ue4\sync\engine\source\runtime\engine\private\unrealengine.cpp:3619]
UE4Editor_UnrealEd!UEditorEngine::Exec() [d:\build++ue4\sync\engine\source\editor\unrealed\private\editorserver.cpp:5715]
UE4Editor_UnrealEd!UUnrealEdEngine::Exec() [d:\build++ue4\sync\engine\source\editor\unrealed\private\unrealedsrv.cpp:691]
UE4Editor_UnrealEd!FConsoleCommandExecutor::Exec() [d:\build++ue4\sync\engine\source\editor\unrealed\private oolkits\fconsolecommandexecutor.cpp:118]
UE4Editor_OutputLog!SConsoleInputBox::OnTextCommitted() [d:\build++ue4\sync\engine\source\developer\outputlog\private\soutputlog.cpp:278]
UE4Editor_OutputLog!TBaseSPMethodDelegateInstance<0,SConsoleInputBox,0,void __cdecl(FText const & __ptr64,enum ETextCommit::Type)>::ExecuteIfSafe() [d:\build++ue4\sync\engine\source\runtime\core\public\delegates\delegateinstancesimpl.h:355]
UE4Editor_Slate!SMultiLineEditableText::OnTextCommitted() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\smultilineeditabletext.cpp:371]
UE4Editor_Slate!FSlateEditableTextLayout::HandleCarriageReturn() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\slateeditabletextlayout.cpp:1455]
UE4Editor_Slate!FSlateEditableTextLayout::HandleKeyDown() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\slateeditabletextlayout.cpp:928]
UE4Editor_Slate!SMultiLineEditableText::OnKeyDown() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\smultilineeditabletext.cpp:607]
UE4Editor_Slate!FEventRouter::Route<FReply,FEventRouter::FBubblePolicy,FKeyEvent,<lambda_2afcd964c763e269e2d1a63f3a72f846> >() [d:\build++ue4\sync\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:270]
UE4Editor_Slate!FSlateApplication::ProcessKeyDownEvent() [d:\build++ue4\sync\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:5097]
UE4Editor_Slate!FSlateApplication::OnKeyDown() [d:\build++ue4\sync\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:5009]
UE4Editor_ApplicationCore!FWindowsApplication::ProcessDeferredMessage() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:1666]
UE4Editor_ApplicationCore!FWindowsApplication::DeferMessage() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:2281]
UE4Editor_ApplicationCore!FWindowsApplication::ProcessMessage() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:929]
UE4Editor_ApplicationCore!FWindowsApplication::AppWndProc() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:766]
user32
user32
UE4Editor_ApplicationCore!FWindowsPlatformApplicationMisc::PumpMessages() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsplatformapplicationmisc.cpp:130]
UE4Editor!FEngineLoop::Tick() [d:\build++ue4\sync\engine\source\runtime\launch\private\launchengineloop.cpp:3615]
UE4Editor!GuardedMain() [d:\build++ue4\sync\engine\source\runtime\launch\private\launch.cpp:174]
UE4Editor!GuardedMainWrapper() [d:\build++ue4\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:145]
UE4Editor!WinMain() [d:\build++ue4\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:276]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:283]
kernel32
ntdll

@Wizard3d Ah, it’s crashing creating the CDO for your MyEditorUtility class the second time around.

I’m not at my desk to test, so this may make no difference, but does this change help at all?


# before
editor_util = MyEditorUtility()
levels = editor_util.get_selected_assets()

# after
levels = MyEditorUtility.get_default_object().get_selected_assets()

@**Jamie Dale **The crash is still occurs on second set of maps

Access violation - code c0000005 (first/second chance not available)

UE4Editor_CoreUObject!UClass::GetDefaultObject() [d:\build++ue4\sync\engine\source\runtime\coreuobject\public\uobject\class.h:2435]
UE4Editor_HotReload!FHotReloadClassReinstancer::SetupNewClassReinstancing() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreloadclassreinstancer.cpp:21]
UE4Editor_HotReload!FHotReloadClassReinstancer::FHotReloadClassReinstancer() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreloadclassreinstancer.cpp:308]
UE4Editor_HotReload!FHotReloadClassReinstancer::Create() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreloadclassreinstancer.h:120]
UE4Editor_HotReload!FHotReloadModule::ReinstanceClass() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreload.cpp:1255]
UE4Editor_HotReload!FHotReloadModule::ReinstanceClasses() [d:\build++ue4\sync\engine\source\developer\hotreload\private\hotreload.cpp:1242]
UE4Editor_HotReload!TBaseRawMethodDelegateInstance<0,FHotReloadModule,void __cdecl(void)>::ExecuteIfSafe() [d:\build++ue4\sync\engine\source\runtime\core\public\delegates\delegateinstancesimpl.h:518]
UE4Editor_PythonScriptPlugin!FPyWrapperTypeReinstancer::ProcessPending() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pywrappertyperegistry.cpp:375]
UE4Editor_PythonScriptPlugin!FPythonScriptPlugin::RunFile() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pythonscriptplugin.cpp:874]
UE4Editor_PythonScriptPlugin!FPythonScriptPlugin::HandlePythonExecCommand() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pythonscriptplugin.cpp:736]
UE4Editor_PythonScriptPlugin!FPythonScriptPlugin::Exec() [d:\build++ue4\sync\engine\plugins\experimental\pythonscriptplugin\source\pythonscriptplugin\private\pythonscriptplugin.cpp:413]
UE4Editor_Core!FSelfRegisteringExec::StaticExec() [d:\build++ue4\sync\engine\source\runtime\core\private\misc\coremisc.cpp:55]
UE4Editor_CoreUObject!StaticExec() [d:\build++ue4\sync\engine\source\runtime\coreuobject\private\uobject\obj.cpp:4161]
UE4Editor_Engine!UEngine::Exec() [d:\build++ue4\sync\engine\source\runtime\engine\private\unrealengine.cpp:3619]
UE4Editor_UnrealEd!UEditorEngine::Exec() [d:\build++ue4\sync\engine\source\editor\unrealed\private\editorserver.cpp:5715]
UE4Editor_UnrealEd!UUnrealEdEngine::Exec() [d:\build++ue4\sync\engine\source\editor\unrealed\private\unrealedsrv.cpp:691]
UE4Editor_UnrealEd!FConsoleCommandExecutor::Exec() [d:\build++ue4\sync\engine\source\editor\unrealed\private oolkits\fconsolecommandexecutor.cpp:118]
UE4Editor_OutputLog!SConsoleInputBox::OnTextCommitted() [d:\build++ue4\sync\engine\source\developer\outputlog\private\soutputlog.cpp:278]
UE4Editor_OutputLog!TBaseSPMethodDelegateInstance<0,SConsoleInputBox,0,void __cdecl(FText const & __ptr64,enum ETextCommit::Type)>::ExecuteIfSafe() [d:\build++ue4\sync\engine\source\runtime\core\public\delegates\delegateinstancesimpl.h:355]
UE4Editor_Slate!SMultiLineEditableText::OnTextCommitted() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\smultilineeditabletext.cpp:371]
UE4Editor_Slate!FSlateEditableTextLayout::HandleCarriageReturn() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\slateeditabletextlayout.cpp:1455]
UE4Editor_Slate!FSlateEditableTextLayout::HandleKeyDown() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\slateeditabletextlayout.cpp:928]
UE4Editor_Slate!SMultiLineEditableText::OnKeyDown() [d:\build++ue4\sync\engine\source\runtime\slate\private\widgets ext\smultilineeditabletext.cpp:607]
UE4Editor_Slate!FEventRouter::Route<FReply,FEventRouter::FBubblePolicy,FKeyEvent,<lambda_2afcd964c763e269e2d1a63f3a72f846> >() [d:\build++ue4\sync\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:270]
UE4Editor_Slate!FSlateApplication::ProcessKeyDownEvent() [d:\build++ue4\sync\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:5097]
UE4Editor_Slate!FSlateApplication::OnKeyDown() [d:\build++ue4\sync\engine\source\runtime\slate\private\framework\application\slateapplication.cpp:5009]
UE4Editor_ApplicationCore!FWindowsApplication::ProcessDeferredMessage() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:1666]
UE4Editor_ApplicationCore!FWindowsApplication::DeferMessage() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:2281]
UE4Editor_ApplicationCore!FWindowsApplication::ProcessMessage() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:929]
UE4Editor_ApplicationCore!FWindowsApplication::AppWndProc() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsapplication.cpp:766]
user32
user32
UE4Editor_ApplicationCore!FWindowsPlatformApplicationMisc::PumpMessages() [d:\build++ue4\sync\engine\source\runtime\applicationcore\private\windows\windowsplatformapplicationmisc.cpp:130]
UE4Editor!FEngineLoop::Tick() [d:\build++ue4\sync\engine\source\runtime\launch\private\launchengineloop.cpp:3615]
UE4Editor!GuardedMain() [d:\build++ue4\sync\engine\source\runtime\launch\private\launch.cpp:174]
UE4Editor!GuardedMainWrapper() [d:\build++ue4\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:145]
UE4Editor!WinMain() [d:\build++ue4\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:276]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:283]
kernel32
ntdll

there is


unreal.GlobalEditorUtilityBase.get_selected_assets()

isn’t it supposed to Get from CB? and what arg it awaits, there isn’t anything in docs.

LogPython: Error: levels = unreal.GlobalEditorUtilityBase.get_selected_assets()
LogPython: Error: TypeError: descriptor ‘get_selected_assets’ of ‘GlobalEditorUtilityBase’ object needs an argument

It’s not a class method, so it needs to be called on an instance, not the type itself.

Your crash also seems to be fixed in 4.22.

Ok, thanks, it’s good to know. It’s left to wait for plugins to catch up with UE version :frowning: