# "Add Component" by Python

**URL:** <https://forums.unrealengine.com/t/add-component-by-python/143662>\
**Category:** Editor Scripting\
**Tags:** unreal-engine\
**Created:** [May 15, 2020, 10:51am UTC](https://forums.unrealengine.com/t/add-component-by-python/143662 "2020-05-15T10:51:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_ec0f74c31](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ec0f74c31](https://forums.unrealengine.com/u/anonymous_user_ec0f74c31)\
**Post date:** [May 15, 2020, 10:51am UTC](https://forums.unrealengine.com/t/add-component-by-python/143662/1 "2020-05-15T10:51:15Z")

</div>

Hi.

I am trying to repeat a command that adds a component using the **“Add Component”** button.  
I need to create the same **ImagePlateComponent** for **SceneComponent** but with Python code.

![UE_AddComponent_ImagePlate.png](https://d3kjluh73b9h9o.cloudfront.net/original/3X/7/1/7134a36fd43adb927d725cd183a150026e9cbe53.png)

I execute code in Output Log:

```auto

scene_component = camera.scene_component
image_component = unreal.ImagePlateComponent()
image_component.k2_attach_to(scene_component, socket_name="None", attach_type=unreal.AttachLocation.KEEP_RELATIVE_OFFSET, weld_simulated_bodies=True)

```

But the **ImagePlateComponent** generated by the python code is different from the one created through the **“Add Component”** button:

```auto

for i in range(scene_component.get_num_children_components()):
    print scene_component.get_child_component(i)

LogPython: <Object '/Game/fbxLevel.fbxLevel:PersistentLevel.cam_for_UE.CameraComponent' (0x000001CA2F795600) Class 'CineCameraComponent'>
LogPython: <Object '/Game/fbxLevel.fbxLevel:PersistentLevel.cam_for_UE.ImagePlate' (0x000001CA2B87C100) Class 'ImagePlateComponent'>
LogPython: <Object '/Engine/Transient.ImagePlateComponent_0' (0x000001CA2B5B1D00) Class 'ImagePlateComponent'>

```

_- second line_: the component created by the **“Add Component”** button  
_- third line_: component created by python code

I do not see the **ImagePlateComponent** (that creates the python code) in the **Details** panel.

\*\*[unreal.Actor](https://docs.unrealengine.com/en-US/PythonAPI/class/Actor.html) \*\*article says that a new component must be registered. But how to do that?  
I can not find the required method ☹

How do I add an **ImagePlateComponent** so that it displays under the **SceneComponent** as attachement?

---

<div class="post-metadata">

**Author:** ![mamoniem](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mamoniem/32/1194698_2.png) [@mamoniem](https://forums.unrealengine.com/u/mamoniem)\
**Post date:** [May 24, 2020, 1:37pm UTC](https://forums.unrealengine.com/t/add-component-by-python/143662/2 "2020-05-24T13:37:39Z")

</div>

Yes you don’t see it, and you won’t see it. From the tiny piece of logic you shared, i see you creating a component without defining which actor to hold it, so you kinda end up with a component in the air…in the memory.

-m

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_ec0f74c31](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_ec0f74c31](https://forums.unrealengine.com/u/anonymous_user_ec0f74c31)\
**Post date:** [June 4, 2020, 8:05pm UTC](https://forums.unrealengine.com/t/add-component-by-python/143662/3 "2020-06-04T20:05:02Z")

</div>

> [@mamoniem](#):
>
> i see you creating a component without defining which actor to hold it

Thanks. Tell me please, How I can define this Actor?

---

<div class="post-metadata">

**Author:** ![Ore\_Creative](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Ore\_Creative](https://forums.unrealengine.com/u/Ore_Creative)\
**Post date:** [March 14, 2021, 12:37pm UTC](https://forums.unrealengine.com/t/add-component-by-python/143662/4 "2021-03-14T12:37:23Z")

</div>

> [@anonymous\_user\_ec0f74c31](#):
>
> Hi.
> 
> I am trying to repeat a command that adds a component using the **“Add Component”** button.  
> I need to create the same **ImagePlateComponent** for **SceneComponent** but with Python code.
> 
> I execute code in Output Log:
> 
> ```auto
> 
> scene_component = camera.scene_component
> image_component = unreal.ImagePlateComponent()
> image_component.k2_attach_to(scene_component, socket_name="None", attach_type=unreal.AttachLocation.KEEP_RELATIVE_OFFSET, weld_simulated_bodies=True)
> 
> ```
> 
> But the **ImagePlateComponent** generated by the python code is different from the one created through the **“Add Component”** button:
> 
> ```auto
> 
> for i in range(scene_component.get_num_children_components()):
> print scene_component.get_child_component(i)
> 
> LogPython: <Object '/Game/fbxLevel.fbxLevel:PersistentLevel.cam_for_UE.CameraComponent' (0x000001CA2F795600) Class 'CineCameraComponent'>
> LogPython: <Object '/Game/fbxLevel.fbxLevel:PersistentLevel.cam_for_UE.ImagePlate' (0x000001CA2B87C100) Class 'ImagePlateComponent'>
> LogPython: <Object '/Engine/Transient.ImagePlateComponent_0' (0x000001CA2B5B1D00) Class 'ImagePlateComponent'>
> 
> ```
> 
> _- second line_: the component created by the **“Add Component”** button  
> _- third line_: component created by python code
> 
> I do not see the **ImagePlateComponent** (that creates the python code) in the **Details** panel.
> 
> \*\*[unreal.Actor](https://docs.unrealengine.com/en-US/PythonAPI/class/Actor.html) \*\*article says that a new component must be registered. But how to do that?  
> I can not find the required method ☹
> 
> How do I add an **ImagePlateComponent** so that it displays under the **SceneComponent** as attachement?

I’m trying to do the same thing but via editor utility widgets. The component shows up for a few seconds in the world and then disappears, and its never added in the details panel for the selected/target actor. Did you ever find a way to do this?

---

<div class="post-metadata">

**Author:** ![Jon.Lambert](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jon.lambert/32/1145404_2.png) [@Jon.Lambert](https://forums.unrealengine.com/u/Jon.Lambert)\
**Post date:** [April 8, 2021, 5:04am UTC](https://forums.unrealengine.com/t/add-component-by-python/143662/5 "2021-04-08T05:04:34Z")

</div>

You need to add a new node to the Blueprint’s SimpleConstructionScript so it’s serialized and properly registered. The easiest way to do this is a little hacky, but you can just instantiate a SSCSEditor widget and use that to add a component to a Blueprint or actor instance. This is exactly what the Blueprint editor uses when you click the +AddComponent button (SSCSEditor::AddNewComponent). You’ll have to wrap this functionality in a plugin or BlueprintFunctionLib to expose it to Python.

---

<div class="post-metadata">

**Author:** ![Mifthbeat](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/mifthbeat/32/919874_2.png) [@Mifthbeat](https://forums.unrealengine.com/u/Mifthbeat)\
**Post date:** [August 28, 2023, 11:56pm UTC](https://forums.unrealengine.com/t/add-component-by-python/143662/6 "2023-08-28T23:56:01Z")

</div>

Oh, it’s so hard for such a simple action. UE scripting is big pain.

---

<div class="post-metadata">

**Author:** ![user\_a21a94b6fd0e051e414868d21b0aa2ef158d2f863f66c259101a2a](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@user\_a21a94b6fd0e051e414868d21b0aa2ef158d2f863f66c259101a2a](https://forums.unrealengine.com/u/user_a21a94b6fd0e051e414868d21b0aa2ef158d2f863f66c259101a2a)\
**Post date:** [September 14, 2024, 2:33am UTC](https://forums.unrealengine.com/t/add-component-by-python/143662/7 "2024-09-14T02:33:00Z")

</div>

why can not set static\_mesh and material dynamically?

```auto
#
# Test_Actor
# > DefaultSceneRoot
# > InstancedStaticMesh_Red
# > InstancedStaticMesh_Green
# > InstancedStaticMesh_Blue
#

actor_list:list = unreal.EditorLevelLibrary.get_all_level_actors()

component_list:list = []

for actor in actor_list:
    if type(actor) == unreal.Actor and actor.get_actor_label() == 'Test_Actor':
        scene:unreal.SceneComponent = actor.get_component_by_class(unreal.SceneComponent)
        for index in range(scene.get_num_children_components()):
            # component already set static_mesh and material.
            component = scene.get_child_component(index)
            if type(component) == unreal.InstancedStaticMeshComponent:
                component_list.append(component)

mesh_ids:list = [0, 1, 2, 1, 2, 1, 0, 2]

for index, id in enumerate(mesh_ids):
    component:unreal.InstancedStaticMeshComponent = component_list[id]
    component.add_instance(unreal.Transform(calc(index, 0, 100), unreal.Rotator(0, 0, 0)))

```
