# Using Python to obtain metadata of objects in Unreal scenes

**URL:** <https://forums.unrealengine.com/t/using-python-to-obtain-metadata-of-objects-in-unreal-scenes/2494478>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, editor, CPP, question\
**Created:** [May 7, 2025, 1:00am UTC](https://forums.unrealengine.com/t/using-python-to-obtain-metadata-of-objects-in-unreal-scenes/2494478 "2025-05-07T01:00:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![qiuter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/qiuter/32/769712_2.png) [@qiuter](https://forums.unrealengine.com/u/qiuter)\
**Post date:** [May 7, 2025, 1:00am UTC](https://forums.unrealengine.com/t/using-python-to-obtain-metadata-of-objects-in-unreal-scenes/2494478/1 "2025-05-07T01:00:40Z")

</div>

I want to try using Python to obtain the Datasmith metadata attributes of objects in Unreal scenes. I have looked at the official Python API, but the documentation lacks sufficient function explanations, which makes me unsure which function to use to obtain the data I want. I use AI to generate code for me, but AI always creates some non-existent function methods 😭

```auto
import unreal
import json

def get_selected_objects_and_children():
    """ Retrieve the currently selected object and all its sub objects at the point """
    selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
    all_objects = []

    def get_children_recursive(actor):
        """ Recursive retrieval of all sub objects """
        all_objects.append(actor)
        # select_actor_name = all_objects[0].get_name()
        for child in actor.get_attached_actors():
            get_children_recursive(child)

    for actor in selected_actors:
        get_children_recursive(actor)

    return all_objects

def get_actor_metadata(actor):
    """Retrieve the MetaData dictionary for a single Actor"""
    # metadata = unreal.EditorLevelLibrary.get_metadata_tag_values(actor)
    # metadata = unreal.DatasmithUserData(actor)
    metadata = unreal.DatasmithUserData.get_datasmith_user_data(actor)
    return metadata or {}

def collect_metadata_from_selection():
    """Main function: Collect MetaData of the selected object and its sub objects"""
    actors = get_selected_objects_and_children()
    if not actors:
        print("No object selected")
        return
    
    print(f" Found together {len(actors)} Object (including sub objects)")

    for actor in actors:
        actor_name = actor.get_name()
        user_data = get_actor_metadata(actor)
        meta = user_data.metadata
        print(f"\n Actor: {actor_name}")
        for key, value in meta.items():
            print(f" {key}:{value}")
        # print(f" MetaData: {meta}")
        # if metadata:
        # for key, value in metadata.items():
        # print(f" {key}:{value}")
        # else:
        # print(" 无 MetaData")

def get_datasmith_user_data(actor):
    # Get DatasmithUserData object
    user_data = unreal.DatasmithUserData.get_datasmith_user_data(actor)
    if user_data:
        # Acquiring Metadata
        metadata = user_data.get_editor_property("Metadata")
        return metadata
    return None

# Example: Obtain the UserData of the selected Actor
selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
for actor in selected_actors:
    metadata = get_datasmith_user_data(actor)
    if metadata:
        print(f"Actor: {actor.get_name()}, Metadata: {metadata}")

# collect_metadata_from_selection()

```

The following are the logs related to code output

collect\_metadata\_from\_selection()Method output (partial):  
LogPython: Actor: Object\_21111-CTOPS-15110-600-M1E1-C100  
LogPython: MetaData: {}  
LogPython: Actor: BRANCH\_4\_of\_PIPE\_\_21111-CTOPS-15110-600-M1E1-C100  
LogPython: MetaData: {}  
LogPython: Actor: Object\_TIR5103\_21111  
LogPython: MetaData: {}  
LogPython: Actor: FLANGE\_1\_of\_BRANCH\_4\_of\_PIPE\_\_21111-CTOPS-15110-600-M1E1-C100

Example output:  
LogPython: Error: Traceback (most recent call last):  
LogPython: Error: File “D:/Project/PythonProject/P\_Projects/TestProject/PythonCode/Unreal/Unreal\_SaveDatasmithUserData.py”, line 101, in   
LogPython: Error: metadata = get\_datasmith\_user\_data(actor)  
LogPython: Error: File “D:/Project/PythonProject/P\_Projects/TestProject/PythonCode/Unreal/Unreal\_SaveDatasmithUserData.py”, line 91, in get\_datasmith\_user\_data  
LogPython: Error: user\_data = unreal.DatasmithUserData.get\_datasmith\_user\_data(actor)  
LogPython: Error: AttributeError: type object ‘DatasmithUserData’ has no attribute ‘get\_datasmith\_user\_data’

 ![微信截图_20250507085633](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/9/9/0998d901415cae1c68b3da259ffde19118730eba.jpeg)

---

<div class="post-metadata">

**Author:** ![fairypark](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@fairypark](https://forums.unrealengine.com/u/fairypark)\
**Post date:** [May 7, 2025, 2:22am UTC](https://forums.unrealengine.com/t/using-python-to-obtain-metadata-of-objects-in-unreal-scenes/2494478/2 "2025-05-07T02:22:28Z")

</div>

unreal.DatasmithContentLibrary.get\_datasmith\_user\_data(object)

" After the Datasmith import process is done, you can access metadata for all Actors or selected Actors using the `unreal.DatasmithContentLibrary` class."

> **[Using Datasmith Metadata in Unreal Engine | Unreal Engine 5.5 Documentation |...](https://dev.epicgames.com/documentation/en-us/unreal-engine/using-datasmith-metadata-in-unreal-engine)**
>
> Get custom metadata about Assets into Unreal, and use Blueprint and Python scripting to work with that metadata in the Editor and at runtime.

> **[unreal.DatasmithContentLibrary — Unreal Python 5.5 (Experimental) documentation](https://dev.epicgames.com/documentation/en-us/unreal-engine/python-api/class/DatasmithContentLibrary)**

---

<div class="post-metadata">

**Author:** ![qiuter](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/qiuter/32/769712_2.png) [@qiuter](https://forums.unrealengine.com/u/qiuter)\
**Post date:** [May 7, 2025, 2:27am UTC](https://forums.unrealengine.com/t/using-python-to-obtain-metadata-of-objects-in-unreal-scenes/2494478/3 "2025-05-07T02:27:02Z")

</div>

Oh, thank you. I finally obtained the metadata for the corresponding object through Unreal. DataSmithContentLibrary. get\_datasmity\_user\_data (actor)  
 ![微信截图_20250507102642](https://d3kjluh73b9h9o.cloudfront.net/optimized/4X/e/1/c/e1c8f62bd2012a2369cead875c808bbda585228b_2_690x13.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [June 6, 2025, 2:28am UTC](https://forums.unrealengine.com/t/using-python-to-obtain-metadata-of-objects-in-unreal-scenes/2494478/4 "2025-06-06T02:28:02Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
