6

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?

kakyo
  • 10,460
  • 14
  • 76
  • 140

3 Answers3

5

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)
kakyo
  • 10,460
  • 14
  • 76
  • 140
2

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

siditious
  • 71
  • 8
0

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.

quimnuss
  • 1,503
  • 2
  • 17
  • 37