def rename_assets(search_pattern, replace_pattern, use_case): # instances of unreal classes system_lib = unreal.SystemLibrary() editor_util = unreal.EditorUtilityLibrary() string_lib = unreal.StringLibrary() # get the selected assets selected_assets = editor_util.get_selected_assets() num_assets = len(selected_assets) replaced = 0 unreal.log("Selected {} asset/s".format(num_assets)) # loop over each asset and rename for asset in selected_assets: asset_name = system_lib.get_object_name(asset) # check if the asset name contains the to be replaced text if string_lib.contains(asset_name, search_pattern, use_case=use_case): search_case = unreal.SearchCase.CASE_SENSITIVE if use_case else unreal.SearchCase.IGNORE_CASE replaced_name = string_lib.replace(asset_name, search_pattern, replace_pattern, search_case=search_case) editor_util.rename_asset(asset, replaced_name) replaced += 1 unreal.log("Replaced {} with {}".format(asset_name, replaced_name)) else: unreal.log("{} did not match the search pattern, was skipped".format(asset_name)) unreal.log("Replaced {} of {} assets".format(replaced, num_assets))
import unreal import os # instances of unreal classes editor_util = unreal.EditorUtilityLibrary() system_lib = unreal.SystemLibrary() editor_asset_lib = unreal.EditorAssetLibrary() # get the selected assets selected_assets = editor_util.get_selected_assets() num_assets = len(selected_assets) cleaned = 0 # hard coded parent path parent_dir = "\\Game" if num_assets > 0: asset_path = editor_asset_lib.get_path_name_for_loaded_asset( selected_assets[0]) parent_dir = os.path.dirname(asset_path) for asset in selected_assets: # get the class instance and the clear text name asset_name = system_lib.get_object_name(asset) asset_class = asset.get_class() class_name = system_lib.get_class_display_name(asset_class) # assemble new path and relocate assets try: new_path = os.path.join(parent_dir, class_name, asset_name) editor_asset_lib.rename_loaded_asset(asset, new_path)
# -*- coding: utf-8 -*- import os import unreal level_lib = unreal.EditorLevelLibrary() util_lib = unreal.EditorUtilityLibrary() Socket_Path = "/Game/Data/Temp/Socket_Loc" def main(): for asset in util_lib.get_selected_assets(): name = asset.get_name() if not isinstance(asset, unreal.StaticMesh): continue container_path = os.path.splitext(asset.get_path_name())[0] container = unreal.load_asset(Socket_Path) if not container: print(f"失败路径 -> {container_path}") return container = level_lib.spawn_actor_from_object( container, unreal.Vector(0.0, 0.0, 0.0)) r = unreal.AttachmentRule.SNAP_TO_TARGET for socket in container.root_component.get_all_socket_names(): mesh = level_lib.spawn_actor_from_object( asset, unreal.Vector(0.0, 0.0, 0.0)) mesh.attach_to_actor(container, socket, r, r, r, False) if __name__ == "__main__":