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
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’