python add_component in level

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:…l#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…

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


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

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

actor.set_editor_property('root_component', audio_component)

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

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

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?