Fixed some bugs

This commit is contained in:
Killergnom
2024-10-19 17:46:28 +02:00
parent e63b208f06
commit d5c73e845a
7 changed files with 85 additions and 56 deletions

View File

@@ -1,15 +1,16 @@
import os
import sys
from PIL import Image
from PIL import Image # type: ignore
import time
# Define suffix lists for BaseColor, Normal, RMA/ORM
BASECOLOR_SUFFIXES = ['_alb', '_albedo', '_bc', '_basecolor', '_b']
NORMAL_SUFFIXES = ['_nrm', '_normal', '_n']
RMA_SUFFIXES = ['_rma']
ORM_SUFFIXES = ['_orm']
EMISSIVE_SUFFIXES = ['_emissive']
OPACITY_SUFFIXES = ['_opacity']
BASECOLOR_SUFFIXES = ['_alb.', '_albedo.', '_bc.', '_basecolor.', '_b.']
NORMAL_SUFFIXES = ['_nrm.', '_normal.', '_n.']
RMA_SUFFIXES = ['_rma.']
ORM_SUFFIXES = ['_orm.']
EMISSIVE_SUFFIXES = ['_emissive.']
OPACITY_SUFFIXES = ['_opacity.']
MASK_SUFFIXES = ['_mask.','_m.']
def detect_texture_type(filename):
""" Detects the type of texture based on its suffix """
@@ -25,16 +26,23 @@ def detect_texture_type(filename):
return 'Emissive'
elif any(suffix in filename.lower() for suffix in OPACITY_SUFFIXES):
return 'Opacity'
elif any(suffix in filename.lower() for suffix in MASK_SUFFIXES):
return 'Mask'
return None
def get_material_name(filename):
""" Strips the 'T_' or 'TX_' prefix and returns the material name """
""" Strips the 'T_' or 'TX_' prefix but keeps the suffix for texture type detection.
Returns the full material name without the suffix for output file naming. """
base_name = os.path.basename(filename)
# Remove the 'T_' or 'TX_' prefix
if base_name.startswith('T_'):
return base_name[2:].split('_')[0] # Assumes 'T_Material_Suffix'
base_name = base_name[2:]
elif base_name.startswith('TX_'):
return base_name[3:].split('_')[0] # Assumes 'TX_Material_Suffix'
return base_name.split('_')[0]
base_name = base_name[3:]
# Return the base_name without the suffix for output naming
return base_name.rsplit('_', 1)[0] # Split only at the last underscore
def process_textures(input_files):
""" Main function to process all textures in a folder and convert to BCR/NMO """
@@ -54,27 +62,44 @@ def process_textures(input_files):
base_path = os.path.dirname(input_files[0])
output_folder = os.path.join(base_path, 'merged')
os.makedirs(output_folder, exist_ok=True)
material_count = len(textures)
print(f"Detected {material_count} Materials to process.")
# Process each material group
for material, files in textures.items():
for index, (material, files) in enumerate(textures.items()):
basecolor_file = files.get('BaseColor')
normal_file = files.get('Normal')
rma_file = files.get('RMA')
orm_file = files.get('ORM')
emissive_file = files.get('Emissive')
opacity_file = files.get('Opacity')
if basecolor_file and normal_file and (rma_file or orm_file):
# Convert to BCR/NMO format
convert_to_bcr_nmo(material, basecolor_file, normal_file, rma_file, orm_file, emissive_file, opacity_file, output_folder)
print(f"{material}: Successfully converted.")
mask_file = files.get('Mask')
missing_files = []
failed_converts = 0
# Check for required textures
if not basecolor_file:
missing_files.append('BaseColor')
if not normal_file:
missing_files.append('Normal')
if not (rma_file or orm_file):
missing_files.append('RMA or ORM')
# Report missing files if any
if missing_files:
print(f"({index+1}/{material_count}) Skipping {material}: missing {', '.join(missing_files)}")
failed_converts += 1
else:
print(f"Skipping {material}: missing necessary files")
# Convert to BCR/NMO format
convert_to_bcr_nmo(material, basecolor_file, normal_file, rma_file, orm_file, emissive_file, opacity_file, mask_file, output_folder)
print(f"({index+1}/{material_count}) {material}: Successfully converted.")
print("+++All materials successfully converted+++")
print(f"+++{material_count - failed_converts} of {material_count} materials successfully converted+++")
time.sleep(3)
def convert_to_bcr_nmo(material, basecolor_file, normal_file, rma_file, orm_file, emissive_file, opacity_file, output_folder):
def convert_to_bcr_nmo(material, basecolor_file, normal_file, rma_file, orm_file, emissive_file, opacity_file, mask_file, output_folder):
""" Converts given textures to BCR and NMO formats """
basecolor_img = Image.open(basecolor_file).convert('RGBA')
normal_img = Image.open(normal_file).convert('RGBA')
@@ -103,6 +128,10 @@ def convert_to_bcr_nmo(material, basecolor_file, normal_file, rma_file, orm_file
if opacity_file:
opacity_img = Image.open(opacity_file).convert('L')
opacity_img.save(os.path.join(output_folder, f"{material}_OP.png"))
if mask_file:
mask_img = Image.open(mask_file).convert('L')
mask_img.save(os.path.join(output_folder, f"{material}_MASK.png"))
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: drag and drop texture files onto the script")