How do I spawn an actor to the level with the usage of the Python scripting plugin that comes built-in with Unreal Engine >= 4.19? (That is, without using Unreal Studio) Is it even possible outside of Unreal Studio? Why does Unreal Studio even have more Python scripting functionality than Unreal Engine?
The first thing to mention here is that Python is only for use within the Editor to help automate activities, but it can’t be used during ‘play’ time. So, if you want to place an actor when you’re creating a scene / world then you can use Python, but if you want to do it at runtime (i.e. press play, do something and have an actor spawn), then you can not. More information here: https://docs.unrealengine.com/en-us/Studio/Python
I’ve never done this, but a quick look suggests that the examples here will help:
More specifically, this example which deals with spawning
I only want to spawn actors while editing, so before I press play. I’ve already checked those links before and as you can see from your last link it is an Unreal Studio example. Those functions don’t exist outside of Unreal Studio, they don’t exist in base Unreal Engine. I’ll edit my question to make it more clear that I want to know how to spawn actors without using Unreal Studio.
You’ll probably want to enable the Editor Scripting Utilities Plugin, then call
unreal.EditorLevelLibrary.spawn_actor_from_class()
That Editor Scripting plugin had been only available to Unreal Studio users in 4.19, but now is for everyone.
Thanks Ro-Su-.
Here is a quick tutorial I found: UE 4.21 - How To Spawn Actors In World With Python - YouTube
great! thank u soooo much!
Link is broken per 20220928.
Seems the Editor Scripting plugin is deprecated in newer versions of the engine. The new method for spawning an actor via python is as follows.
import unreal
# Get a class for spawning
my_class = unreal.EditorAssetLibrary.load_blueprint_class('/Game/Project/Path/To/Blueprint.Blueprint')
# Get the editor actor subsystem
actor_subsys = unreal.get_editor_subsystem(unreal.EditorActorSubsystem)
# Actually spawn the actor
spawn_location = unreal.Vector(0, 0, 0)
inst = actor_subsys.spawn_actor_from_class(my_class, spawn_location)
Hey this helped me @TheGreenMat . Thank you!
It seems you described the same function as like that:
new_act = unreal.EditorLevelLibrary.spawn_actor_from_class(my_class, pos, rot)
Unfortunately, this function is slow in UE5 and very slow in UE4. I spawned 350 actors only and UE4 took about 5-7 seconds to finish spawning. I’m very disappointed.
I found that this BP is very fast. But I cannot find how to run it in Python.