Reputation: 268
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')
# can 'load' the asset, but this does not seem to be cast to 'object' type.
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)
Upvotes: 0
Views: 2814
Reputation: 268
I feel somewhat confused - after trying yesterday to make this work without success, I tried to use the loaded building mesh asset, and it somehow worked this time:
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)
I'm not sure why this wasn't working before - as I thought the loaded asset was an object (but it didn't work before).
Upvotes: 1