editor - python script - how to add component to actor

Hi,

I can spawn actors to a level, but how do I attach components to an actor that is not instanced in the level

I mean with a python editor script

thanks

1 Like

Use the unreal. SubobjectDataSubsystem

root_data_handle: unreal.SubobjectDataHandle = 
         subsystem.k2_gather_subobject_data_for_blueprint(blueprint)[0]

sub_handle, fail_reason = subsystem.add_new_subobject(
        params=unreal.AddNewSubobjectParams(
            parent_handle=root_data_handle,
            new_class=new_class,
            blueprint_context=blueprint))

See Using Python to Create Unreal Engine Objects | unrealcode.net for an example

thanks

but it does not work with 4.27

I tried this

asset = unreal.load_asset("/Game/Vehicles/Truck/Truck.Truck")
actor = unreal.Actor.cast(asset)

the cast does not work
I want to modify that blueprint, not an instance of it

Assuming that /Game/Vehicles/Truck/Truck.Truck is a blueprint the call to load_asset() returns a thing of type unreal.Blueprint. This is not an actor. A blueprint is a thing which can be used to create an actor, but it is not itself an actor. So it cannot be cast to an actor.

If you type

print(asset)

You will see something like

LogPython: <Object '/Game/Vehicles/Truck/Truck.Truck' (0x000008FD25B46000) Class 'Blueprint'>

showing that asset is of type ‘Blueprint’, and if you type

help(unreal.Blueprint)

it shows the base classes of unreal.Blueprint are:
BlueprintCore
Object
_ObjectBase
_WrapperBase
object

These are the things asset can be cast to. Note that actor is not one of the base classes

ok but how do I modify the blueprint scenegraph ?

I want to

  • remove all static meshes components
  • add new ones

This article shows how to add new static mesh components: Using Python to Create Unreal Engine Objects | unrealcode.net

To delete a static mesh component use

unreal.SubobjectDataSubsystem.delete_subobject()