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

**URL:** <https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866>\
**Category:** Programming & Scripting\
**Tags:** Python, unreal-engine, CPP, Blueprint, question\
**Created:** [April 5, 2023, 11:20pm UTC](https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866 "2023-04-05T23:20:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JediKnightChan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jediknightchan/32/1156960_2.png) [@JediKnightChan](https://forums.unrealengine.com/u/JediKnightChan)\
**Post date:** [April 5, 2023, 11:20pm UTC](https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866/1 "2023-04-05T23:20:54Z")

</div>

First, I create blueprint

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![JediKnightChan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jediknightchan/32/1156960_2.png) [@JediKnightChan](https://forums.unrealengine.com/u/JediKnightChan)\
**Post date:** [April 6, 2023, 10:16am UTC](https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866/2 "2023-04-06T10:16:07Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![JediKnightChan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jediknightchan/32/1156960_2.png) [@JediKnightChan](https://forums.unrealengine.com/u/JediKnightChan)\
**Post date:** [April 6, 2023, 2:41pm UTC](https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866/3 "2023-04-06T14:41:10Z")

</div>

> [@JediKnightChan](#):
>
> ```auto
> 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)])
> 
> ```

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.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/a/d/7ad5122e69b83c49d48306226674176050139322.png)

---

<div class="post-metadata">

**Author:** ![JediSkywalker2](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jediskywalker2/32/88719_2.png) [@JediSkywalker2](https://forums.unrealengine.com/u/JediSkywalker2)\
**Post date:** [April 6, 2023, 8:39pm UTC](https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866/5 "2023-04-06T20:39:12Z")

</div>

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![bibiocoad12](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/bibiocoad12/32/777456_2.png) [@bibiocoad12](https://forums.unrealengine.com/u/bibiocoad12)\
**Post date:** [November 17, 2023, 6:43pm UTC](https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866/6 "2023-11-17T18:43:49Z")

</div>

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/](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!

---

<div class="post-metadata">

**Author:** ![JediKnightChan](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jediknightchan/32/1156960_2.png) [@JediKnightChan](https://forums.unrealengine.com/u/JediKnightChan)\
**Post date:** [November 17, 2023, 10:03pm UTC](https://forums.unrealengine.com/t/python-trying-to-create-subobject-in-blueprint-getting-not-valid-handles/855866/7 "2023-11-17T22:03:15Z")

</div>

No, this was the reason why I switched to 5.1 (which now I regret because its multiplayer root motion [is broken](https://forums.unrealengine.com/t/melee-horrible-server-to-non-owning-client-position-replication/1191124/3)).

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