20tab / UnrealEnginePython

Embed Python in Unreal Engine 4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to spawn Actor into editor world.

AgenteDog opened this issue · comments

Hey, so I have been trying to place a static mesh into an editor world with this code:

import unreal_engine as ue
from unreal_engine.classes import Blueprint
from unreal_engine import FVector, FRotator
world = ue.get_editor_world()
blueprint = ue.load_object("StaticMesh'", '/Game/CoD/xmodels/foliage_desertbrush_1_LOD0.foliage_desertbrush_1_LOD0')
actor000 = world.actor_spawn(Actor, FVector(0, 0, 0), FRotator(0, 0, 0))

But I am getting this error

LogPython: Error: argument is not a UObject
LogPython: Error: Traceback (most recent call last):
LogPython: Error:   File "<string>", line 7, in <module>
LogPython: Error: Exception: argument is not a UObject

Does anyone know a fix?

As you've spotted, only subclasses of Actor can be loaded into the scene.
I don't think that "Actor" a valid class reference as you've used it here.

The way I'd recommend doing it is by creating a new uclass inheriting from actor, and then applying the static mesh to that. This is easiest to do from the editor, but can be done from python if you really really have to.

Once you've made your class, just load that with ue.find_class("YourClass")

Hey, so my goal here is to import hundreds of assets and place them in the scene. I am not sure because I am kind of a noob but can UClasses be created with python itself? I already have a script working with the native python integration that comes with 4.21, but, I need to make the script into an Unreal Engine plugin, and from what I have investigated, you can't do that with that python integration.

When you drag a static mesh into the scene, it actually causes a static mesh actor to be spawned:
image

If you click on it, you'll notice it has a static mesh component. So, from Python you need to spawn the same actor, access that component, and set its mesh:

from unreal_engine.classes import StaticMeshActor, StaticMesh
mesh= ue.load_object(StaticMesh, '/Game/CoD/xmodels/foliage_desertbrush_1_LOD0.foliage_desertbrush_1_LOD0')
obj = ue.get_editor_world().actor_spawn(StaticMeshActor)
obj.StaticMeshComponent.StaticMesh = mesh

Thanks!

Hey
Is it possible to load unreal uasset from outside the unreal project?
For Example:-
ue.load_object(StaticMesh, 'S:/server/test_show/assets/props/colorCalibrator/SM_ColorCalibrator')

My unreal project is in my local "d:/my_project/"