Python: Trying to create subobject in blueprint, getting not valid handles

First, I create blueprint

package_path = "/Game/Blueprints"
factory = unreal.BlueprintFactory()
factory.set_editor_property("parent_class", unreal.Actor)
# make the blueprint
asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
blueprint = asset_tools.create_asset("test", package_path, None, factory)

Then, try to get root handle for it to attach other components to it later. However, I get:

subsystem = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)
BFL = unreal.SubobjectDataBlueprintFunctionLibrary
print([BFL.is_handle_valid(x) for x in subsystem.k2_gather_subobject_data_for_blueprint(context=blueprint)])

LogPython: [False, False]

So later when I try to create subobject and attach it to the root handle, attach returns False. Could anyone help to fix this or provide working code for creating blueprints with static mesh components?

I have serious suspicions that people who wrote the code above were using 5.1 and since I’m using 5.0 nothing is working…

I am shocked. Indeed, it only works in 5.1. Whoever made the same not working api in 5.0, I don’t know what to say to you.

import unreal

"""Creates Actor blueprint with static mesh components. WARNING: Works only in UE 5.1
"""

# Change me!
ASSET_NAME = "TestBlueprint"
ASSET_DIR = "/Game/Blueprints/"

BFL = unreal.SubobjectDataBlueprintFunctionLibrary
SDS = unreal.get_engine_subsystem(unreal.SubobjectDataSubsystem)


def get_sub_handle_object(sub_handle):
    obj = BFL.get_object(BFL.get_data(sub_handle))
    return obj


def add_component(root_data_handle, subsystem, blueprint, new_class, name):
    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(f"ERROR from sub_object_subsystem.add_new_subobject: {fail_reason}")

    subsystem.rename_subobject(handle=sub_handle, new_name=unreal.Text(name))
    subsystem.attach_subobject(owner_handle=root_data_handle, child_to_add_handle=sub_handle)
    obj = BFL.get_object(BFL.get_data(sub_handle))
    return sub_handle, obj


def create_actor_blueprint(asset_name, asset_dir):
    factory = unreal.BlueprintFactory()
    factory.set_editor_property("parent_class", unreal.Actor)

    asset_tools = unreal.AssetToolsHelpers.get_asset_tools()
    blueprint = asset_tools.create_asset(asset_name, asset_dir, None, factory)

    root_data_handle = SDS.k2_gather_subobject_data_for_blueprint(blueprint)[0]
    scene_handle, scene = add_component(root_data_handle, SDS, blueprint, unreal.SceneComponent, name="Scene")

    for i in range(2):
        sub_handle, weight = add_component(
            scene_handle,
            subsystem=SDS,
            blueprint=blueprint,
            new_class=unreal.StaticMeshComponent,
            name=f"Weight{i}")
        assert isinstance(weight, unreal.StaticMeshComponent)
        mesh = unreal.EditorAssetLibrary.find_asset_data("/Game/PROPS/Ambiance/SM_PROB_Ventilator_01").get_asset()
        weight.set_static_mesh(mesh)
        weight.set_editor_property(name="relative_location", value=unreal.Vector(10.0, -165.0, 640.0))

    unreal.EditorAssetLibrary.save_loaded_asset(blueprint)


create_actor_blueprint(ASSET_NAME, ASSET_DIR)

Can you please tell me if you’ve found a solution for 5.0? Unfortunately, I’ve also encountered this issue in this version. I’m not very experienced in python and would appreciate any help. I’ve tried using add_child_component and passing it the sub-object descriptor, but I’m still getting an error. As a last resort, I’ll turn to https://cn.papersowl.com/python-ghostwriting/ , which offers a python code ghostwriting service. However, I’d like to figure this out on my own. Perhaps someone can suggest an alternative code for creating schemas. Thanks in advance for any support!

No, this was the reason why I switched to 5.1 (which now I regret because its multiplayer root motion is broken).

Unless they made an update with fixes for 5.0 since that time, which I estimate as a low probability.