kakyo
kakyo

Reputation: 11590

Spawn actor from class in Unreal Engine using Python

Using Blueprint, I can SpawnActorFromClass with a StaticMeshActor, but with a Python script via the builtin Python Script plugin,

unreal.EditorLevelLibrary().spawn_actor_from_class(ue.Class(name='StaticMeshActor'), location, rot)

I got:

LogPython: Error: TypeError: EditorLevelLibrary: Failed to convert parameter 'actor_class' when calling function 'EditorLevelLibrary.SpawnActorFromClass' on 'Default__EditorLevelLibrary'
LogPython: Error:   TypeError: NativizeProperty: Cannot nativize 'Class' as 'ActorClass' (ClassProperty)
LogPython: Error:     TypeError: NativizeClass: Cannot nativize 'Class' as 'Class' (allowed Class type: 'Actor')

What am I missing?

Upvotes: 6

Views: 4186

Answers (3)

quimnuss
quimnuss

Reputation: 1561

In case of any blueprint actor in ue5.0 is

unreal.EditorLevelLibrary().spawn_actor_from_class(unreal.EditorAssetLibrary.load_blueprint_class('/Game/TopDown/Actors/WinningHeart'), location, rot)

Where you can get the class path by hovering on the asset on the content drawer or right-click -> Copy Reference.

In my case it was a blueprint actor called 'WinningHeart'. location and rot are unreal.Vector and unreal.Rotator respectively.

Upvotes: 0

siditious
siditious

Reputation: 81

I'm not sure if you're using the Python plugin by 20tab or not, but you can accomplish this quite easily from the in editor console, or even at runtime using the following code sample

def spawn(cls):
    ue.editor_deselect_actors()
    obj = ue.get_editor_world().actor_spawn(cls)
    ue.editor_select_actor(obj)
    return obj
__builtins__['spawn'] = spawn # so it's always available in the Py console

The plugin is available for free at https://github.com/20tab/UnrealEnginePython and currently supports up through version 4.22

Upvotes: 2

kakyo
kakyo

Reputation: 11590

Figured this out by myself. Turns out that the .spawn_actor_from_class() call does not accept ue.Class. Instead it receives socalled ClassProperty derived from built-in types. So the correct call should be:

unreal.EditorLevelLibrary().spawn_actor_from_class(ue.StaticMeshActor.static_class(), location, rot)

Upvotes: 5

Related Questions