# python add\_component in level

**URL:** <https://forums.unrealengine.com/t/python-add_component-in-level/117601>\
**Category:** Editor Scripting\
**Tags:** unreal-engine\
**Created:** [October 17, 2018, 2:03am UTC](https://forums.unrealengine.com/t/python-add_component-in-level/117601 "2018-10-17T02:03:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnson5jr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@johnson5jr](https://forums.unrealengine.com/u/johnson5jr)\
**Post date:** [October 17, 2018, 2:03am UTC](https://forums.unrealengine.com/t/python-add_component-in-level/117601/1 "2018-10-17T02:03:43Z")

</div>

I’d like to be able to add a component to a StaticMeshActor in a level via python, but I am having trouble with the code. I’m not sure I’m clear on the Name parameter from the API: [https://api.unrealengine.com/INT/Pyt…l#unreal.Actor](https://api.unrealengine.com/INT/PythonAPI/class/Actor.html#unreal.Actor)

This is my non-working code. I’d love any insight into this! Thanks!

import unreal  
empty\_transform = [0,0,0], [0,0,0], [1,1,1]]  
selected\_actors = unreal.EditorLevelLibrary.get\_selected\_level\_actors()  
actor = selected\_actors[0]  
actor.add\_component(“unreal.AudioComponent”, True, empty\_transform, actor.get\_class()) # This isn’t right…

---

<div class="post-metadata">

**Author:** ![Robert.Clayton](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Robert.Clayton](https://forums.unrealengine.com/u/Robert.Clayton)\
**Post date:** [October 17, 2018, 8:13pm UTC](https://forums.unrealengine.com/t/python-add_component-in-level/117601/2 "2018-10-17T20:13:13Z")

</div>

I was having the same issue for another component type. I’d attempted all of the various ways you might imagine for that scenario:  
‘unreal.AudioComponent’  
‘unreal.Audio’  
‘AudioComponent’  
‘Audio’

etc.

In the end, I gave up on that function and went another route:

```auto

import unreal
actor = unreal.EditorLevelLibrary.get_selected_level_actors()[0]
audio_component = unreal.AudioComponent()
audio_component.attach_to_component(actor.root_component, '',unreal.AttachmentRule.KEEP_WORLD, unreal.AttachmentRule.KEEP_WORLD, unreal.AttachmentRule.KEEP_WORLD, False)

```

If the actor doesn’t have a root component, instead use

```auto

actor.set_editor_property('root_component', audio_component)

```

* * *

I would still love to see a working example of actor.add\_component(), though.

---

<div class="post-metadata">

**Author:** ![johnson5jr](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@johnson5jr](https://forums.unrealengine.com/u/johnson5jr)\
**Post date:** [October 19, 2018, 7:16pm UTC](https://forums.unrealengine.com/t/python-add_component-in-level/117601/3 "2018-10-19T19:16:58Z")

</div>

Thanks, Robert. This is very helpful. I would still like to see an example of add\_component too.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_1d736ae51](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_1d736ae51](https://forums.unrealengine.com/u/anonymous_user_1d736ae51)\
**Post date:** [April 10, 2019, 9:28pm UTC](https://forums.unrealengine.com/t/python-add_component-in-level/117601/4 "2019-04-10T21:28:14Z")

</div>

> [@Robert.Clayton](#):
>
> I was having the same issue for another component type. I’d attempted all of the various ways you might imagine for that scenario:  
> ‘unreal.AudioComponent’  
> ‘unreal.Audio’  
> ‘AudioComponent’  
> ‘Audio’
> 
> etc.
> 
> In the end, I gave up on that function and went another route:
> 
> ```auto
> 
> import unreal
> actor = unreal.EditorLevelLibrary.get_selected_level_actors()[0]
> audio_component = unreal.AudioComponent()
> audio_component.attach_to_component(actor.root_component, '',unreal.AttachmentRule.KEEP_WORLD, unreal.AttachmentRule.KEEP_WORLD, unreal.AttachmentRule.KEEP_WORLD, False)
> 
> ```
> 
> If the actor doesn’t have a root component, instead use
> 
> ```auto
> 
> actor.set_editor_property('root_component', audio_component)
> 
> ```
> 
> * * *
> 
> I would still love to see a working example of actor.add\_component(), though.

Hello! I’ve encountered some issue - The added component doesn’t editable and is empty(there is no options whatsoever), is this expected behavior? How to work with this this further?

![add_actor_component_.jpg](https://d3kjluh73b9h9o.cloudfront.net/original/3X/6/6/66cbfb06eca1c5f9fcf93187f9323a78232e6061.jpeg)

---

<div class="post-metadata">

**Author:** ![NGaming\_Ale](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@NGaming\_Ale](https://forums.unrealengine.com/u/NGaming_Ale)\
**Post date:** [September 22, 2021, 1:52pm UTC](https://forums.unrealengine.com/t/python-add_component-in-level/117601/5 "2021-09-22T13:52:31Z")

</div>

Have u found the solution to this problem?
