Skip to content

Commit

Permalink
Auto pack type Detection
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspirata committed Jun 27, 2024
1 parent 4d8101e commit 5d6bc26
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 26 deletions.
24 changes: 11 additions & 13 deletions Mcblend Source/Materials/Materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,13 +474,11 @@ def find_image(image_name, root_folder):
extracted_file_path = zip_ref.extract(image_name, extract_path)
return extracted_file_path

if r_props.format_fix and "grass" in image_name:
format_fixed = os.path.join(dirpath, "short_" + image_name)
if os.path.isfile(format_fixed):
if "grass" in image_name:
if os.path.isfile(format_fixed := os.path.join(dirpath, "short_" + image_name)):
return format_fixed

format_fixed = os.path.join(dirpath, image_name.replace("short_", ""))
if os.path.isfile(format_fixed):
if os.path.isfile(format_fixed := os.path.join(dirpath, image_name.replace("short_", ""))):
return format_fixed

return None
Expand Down Expand Up @@ -947,8 +945,8 @@ def emission_texture_change(path, emission_texture_node, new_normal_image_path,
# Image Texture Update
if r_props.use_i and "MWO" not in image_texture:
for pack, pack_info in resource_packs.items():
path, type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "Texture" not in type:
path, Type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "Texture" not in Type:
continue


Expand All @@ -965,8 +963,8 @@ def emission_texture_change(path, emission_texture_node, new_normal_image_path,
# Normal Texture Update
if r_props.use_n and r_props.use_additional_textures and "MWO" not in image_texture:
for pack, pack_info in resource_packs.items():
path, type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "PBR" not in type:
path, Type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "PBR" not in Type:
continue

if new_normal_image_path := normal_texture_change(path, normal_texture_node, normal_map_node, PBSDF, image_texture_node, image_texture, new_image_path, image_path):
Expand All @@ -983,8 +981,8 @@ def emission_texture_change(path, emission_texture_node, new_normal_image_path,
# Specular Texture Update
if r_props.use_s and r_props.use_additional_textures and "MWO" not in image_texture:
for pack, pack_info in resource_packs.items():
path, type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "PBR" not in type:
path, Type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "PBR" not in Type:
continue

if new_specular_image_path := specular_texture_change(path, specular_texture_node, LabPBR_s, new_normal_image_path, PBSDF, image_texture_node, image_texture, new_image_path, image_path):
Expand All @@ -1002,8 +1000,8 @@ def emission_texture_change(path, emission_texture_node, new_normal_image_path,
# Emission Texture Update
if r_props.use_e and r_props.use_additional_textures and "MWO" not in image_texture:
for pack, pack_info in resource_packs.items():
path, type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "PBR" not in type:
path, Type, enabled = pack_info["path"], pack_info["type"], pack_info["enabled"]
if not enabled or "PBR" not in Type:
continue

if emission_texture_found := emission_texture_change(path, emission_texture_node, new_normal_image_path, new_specular_image_path, PBSDF, image_texture_node, image_texture, new_image_path, image_path):
Expand Down
5 changes: 0 additions & 5 deletions Mcblend Source/Properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ class ResourcePackProperties(PropertyGroup):
default=True,
)

format_fix: BoolProperty(
name="Format 22 Fix",
default=True,
)

use_additional_textures: BoolProperty(
name="Use Additional Textures",
default=True,
Expand Down
54 changes: 46 additions & 8 deletions Mcblend Source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,6 @@ def draw(self, context):
row = sbox.row()
row.prop(scene.resource_properties, "ignore_dublicates")

row = sbox.row()
row.prop(scene.resource_properties, "format_fix")

row = sbox.row()
row.prop(scene.resource_properties, "use_i")

Expand Down Expand Up @@ -861,22 +858,63 @@ class AddResourcePack(Operator):
bl_options = {'REGISTER', 'UNDO'}

filepath: bpy.props.StringProperty(subtype="DIR_PATH")
Type: bpy.props.EnumProperty(items=[('Texture', 'Texture', ''), ('PBR', 'PBR', ''), ('Texture & PBR', 'Texture & PBR', ''), ])
Type: bpy.props.EnumProperty(items=[('Automatic', 'Automatic', ''), ('Texture & PBR', 'Texture & PBR', ''), ('Texture', 'Texture', ''), ('PBR', 'PBR', '')])

def execute(self, context):
scene = context.scene
resource_packs = get_resource_packs()

resource_pack_type = self.Type
if resource_pack_type == "Automatic":
has_texture = False
has_pbr = False
resource_pack_type = "Texture & PBR"

if os.path.isdir(self.filepath):
for root, _, files in os.walk(self.filepath):
for file in files:
if any(suffix in file for suffix in ('_n', '_s', '_e')):
has_pbr = True
else:
has_texture = True

elif self.filepath.endswith(('.zip', '.jar')):
try:
with zipfile.ZipFile(self.filepath, 'r') as zip_ref:
for zip_info in zip_ref.infolist():
if any(suffix in zip_info.filename for suffix in ('_n', '_s', '_e')):
has_pbr = True
else:
has_texture = True
except zipfile.BadZipFile:
print(f"Warning: '{self.filepath}' is not a valid zip file.")

else:
print(f"{os.path.abspath(self.filepath)} isn't a folder, so checking {os.path.dirname(self.filepath)}")
for root, _, files in os.walk(os.path.dirname(self.filepath)):
for file in files:
print(file)
if any(suffix in file for suffix in ('_n', '_s', '_e')):
has_pbr = True
else:
has_texture = True

if has_texture and has_pbr:
resource_pack_type = 'Texture & PBR'
elif has_texture:
resource_pack_type = 'Texture'
elif has_pbr:
resource_pack_type = 'PBR'

if os.path.isdir(self.filepath) or self.filepath.endswith(('.zip', '.jar')):
if os.path.exists(os.path.abspath(self.filepath)) and os.path.basename(self.filepath) != "":
pack_name = os.path.basename(self.filepath)
resource_packs[pack_name] = {"path": os.path.abspath(self.filepath), "type": self.Type, "enabled": True}
resource_packs[pack_name] = {"path": os.path.abspath(self.filepath), "type": resource_pack_type, "enabled": True}
else:
pack_name = os.path.basename(os.path.dirname(self.filepath))
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": self.Type, "enabled": True}
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": resource_pack_type, "enabled": True}
else:
pack_name = os.path.basename(os.path.dirname(self.filepath))
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": self.Type, "enabled": True}
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": resource_pack_type, "enabled": True}

set_resource_packs(resource_packs)

Expand Down

0 comments on commit 5d6bc26

Please sign in to comment.