unreal:选取关卡中的actor并修改actor的static mesh路径到指定路径下
import unreal
# 获取所选actor在指定路径下的绝对路径
def find_static_mesh_in_path(search_path, target_name):
# 转换路径格式:/Game/ -> /Game
clean_path = search_path.rstrip('/')
# 获取所有资产路径(递归)
asset_paths = unreal.EditorAssetLibrary.list_assets(clean_path, recursive=True)
found_assets = ""
for asset_path in asset_paths:
# 跳过空路径
if not asset_path:
continue
# 获取资产数据
asset_data = unreal.AssetRegistryHelpers.get_asset_registry().get_asset_by_object_path(asset_path)
# 过滤静态网格体
if asset_data.asset_class_path.asset_name != "StaticMesh":
continue
# 获取资产名称和路径
asset_name = asset_data.asset_name
package_path = asset_data.package_name
if asset_name == target_name:
display_path = str(package_path)
found_assets = display_path
# 输出结果
return found_assets
# 获取所选actor的静态网格体名称
def get_selected_static_mesh_names():
# 获取选中的Actor
selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
mesh_names = []
for actor in selected_actors:
# 获取Actor的所有静态网格组件
mesh_components = actor.get_components_by_class(unreal.StaticMeshComponent)
for component in mesh_components:
static_mesh = component.static_mesh
if static_mesh:
# 获取actor name
mesh_name = static_mesh.get_name()
mesh_names.append(mesh_name)
abs_path = ""
# 获取所选actor在指定路径下的绝对路径
abs_path = find_static_mesh_in_path("/Game/Test2", mesh_name)
if abs_path:
unreal.log(f" {mesh_name}: {abs_path}")
# 替换资产引用路径
new_mesh = unreal.load_asset(abs_path)
new_mesh_component = actor.static_mesh_component
if new_mesh_component:
new_mesh_component.set_static_mesh(new_mesh)
unreal.log(f"已更新 {actor.get_name()} 的静态网格体")
else:
unreal.log_warning(f"{actor.get_name()} 没有找到静态网格体组件")
else:
unreal.log(f" {mesh_name}未在指定路径下找到对应的static mesh")
# 刷新视口
unreal.SystemLibrary.execute_console_command(None, "r.redraw")
else:
pass
# 打印结果
if len(mesh_names) == 0:
unreal.log("没有找到包含静态网格体的选中Actor")
# 测试函数
get_selected_static_mesh_names()
# 筛选所选actor的SM路径是否在指定路径下
def get_selected_static_mesh_paths_with_filter():
selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
# 存储不符合条件的条目(格式:字典列表)
invalid_entries = []
target_path = "/Game/Test2/Cube"
for actor in selected_actors:
# 获取Actor显示名称(在场景中的命名)
actor_name = actor.get_actor_label()
# 获取所有静态网格组件
components = actor.get_components_by_class(unreal.StaticMeshComponent)
for component in components:
static_mesh = component.static_mesh
if static_mesh:
asset_path = static_mesh.get_path_name()
# 检查路径是否符合要求
if not asset_path.startswith(target_path):
# 获取网格体资源名称
mesh_name = static_mesh.get_name()
invalid_entries.append({
"actor": actor_name,
"mesh": mesh_name,
"path": asset_path
})
# 输出结果
if invalid_entries:
print(f"以下资源不在 {target_path} 路径下:")
for entry in invalid_entries:
print(f"Actor:{entry['actor']}")
print(f"网格体:{entry['mesh']}")
print(f"路径:{entry['path']}")
print("-" * 50)
else:
print(f"所有静态网格体路径均符合 {target_path} 路径要求")
return invalid_entries
# 执行函数
get_selected_static_mesh_paths_with_filter()
更多推荐



所有评论(0)