How to create static mesh actors in level using Python Script

Background: I am trying to learn how to automatically generate an environment in UE4 using Python (and Blueprints). I have installed plug-in. Using UE4.25, with python 2.7 (as per python scripting plug-in). I’m not that familiar with using scripting languages - and have been doing most work in blueprints. I’ve successfully added a test fbx/static mesh into my contents directory using Python - and have been able to spawn a generic (empty) static mesh actor into the level using Python.

Goal: I am trying use Python to automate the generation of simple to complex patterns of assets/static meshes across a simple level - for automatic scenario generation driven by a Python backend. I only want this for editor work - not real-time (a later goal).

Problem: the only way to spawn seems to be using the EditorLevelLibrary.spawn_actor_from_class or EditorLevelLibrary.spawn_actor_from_object command. However, I can’t use the “static mesh” directly as it is neither a class nor object. I don’t know how to re-cast asset into the correct form. I tried generating a generic Static Mesh Actor (succeeded), but then can’t assign the Static Mesh as this seems to be a read-only property, and there didn’t seem to be any set_static_mesh functions. I’m sure there is an easy way to cast these things - but the documentation is horrendous.

Question: What is the best way to take an existing static mesh and to generate a number of static mesh actors into the level using the inbuilt Python Scripting plug-in.

import unreal

# Filename paths
staticmesh_fbx = "D:\Data\Unreal Projects\Assets\house_w_collision_3.fbx"
    
def spawnActor():
# Purpose to auto-populate a single (or set) of static meshes in a level     
    
    building_mesh = unreal.load_asset('/Game/StaticMeshes/house_w_collision_3.house_w_collision_3')
    
    actor_class = unreal.StaticMeshActor
    actor_location = unreal.Vector(0.0,0.0,0.0)
    actor_rotation = unreal.Rotator(0.0,0.0,0.0)
    
    test_actor = unreal.EditorLevelLibrary.spawn_actor_from_class(actor_class, actor_location, actor_rotation)
1 Like

Not sure why - but my previous attempts to use the loaded asset as the source for the spawn actor from object wasn’t working - but now it did. The following works correctly to spawn the static mesh into a static mesh actor in the level at a given location:

import unreal

# Filename paths
staticmesh_fbx = "D:\Data\Unreal Projects\Assets\house_w_collision_3.fbx"

def spawnActor():
   obj = unreal.load_asset('/Game/StaticMeshes/house_w_collision_3.house_w_collision_3')
  actor_location = unreal.Vector(0.0,0.0,0.0)
  actor_rotation = unreal.Rotator(0.0,0.0,0.0)
  unreal.EditorLevelLibrary.spawn_actor_from_object(obj, actor_location, actor_rotation)
1 Like

Sorry - realised that I couldn’t “resolve” this using just a comment.

The following works correctly to spawn the static mesh into a static mesh actor in the level at a given location:

import unreal
 
 # Filename paths
 staticmesh_fbx = "D:\Data\Unreal Projects\Assets\house_w_collision_3.fbx"
 
 def spawnActor():
    obj = unreal.load_asset('/Game/StaticMeshes/house_w_collision_3.house_w_collision_3')
   actor_location = unreal.Vector(0.0,0.0,0.0)
   actor_rotation = unreal.Rotator(0.0,0.0,0.0)
   unreal.EditorLevelLibrary.spawn_actor_from_object(obj, actor_location, actor_rotation)
1 Like

module ‘unreal’ has no attribute ‘EditorLevelLibrary’

1 Like