"Add Component" by Python

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

I execute code in Output Log:



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:



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 **article says that a new component must be registered. But how to do that?
I can not find the required method :frowning:

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

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

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

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?

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.

1 Like

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

why can not set static_mesh and material dynamically?

#
# 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)))