Outsource Asset Importer - Screenshoter

 avatar
JO00
python
2 months ago
5.9 kB
7
Indexable
import unreal
import os
import csv

unreal.EditorLevelLibrary.editor_set_game_view(True)

directory_rest=os.path.dirname(mesh_list_csv)

#os.path.dirname(__file__)

trello_config=str(directory_rest)+"/trello_config.txt"
with open(trello_config, "r", newline="", encoding="utf-8") as f:
        all_lines=f.readlines()
        mesh_to_screenshot=int(all_lines[4].strip())
#########
######## importing important locations from csv
csv_location=str(directory_rest)+"/mat_data.csv"

#####reading csv for ue project definitions
with open(csv_location, newline="", encoding="utf-8") as f:
    reader = csv.reader(f)
    rows = list(reader)
    mat_names=rows[0]
    mat_name_locations=rows[1]
    ue_folder_text=rows[2]
    ue_folder_mesh=rows[3]
    ue_folder_mats=rows[4]
    thumb_level_loc=rows[5]

###getting values of where mesh folder is and the meshes to import from that config
mesh_csv_location=str(directory_rest)+"/mesh_data.csv"
with open(mesh_csv_location, newline="", encoding="utf-8") as f:
    reader = csv.reader(f)
    rows = list(reader)
    mesh_import_folder=rows[0]
    meshes_to_import=rows[1]

###the actual import bit
try:
#######
#########
###now creating thumbnial
    print("making thumb")
    size_thumb=500
    
    if check_cam_state==True:
        print("cam is checked! now swapping to it")
        selected_actors = unreal.EditorLevelLibrary.get_selected_level_actors()
        thumb_camera = selected_actors[0]
        unreal.EditorLevelLibrary.pilot_level_actor(thumb_camera)


    if check_cam_state==False:
        unreal.EditorLevelLibrary.load_level(thumb_level_loc[0])

    #replacer = None

    all_actors = unreal.EditorLevelLibrary.get_all_level_actors()
    thumb_mesh=None

    for a in all_actors:
        if "thumb_mesh" in a.tags:
            thumb_mesh = a
            print("loaded mesh")
            break

    if check_cam_state==False:
        thumb_camera = None
        for a in all_actors:
                if "thumb_camera" in a.tags:
                    thumb_camera = a
                    print("loaded camera")
                    break

    # Execute the console command
    unreal.SystemLibrary.execute_console_command(None, "r.ScreenPercentage 10")
    unreal.SystemLibrary.execute_console_command(None, "r.Tonemapper.Sharpen 1")
    unreal.SystemLibrary.execute_console_command(None, "r.ShadowQuality 10")
    unreal.SystemLibrary.execute_console_command(None, "r.DefaultFeature.AntiAliasing 2")

    #editor_subsystem.EditorLevelLibrary.pilot_level_actor(thumb_camera)

    unreal.EditorLevelLibrary.pilot_level_actor(thumb_camera)
    mesh=meshes_to_import[dropdown_selection]
    #for mesh in meshes_to_import:
    #now level is good for thumbnial later
    mesh_path=ue_folder_mesh[0]+"/"+mesh+"."+mesh

    static_load=unreal.EditorAssetLibrary.load_asset(mesh_path)
    ###camera fixing before thumb logic
    mesh_comp = thumb_mesh.get_editor_property("static_mesh_component")
    mesh_comp.set_editor_property("static_mesh", static_load)
    output_path=mesh_import_folder[0]+"/"+mesh+".png"
    #mesh_comp.reset_materials_to_default()

    default_mats = static_load.get_editor_property("static_materials")
    for i, mat_slot in enumerate(default_mats):
        mat_interface = mat_slot.material_interface
        mesh_comp.set_material(i, mat_interface)

    ###moving camera to fit into frame by my calc
    ###this only works with downwards angle slope so this is set too!
    #asset must be at 0,0,0


    ###minimum is 10cm so
    #if size_x<10:
    #    size_x=10
    #if size_y<10:
    #    size_x=10
    #if size_x<10:
    #    size_z=10

    if check_cam_state==False:
         
        bounding_box = static_load.get_bounding_box()
        min_bounds = bounding_box.min
        max_bounds = bounding_box.max

        size_x = round(max_bounds.x - min_bounds.x, 2)
        size_y = round(max_bounds.y - min_bounds.y, 2)
        size_z = round(max_bounds.z - min_bounds.z, 2)

        ###moving to fit now
        ##theres 3 different modes depending on the bound size if its tall, flat or regular
        print("screen thumb now")

        if size_x>size_z and size_y>size_z: # flat so need to point directly down above and take shot
            print("chosen flat down")
            new_location = unreal.Vector(size_x*1.6, size_y*-0.2, size_z*2)   # X, Y, Z in world space
            new_rotation = unreal.Rotator(0, -80, -1.6)
        
        elif size_y<15 or size_x<15 or size_z<15: # for really small ones
            print("chosen small")
            new_location = unreal.Vector(size_x*-10, size_y*0.5, size_z*0.8)   # X, Y, Z in world space
            new_rotation = unreal.Rotator(0, -12, -3.6)
        
        else:
            print("chosen final")
            new_location = unreal.Vector(size_x*-1.8, size_y*0.2, size_z*1.1)   # X, Y, Z in world space
            new_rotation = unreal.Rotator(0, -25.4, -3.4)
        
        #old
        # new_location = unreal.Vector(size_x*1.15, size_y*-0.4825, size_z*1.2)   # X, Y, Z in world space
        #new_rotation = unreal.Rotator(0, -28.388928, 154.601825)

        thumb_camera.set_actor_location(new_location, sweep=False, teleport=True)
        thumb_camera.set_actor_rotation(new_rotation, teleport_physics=True)


        print("saving to")
        print(output_path)

    #finally take screensho
    unreal.AutomationLibrary.take_high_res_screenshot(
        size_thumb,
        size_thumb,
        output_path
    )

    #wait_for_file(output_path)
    
    unreal.EditorAssetLibrary.save_asset(static_load.get_path_name())

    unreal.EditorLevelLibrary.pilot_level_actor(None)

        

except Exception as e:
    print("FAILED:", e)
    raise


###now to take screenshots
Editor is loading...
Leave a Comment