# How to add component to existing actor in level with Python/Blueprint?

**URL:** <https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849>\
**Category:** Editor Scripting\
**Tags:** Editor-Scripting, unreal-engine, UE4, question\
**Created:** [January 3, 2020, 2:47pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849 "2020-01-03T14:47:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![YanDaik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@YanDaik](https://forums.unrealengine.com/u/YanDaik)\
**Post date:** [January 3, 2020, 2:47pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/1 "2020-01-03T14:47:08Z")

</div>

In the level editor in the Details panel there is a button “Add Component”. And you can add any component to an existing actor.

But how can you add a component (StaticMeshComponent) to actor with a Python or Blueprint Script or maybe c++?

Cant find anything in references. It looks like it should be implemented.

---

<div class="post-metadata">

**Author:** ![Jamie\_Dale](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jamie_dale/32/1165413_2.png) [@Jamie\_Dale](https://forums.unrealengine.com/u/Jamie_Dale)\
**Post date:** [January 6, 2020, 8:22pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/2 "2020-01-06T20:22:29Z")

</div>

Unfortunately a lot of the logic needed to correctly add an instanced component to an actor (both in a level and in a BP) for the editor is currently tied to the UI flow itself.

We have an open issue (UE-64131) to untangle that logic so it can be shared by the UI and scripting, however I can’t say at this point when that will happen.

If you’re curious about investigating more yourself, then SSCSEditor is the class that handles the UI. In this case a subset of the code dealing with the EComponentEditorMode::ActorInstance path of SSCSEditor::AddNewComponent will likely be required.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b850915b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b850915b](https://forums.unrealengine.com/u/anonymous_user_b850915b)\
**Post date:** [March 3, 2020, 9:12pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/3 "2020-03-03T21:12:19Z")

</div>

I’m trying to wrap my head around all of this. So in order to get that functionality we would need to write some c++ code to implement that feature? Currently the SSCSEditor is currently not available to BP or python?

---

<div class="post-metadata">

**Author:** ![00Silver](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@00Silver](https://forums.unrealengine.com/u/00Silver)\
**Post date:** [March 9, 2020, 6:24am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/4 "2020-03-09T06:24:33Z")

</div>

I’m also trying to understand the best way to get this done. Can adding a component to existing actor or creating a fresh actor with components in level be done with c++ if not with python? And would I follow this order to do it :

1. **World-\>SpawnActor\<AActor\>(Class, Transform, SpawnParameters)**
2. **Actor-\>PostSpawnInitialize(Transform, Actor, Pawn, bRemoteOwned, bNoFail, bDeferConstruction)**
3. **Actor-\>PostActorCreated()** or **PostLoad()**
4. **ActorComponent-\>OnComponentCreated()**
5. **Actor-\>PreInitializeComponents()**
6. **ActorComponent-\>InitializeComponent()**
7. **Actor-\>PostInitializeComponents()**
8. \*\*OnActorSpawned() ??? \*\*( Not sure what this is or if it is important )
9. **BeginPlay() ???**

If so could I just UFunction these in a custom cpp and h files, compile them to make them accessible in python?

---

<div class="post-metadata">

**Author:** ![YanDaik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@YanDaik](https://forums.unrealengine.com/u/YanDaik)\
**Post date:** [March 12, 2020, 11:21am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/5 "2020-03-12T11:21:18Z")

</div>

I made this function and it works fine:

```auto

//You pass to it class of component, otherwise it creates StaticMeshComponent
UActorComponent* UBlackMesaLifeBlueprintLibrary::AddComponent(AActor* a,USceneComponent *future_parent, FName name, UStaticMesh* mesh, UClass* NewComponentClass)
{

    UActorComponent* retComponent = 0;
    if (!NewComponentClass)
    {
        //DuplicateObject<UStaticMeshComponent>(dup_source,a,name)
        UStaticMeshComponent* NewComponent = NewObject<UStaticMeshComponent>(a, name);// , EObjectFlags::RF_DefaultSubObject);
        //UStaticMeshComponent* NewComponent = DuplicateObject<UStaticMeshComponent>(dup_source, a, name/*ActorCmp, NewActor, NewName*/);
        //a->AddOwnedComponent(NewComponent);
        FTransform CmpTransform;// = dup_source->GetComponentToWorld();
        NewComponent->SetStaticMesh(mesh);
        NewComponent->AttachToComponent(future_parent, FAttachmentTransformRules::KeepWorldTransform);
        NewComponent->SetComponentToWorld(CmpTransform);
        a->AddInstanceComponent(NewComponent);
        NewComponent->OnComponentCreated();
        NewComponent->RegisterComponent();

        a->RerunConstructionScripts();
        retComponent = NewComponent;

    }
    else {
        UActorComponent* NewComponent = NewObject<UActorComponent>(a, NewComponentClass, name);
        FTransform CmpTransform;// = dup_source->GetComponentToWorld();
        //NewComponent->AttachToComponent(sc, FAttachmentTransformRules::KeepWorldTransform);
        // Do Scene Attachment if this new Comnponent is a USceneComponent
        if (USceneComponent* NewSceneComponent = Cast<USceneComponent>(NewComponent))
        {
            if(future_parent !=0)
                NewSceneComponent->AttachToComponent(future_parent, FAttachmentTransformRules::KeepWorldTransform);
            else
                NewSceneComponent->AttachToComponent(a->GetRootComponent(), FAttachmentTransformRules::KeepWorldTransform);

            NewSceneComponent->SetComponentToWorld(CmpTransform);
        }
        a->AddInstanceComponent(NewComponent);
        NewComponent->OnComponentCreated();
        NewComponent->RegisterComponent();

        a->RerunConstructionScripts();
        retComponent = NewComponent;
    }

    return retComponent;
}

```

---

<div class="post-metadata">

**Author:** ![YanDaik](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@YanDaik](https://forums.unrealengine.com/u/YanDaik)\
**Post date:** [March 12, 2020, 11:27am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/6 "2020-03-12T11:27:18Z")

</div>

> [@Jamie\_Dale](#):
>
> Unfortunately a lot of the logic needed to correctly add an instanced component to an actor

What milestones has this code? Is it incorrect? I had no problems so far with it.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b850915b](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b850915b](https://forums.unrealengine.com/u/anonymous_user_b850915b)\
**Post date:** [March 16, 2020, 6:18pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/7 "2020-03-16T18:18:46Z")

</div>

Hey YanDaik,

Thanks for sharing that snippet of code.

---

<div class="post-metadata">

**Author:** ![eobet](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/eobet/32/465239_2.png) [@eobet](https://forums.unrealengine.com/u/eobet)\
**Post date:** [April 12, 2020, 11:18am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/8 "2020-04-12T11:18:38Z")

</div>

> [@Jamie\_Dale](#):
>
> Unfortunately a lot of the logic needed to correctly add an instanced component to an actor (both in a level and in a BP) for the editor is currently tied to the UI flow itself.
> 
> We have an open issue (UE-64131) to untangle that logic so it can be shared by the UI and scripting, however I can’t say at this point when that will happen.
> 
> If you’re curious about investigating more yourself, then SSCSEditor is the class that handles the UI. In this case a subset of the code dealing with the EComponentEditorMode::ActorInstance path of SSCSEditor::AddNewComponent will likely be required.

I have an editor utility widget that gets the current selection and does stuff to it, but it only works in the world outliner, not in a blueprint actor outliner. Is that the same issue as the one above?

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_b29fef431](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_b29fef431](https://forums.unrealengine.com/u/anonymous_user_b29fef431)\
**Post date:** [October 15, 2020, 8:26pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/9 "2020-10-15T20:26:01Z")

</div>

> [@YanDaik](#):
>
> I made this function and it works fine:
> 
> ```auto
> 
> //You pass to it class of component, otherwise it creates StaticMeshComponent
> UActorComponent* UBlackMesaLifeBlueprintLibrary::AddComponent(AActor* a,USceneComponent *future_parent, FName name, UStaticMesh* mesh, UClass* NewComponentClass)
> {
> 
> UActorComponent* retComponent = 0;
> if (!NewComponentClass)
> {
> //DuplicateObject<UStaticMeshComponent>(dup_source,a,name)
> UStaticMeshComponent* NewComponent = NewObject<UStaticMeshComponent>(a, name);// , EObjectFlags::RF_DefaultSubObject);
> //UStaticMeshComponent* NewComponent = DuplicateObject<UStaticMeshComponent>(dup_source, a, name/*ActorCmp, NewActor, NewName*/);
> //a->AddOwnedComponent(NewComponent);
> FTransform CmpTransform;// = dup_source->GetComponentToWorld();
> NewComponent->SetStaticMesh(mesh);
> NewComponent->AttachToComponent(future_parent, FAttachmentTransformRules::KeepWorldTransform);
> NewComponent->SetComponentToWorld(CmpTransform);
> a->AddInstanceComponent(NewComponent);
> NewComponent->OnComponentCreated();
> NewComponent->RegisterComponent();
> 
> a->RerunConstructionScripts();
> retComponent = NewComponent;
> 
> }
> else {
> UActorComponent* NewComponent = NewObject<UActorComponent>(a, NewComponentClass, name);
> FTransform CmpTransform;// = dup_source->GetComponentToWorld();
> //NewComponent->AttachToComponent(sc, FAttachmentTransformRules::KeepWorldTransform);
> // Do Scene Attachment if this new Comnponent is a USceneComponent
> if (USceneComponent* NewSceneComponent = Cast<USceneComponent>(NewComponent))
> {
> if(future_parent !=0)
> NewSceneComponent->AttachToComponent(future_parent, FAttachmentTransformRules::KeepWorldTransform);
> else
> NewSceneComponent->AttachToComponent(a->GetRootComponent(), FAttachmentTransformRules::KeepWorldTransform);
> 
> NewSceneComponent->SetComponentToWorld(CmpTransform);
> }
> a->AddInstanceComponent(NewComponent);
> NewComponent->OnComponentCreated();
> NewComponent->RegisterComponent();
> 
> a->RerunConstructionScripts();
> retComponent = NewComponent;
> }
> 
> return retComponent;
> }
> 
> ```

Thanks for posting this YanDaik, just what I needed to get started with adding a component to an actor. I changed a few bits to suit my own needs but the overall idea works perfectly. I’m sure you probably know this, but if not, a small tip that maybe useful: instead of using UClass\* as a parameter, use TSubclassOf\<UActorComponent\> . This will enforce that the class chosen will be derived from UActorComponent and will limit the choices available in Blueprint. Thanks again

---

<div class="post-metadata">

**Author:** ![BenjaFriend22](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/benjafriend22/32/915021_2.png) [@BenjaFriend22](https://forums.unrealengine.com/u/BenjaFriend22)\
**Post date:** [April 5, 2022, 8:01pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/10 "2022-04-05T20:01:00Z")

</div>

With Unreal Engine 5.0 this functionality has been added with support for Blueprints and Python. Check out [this snippet](https://dev.epicgames.com/community/snippets/dV5/add-a-point-light-component-with-blueprint-script-in-the-editor) to see an example of adding a point light component to a blueprint and actors in the level.

---

<div class="post-metadata">

**Author:** ![Lukeha](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lukeha](https://forums.unrealengine.com/u/Lukeha)\
**Post date:** [May 18, 2022, 9:25am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/11 "2022-05-18T09:25:22Z")

</div>

Thanks Ben for the blueprint snippets - they work as expected 😃

But when I try convert the BP into python the component doesn’t seem to get attached. Can you tell what I’m doing wrong?

```python
so_subsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)  
for actor in unreal.VRScoutingInteractor.get_selected_actors():
    root_sub_object = so_subsystem.k2_gather_subobject_data_for_instance(actor)[0]
    new_sub_object = so_subsystem.add_new_subobject(unreal.AddNewSubobjectParams(
        parent_handle=root_sub_object,
        new_class=unreal.PointLightComponent,
    ))
    print(f"created {new_sub_object}")

# LogPython: created (<Struct 'SubobjectDataHandle' (0x000008ED0C7C12D0) {}>, Text(""))

```

---

<div class="post-metadata">

**Author:** ![flypopo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@flypopo](https://forums.unrealengine.com/u/flypopo)\
**Post date:** [August 13, 2022, 9:56am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/12 "2022-08-13T09:56:48Z")

</div>

> [@Lukeha](#):
>
> `AddNewSubobjectParams`

no use in 5.0.3

---

<div class="post-metadata">

**Author:** ![Solothek](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Solothek](https://forums.unrealengine.com/u/Solothek)\
**Post date:** [October 31, 2022, 3:12pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/13 "2022-10-31T15:12:54Z")

</div>

What do you mean by no use ? How would you add a Suboject in 5.0.3 ? I’m running into the same issue where subsystem.add\_new\_subobject(AddNewSubobjectParam()) does nothing…

---

<div class="post-metadata">

**Author:** ![Shawn\_Wsa](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shawn\_Wsa](https://forums.unrealengine.com/u/Shawn_Wsa)\
**Post date:** [May 13, 2023, 4:59am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/14 "2023-05-13T04:59:26Z")

</div>

@Lukeha is there any update for this Python problem?

---

<div class="post-metadata">

**Author:** ![Shawn\_Wsa](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Shawn\_Wsa](https://forums.unrealengine.com/u/Shawn_Wsa)\
**Post date:** [May 13, 2023, 4:59am UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/15 "2023-05-13T04:59:30Z")

</div>

@BenjaFriend22 Could you please give a example in Python scripts?

---

<div class="post-metadata">

**Author:** ![Lukeha](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Lukeha](https://forums.unrealengine.com/u/Lukeha)\
**Post date:** [May 18, 2023, 8:02pm UTC](https://forums.unrealengine.com/t/how-to-add-component-to-existing-actor-in-level-with-python-blueprint/135849/16 "2023-05-18T20:02:39Z")

</div>

> [@Lukeha](#):
>
> ```auto
> so_subsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)  
> for actor in unreal.VRScoutingInteractor.get_selected_actors():
> root_sub_object = so_subsystem.k2_gather_subobject_data_for_instance(actor)[0]
> new_sub_object = so_subsystem.add_new_subobject(unreal.AddNewSubobjectParams(
> parent_handle=root_sub_object,
> new_class=unreal.PointLightComponent,
> ))
> print(f"created {new_sub_object}")
> 
> ```

@Shawn_Wsa I think it was just a bug in 5.0. When I run the exact python snippet above in 5.2 the point light component is attached to the selected actor as expected
