Creating an actor with components in Python

Hello,

I’m currently trying to write a python script that will read through a folder and create “targets”. A target is basically an actor with a plane and a texture. In my folder I have an XML for name and dimensions and then a .dds for the texture. I’m able to import the texture and create a material with it, I’m also able to create the blueprint actor but when I want to create a StaticMeshComponent for the created actor I just can’t seem to find the solution.

I currently use this code to create a blueprint :

    # Create an Actor factory
    factory = unreal.BlueprintFactory()
    factory.set_editor_property("parent_class", unreal.Actor)

    # make the blueprint asset
    new_asset = ASSET_TOOLS.create_asset("BP_"+asset_name, package_path, None, factory)
    if not isinstance(new_asset, unreal.Blueprint):
        raise Exception("Failed to create blueprint asset")

Then to add my StaticMeshComponent I’ve tried

    # Add staticmeshcomponent to our actor
    static_mesh: unreal.StaticMeshComponent = unreal.StaticMeshComponent()
    static_mesh.attach_to(new_asset .root_component)

Also :


    root_data_handle: unreal.SubobjectDataHandle = subsystem.k2_gather_subobject_data_for_blueprint(context=blueprint)[0]

    sub_handle, fail_reason = subsystem.add_new_subobject(
        params=unreal.AddNewSubobjectParams(
            parent_handle=root_data_handle,
            new_class=new_class,
            blueprint_context=blueprint))
    if not fail_reason.is_empty():
        raise Exception("ERROR from sub_object_subsystem.add_new_subobject: {fail_reason}")

    subsystem.rename_subobject(handle=sub_handle, new_name=unreal.Text(name))

I don’t get any errors but the component isn’t created.

Hi,

I am trying to do the same thing, if you want to create a Static Mesh Component you can do this:

 factory = unreal.BlueprintFactory()
 factory.set_editor_property("ParentClass", unreal.Actor)
 
 # make the blueprint
 asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
 bp_actor = asset_tools.create_asset(blueprint_name, asset_path, None, factory)
 
 unreal.EditorAssetLibrary.save_loaded_asset( bp_actor )
 
 # get the root data handle
 subsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)
 root_data_handle = subsystem.k2_gather_subobject_data_for_blueprint(bp_actor)[0]
 
 staticmesh = unreal.StaticMeshComponent
 
 #add new component
 sub_handle, fail_reson = subsystem.add_new_subobject(
             params=unreal.AddNewSubobjectParams(
                 parent_handle=root_data_handle,
                 new_class=staticmesh,
                 blueprint_context=bp_actor))

subsystem.rename_subobject(handle=sub_handle, new_name=unreal.Text("piece0"))

My issue is that I dont know how to edit the new Static Mesh Component, any idea?

This topic has been moved from International to Programming & Scripting: C++.

When posting, please review the categories to ensure your topic is posted in the most relevant space.

Hopefully, this category change will help to get an answer. In the meantime, good luck and happy developing. :slight_smile:

1 Like