Outsource Asset Processor - Baker

 avatar
JO00
python
2 months ago
6.5 kB
8
Indexable
import bpy
import time
import os 
import csv

#getting the config
config_file=str(os.path.dirname(__file__))+"/shader_graphs/config.txt"
text_resolution=["512","1024","2048","4096"]
format_options=[".tga",".png",".jpeg"]
format_long=["TARGA","PNG","JPEG"]

text_res_chosen=1
format_chosen=0
bake_extrusion=1.0
ao_intensity=1.0
position_mask_val=1.0

####GETTING VALUES FROM CONFIG
with open(config_file, "r", newline="", encoding="utf-8") as f:
    all_lines=f.readlines()
    text_res_chosen=int(all_lines[3].strip())
    format_chosen=int(all_lines[4].strip())
    bake_extrusion=all_lines[5].strip()
    ao_intensity=all_lines[6].strip()
    position_mask_val=all_lines[7].strip()

#delete that stupid cube
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)


#now to import the original path and the list of meshes
csv_location=str(os.path.dirname(__file__))+"/shader_graphs/mesh_data.csv"
with open(csv_location, newline="", encoding="utf-8") as f:
    reader = csv.reader(f)
    rows = list(reader)

folder_path = rows[0][0]         
mesh_array = rows[1]

#now loop importing meshes
for mesh_name in mesh_array:
    #test meshes high and low poly
    high_poly_name=folder_path+"/"+mesh_name+"_high"
    #check if fbx or obj now
    if os.path.isfile(high_poly_name+".obj"):
        bpy.ops.wm.obj_import(filepath=high_poly_name+".obj")
    else:
        bpy.ops.wm.fbx_import(filepath=high_poly_name+".fbx")

    high_poly_name=bpy.context.active_object.name
    bpy.context.active_object.scale = (2, 2, 2)
    bpy.ops.object.transform_apply(scale=True)

    #then low
    low_poly_name=folder_path+"/"+mesh_name
    #check if fbx or obj now
    if os.path.isfile(low_poly_name+".obj"):
        bpy.ops.wm.obj_import(filepath=low_poly_name+".obj")
    else:
        bpy.ops.wm.fbx_import(filepath=low_poly_name+".fbx")
    low_poly_name=bpy.context.active_object.name
    bpy.context.active_object.scale = (2, 2, 2)
    bpy.ops.object.transform_apply(scale=True)

    #set scene up correcty
    bpy.context.scene.render.engine = 'CYCLES'
    bpy.context.scene.cycles.device = 'GPU'
    bpy.ops.object.select_all(action='DESELECT')

    #select low poly to set obj for mats
    obj = bpy.data.objects[low_poly_name]
    obj.select_set(True)    
    bpy.context.view_layer.objects.active = obj

    #select all then low poly last so it bakes to low poly!

    objectToSelect = bpy.data.objects[high_poly_name]
    objectToSelect.select_set(True)
    objectToSelect = bpy.data.objects[low_poly_name]
    objectToSelect.select_set(True)       
    bpy.context.view_layer.objects.active = objectToSelect

    #from the low poly mat make baking text node
    mat=obj.data.materials[0]
    mat.use_nodes = True
    nodes = mat.node_tree.nodes

    ###for loop per image
    bake_count=0
    #textures_to_bake=["AO","NORMAL"]
    textures_to_bake=["NORMAL"]

    def bake_channel (channel_item):
        tex = nodes.new('ShaderNodeTexImage')
        dimensions=int(text_resolution[text_res_chosen])

        bake_name="bake"+str(bake_count)
        pic=bpy.data.images.new(name=bake_name,width=dimensions,height=dimensions,alpha=False,float_buffer=False)
        tex.image = pic
        pic.colorspace_settings.name = 'Non-Color'
        nodes.active = tex
        #bpy.ops.object.select_all(action='DESELECT')

        #set bake settings correctly and the image file as target
        nodes.active = tex
        bpy.context.scene.render.bake.use_selected_to_active = True
        
        #bpy.context.scene.render.bake.cage_extrusion = 0.3

###############################################################################################################
#fixing washed out?
        bg = bpy.context.scene.world.node_tree.nodes["Background"]
        bg.inputs[1].default_value = 0.0


        scene = bpy.context.scene
        scene.render.bake.use_clear = True
        scene.render.bake.margin = 16
        #bpy.render.bake.use_selected_to_active = False
        scene.render.bake.normal_space = 'TANGENT'

        scene.render.bake.cage_extrusion = float(bake_extrusion)


        scene.render.bake.target = 'IMAGE_TEXTURES'
        scene.cycles.bake_type = channel_item

        scene.cycles.samples = 1024

        bpy.context.scene.unit_settings.system = 'METRIC'
        bpy.context.scene.unit_settings.scale_length = 1.0

        #scene.cycles.bake_samples = 1024

        #bake and save to location
        location_save=folder_path+"/"+mesh_name
        name_of_file=channel_item
        file_format=format_options[format_chosen]
        object_name=mesh_name
        file_name=location_save+"/T_"+object_name+"_"+name_of_file+file_format
        bpy.ops.object.bake(type=channel_item, save_mode='INTERNAL')
        #bake_count+=1
        
        if os.path.isfile(file_name):
            print("its a file!!")
            print(file_name)
            os.remove(file_name)
            print("removed file")

        #bpy.ops.image.save_as(save_as_render=False, filepath=location_save, relative_path=True, show_multiview=False, use_multiview=False)
        pic.filepath_raw = file_name
        pic.file_format = format_long[format_chosen]



        pic.save()

    for chan_item in textures_to_bake:
        #create empty image for ao bake
        bake_channel(chan_item)
        
    ####bake better position, curvature
    noise_location=str(os.path.dirname(__file__))+"/icons/blend_noise.tga"
    noise_image=bpy.data.images.load(noise_location)
    ambient_graph=curvature_graph=str(os.path.dirname(__file__))+"/shader_graphs/ambient.py"
    curvature_graph=str(os.path.dirname(__file__))+"/shader_graphs/curvature.py"
    dirt_edges_graph=str(os.path.dirname(__file__))+"/shader_graphs/dirt_edge.py"
    dirt_from_ground=str(os.path.dirname(__file__))+"/shader_graphs/dirt_from_ground.py"

    ###get the shader graph script as its long
    #makes curvature
    with open(ambient_graph) as f:
        exec(f.read())

    with open(curvature_graph) as f:
        exec(f.read())
        
    with open(dirt_edges_graph) as f:
        exec(f.read())
        
    with open(dirt_from_ground) as f:
        exec(f.read())


    #bpy.context.scene.cycles.bake_type = 'EMIT'
    #####



    ##this wipes everything clean
    bpy.ops.object.delete()
Editor is loading...
Leave a Comment