Files
1960-utils/Blender/_Source/L1960_Tools_1_7_0/PrepareLods.py
ProDeath21 1529bda410 1.7.0
Automated MLOD
optimized material fix
2024-03-25 20:21:32 +01:00

128 lines
4.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import bpy
import os
### GENRATE MLOD ###
plugin_dir = bpy.utils.user_resource('SCRIPTS')
plugin_path = "addons\L1960Tools"
L1960_path = os.path.join(plugin_dir, plugin_path)
colorpalettes = [
"ColorPalette_01.png",
"ColorPalette_02.png"
]
enum_palettes = []
for file in colorpalettes:
enum_palettes.append((file, file[:-4], "Select " + file[:-4] + " for MLOD"))
class EnumColorPalettes(bpy.types.PropertyGroup):
mlod_enum_selection: bpy.props.EnumProperty(
name="Color Palettes for MLOD",
items=enum_palettes,
description="Choose a palette",
default=0
)
class MESH_OT_set_up_mlod(bpy.types.Operator):
"""Set´s up a material to be used for MLOD´s"""
bl_idname = "mesh.set_up_mlod"
bl_label = "Set´s up a material to be used for MLOD´s"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
#Load Color Palettes
self.import_palettes_textures()
#Selected Mesh
obj = bpy.context.active_object
if obj not in bpy.context.selected_objects or obj.type != "MESH":
self.report({'WARNING'}, 'Select a Mesh to continue')
return {"CANCELLED"}
arr_layers = obj.data.uv_layers
if not arr_layers.get("MLOD") or len(arr_layers) > 1:
for uv_layer in reversed(arr_layers):
arr_layers.remove(uv_layer)
arr_layers.new(name = 'MLOD')
texture_filepath = os.path.join(L1960_path, colorpalettes[1])
if not len(obj.data.materials) == 0:
obj.data.materials.clear()
palette_enum_selection = context.scene.color_palettes.mlod_enum_selection
mlod_material_name = "MLOD_" + palette_enum_selection[:-4]
if not bpy.data.materials.get(mlod_material_name):
material = bpy.data.materials.new(name=mlod_material_name)
material.use_nodes = True
bsdf_node = material.node_tree.nodes["Principled BSDF"]
texImage = material.node_tree.nodes.new('ShaderNodeTexImage')
texImage.image = bpy.data.images.get(palette_enum_selection)
material.node_tree.links.new(bsdf_node.inputs['Base Color'], texImage.outputs['Color'])
obj.data.materials.append(bpy.data.materials.get(mlod_material_name))
self.report({'INFO'}, 'Mesh configured like MLOD')
return {"FINISHED"}
def import_palettes_textures(self):
for image_name in colorpalettes:
texture_name = image_name.split(".")[0]
if texture_name not in bpy.data.textures:
texture = bpy.data.textures.new(name=texture_name, type='IMAGE')
else:
texture = bpy.data.textures.get(texture_name)
if image_name not in bpy.data.images:
image = bpy.data.images.load(os.path.join(L1960_path, image_name))
else:
image = bpy.data.images.get(image_name)
texture.image = image
### PREPARE LODS ###
class MESH_OT_prepare_lods_decimate(bpy.types.Operator):
"""Copy current Mesh and apply decimate Modifier"""
bl_idname = "mesh.prepare_lods_decimate"
bl_label = "Copy current Mesh and apply decimate Modifier"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
#Selected Mesh
obj = bpy.context.active_object
if obj not in bpy.context.selected_objects or obj.type != "MESH":
self.report({'WARNING'}, 'Select a Mesh to continue')
return {"CANCELLED"}
if not obj.name[:-1].endswith('LOD'):
obj.name = obj.name + '_LOD0'
LODnumber = context.scene.lod_slider #Get from Slider
startLODcount = int(obj.name[-1])
endLODcount = startLODcount + LODnumber
for i in range (startLODcount + 1, endLODcount):
new_obj = obj.copy()
new_obj.data = obj.data.copy()
new_obj.name = obj.name[:-1] + str(i)
bpy.context.collection.objects.link(new_obj)
for t in range (startLODcount, i):
newModifierName = 'LOD_Decimate_' + str(t)
new_obj.modifiers.new(type='DECIMATE', name=newModifierName)
mod = new_obj.modifiers[newModifierName]
mod.ratio = 0.49
mod.use_collapse_triangulate = True
self.report({'INFO'}, 'LOD´s created')
return {"FINISHED"}