Workspace TRAINING

Tools Breakdown

Custom pipelines, automation scripts, and utility tools for production.

This tool uncheck sRGB from all utilitary textures in a folder.

 import unreal

# Check if at least one folder is selected in the Content Browser, if not log a warning and exit the script
if not unreal.EditorUtilityLibrary.get_selected_folder_paths():
    unreal.log_warning("¨Please, select at least one folder in the Content Browser.")
    exit()

def get_all_assets(): #define a function to get all assets in the selected folders in the content browser and return a list of all assets paths

    Folders_Path = unreal.EditorUtilityLibrary.get_selected_folder_paths()  #get the selected folders paths in the content browser
    total_frames = len(Folders_Path)#define variable to setup Progress Dialogs for Slow Operations

    Total_List_Assets = [] #Define list to store all assets paths in the selected folders

    if total_frames > 0: # Security check
        text_label = "Search for assets in folders..."

        
        with unreal.ScopedSlowTask(total_frames, text_label) as slow_task: #Initialize the progress dialog 
            slow_task.make_dialog(True)  

            for folder in Folders_Path: #loop through each selected folders paths

                if slow_task.should_cancel(): 
                    unreal.log_warning("Operation cancelled by the user.")
                    break

                slow_task.enter_progress_frame(1, f"Processing : {folder}") #Advance the progress by one frame and update the text in the progress dialog to show which folder is currently being processed, this allows the user to see the progress of the operation and which folder is being processed at any given time

                clean_path = folder.replace("/All", "") 
                Assets_In_Folder = (unreal.EditorAssetLibrary.list_assets(str(clean_path), True, False)) #Get the list of all assets paths in the current folder being processed using the unreal.EditorAssetLibrary.list_assets function, this function takes the cleaned folder path as an argument and returns a list of all assets paths in that folder, the second argument "True" is used to include subfolders in the search and the third argument "False" is used to exclude folders from the search
                Total_List_Assets.extend(Assets_In_Folder) 

        return Total_List_Assets   
    
Total_Assets = get_all_assets() 
unreal.log(f"Total assets in folder: {len(Total_Assets)}")
            
      
def get_texture2d_assets():

    total_frames = len(Total_Assets) #define variable to setup Progress Dialogs for Slow Operations

    Texture2D_present = False 

    if total_frames > 0:
        text_label = "Search for Texture2D in Assets"

        with unreal.ScopedSlowTask(total_frames, text_label) as slow_task:
            slow_task.make_dialog(True)  

            Texture2D_assets = []
            for asset in Total_Assets:

                if slow_task.should_cancel():
                    unreal.log_warning("Operation cancelled by the user.")
                    break

                slow_task.enter_progress_frame(1, f"Processing : {asset}")

                get_selected_assets = unreal.EditorAssetLibrary.load_asset(asset) #Load the asset using the unreal.EditorAssetLibrary.load_asset function, this function takes the asset path as an argument and returns the loaded asset object, we will use this object to check if it is a Texture2D asset and to modify its properties later in the script
                if isinstance(get_selected_assets, unreal.Texture2D): #Check if the loaded asset is an instance of the unreal.Texture2D class, this is used to filter out only the Texture2D assets from the total list of assets paths we have collected, if the asset is a Texture2D asset then we will add it to the list of Texture2D assets that we will use later in the script to modify their properties
                    Texture2D_assets.extend([get_selected_assets]) #Add the loaded Texture2D asset to the list of Texture2D 
                    Texture2D_present = True 
        return Texture2D_assets ,Texture2D_present 

Total_Texture2D_Assets, is_texture_2d_present = get_texture2d_assets()
unreal.log(f"is there any Texture2D assets in the selected folders? : {is_texture_2d_present}") #Log whether we have found any Texture2D assets in the selected folders, this is used for debugging purposes to ensure that we are correctly identifying Texture2D assets in the selected folders, it will print "True" if we have found at least one Texture2D asset and "False" if we have not found any Texture2D assets in the selected folders

if Total_Texture2D_Assets is not None: #Check if the list of Texture2D assets is not empty, if it is empty log a message to the user and exit the script, if it is not empty then we will proceed to modify the properties of the Texture2D assets in the list
    unreal.log(f"Total Texture2D assets in folder: {len(Total_Texture2D_Assets)}")

else: #If the list of Texture2D assets is empty, log a message to the user indicating that no Texture2D assets were found 
    unreal.log("No Texture2D assets found in the selected folders.")


def remove_srgb():

    Total_frame = len(Total_Texture2D_Assets)

    if Total_frame > 0:
        text_label = "Removing sRGB from Texture2D assets..."

        with unreal.ScopedSlowTask(Total_frame, text_label) as slow_task:
            slow_task.make_dialog(True)
            total_srgb_removed = 0

            #loop through the Asset_Texture2D list and check if the asset name contains the word defined in the "word_search" variable, if it does then set the sRGB property to False and count the total number of assets that have been modified
            for asset in Total_Texture2D_Assets:

                if slow_task.should_cancel():
                    unreal.log_warning("Operation cancelled by the user.")
                    break

                slow_task.enter_progress_frame(1, f"Processing : {asset}")

                if word_search in asset.get_name(): #"word_search" is not defined in the code, define in Unreal Variable Editor Utility Widget 
                    unreal.log(f"Found asset {asset.get_name()} with word {word_search} inside : {asset}") 
                    if asset.get_editor_property("sRGB") == True: 
                        asset.set_editor_property("sRGB", False)
                        total_srgb_removed += 1
        return total_srgb_removed #Return the total number of assets that have been modified with the sRGB property set to False, this is used to provide feedback to the user on how many assets were successfully modified by the script, we will log this information in the output log after the function is called to informthe user ofthe results ofthe operation
        
total_srgb_removed = remove_srgb()#define a variable to count the total number of assets that have been modified,define in Unreal Variable Editor Utility Widget