Skip to content

Commit

Permalink
New unpacker, better add resource pack algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Aspirata committed Jul 9, 2024
1 parent ba2ef06 commit f208cc9
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Mcblend Source/MCB_API.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def GetConnectedSocketTo(input, tag, material=None):
return link.from_socket
except:
Absolute_Solver("005", __name__, traceback.format_exc())

def blender_version(blender_version, debug=None):

try:
Expand Down
25 changes: 16 additions & 9 deletions Mcblend Source/Materials/Materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,27 +418,34 @@ def fast_find_image(textures_paths, texture_name):
return predicted_texture
return None


# Сделать умную распаковку шобы типа если файл уже есть в распакованном виде, то использовать его от туда, а не распаковывать по новой
def zip_unpacker(root_folder, file=None):
with zipfile.ZipFile(root_folder, 'r') as zip_ref:
infolist = list(filter(lambda x: x.filename.endswith('.png'), zip_ref.infolist()))
for zip_info in infolist:
if os.path.basename(zip_info.filename) == image_name:
extract_path = os.path.join(resource_packs_directory, os.path.splitext(file if file is not None else os.path.basename(root_folder))[0])
extracted_file_path = zip_ref.extract(zip_info, extract_path)
return extracted_file_path
if os.path.isfile(extract_path):
return extract_path
else:
extracted_file_path = zip_ref.extract(zip_info, extract_path)
return extracted_file_path

elif "grass" in image_name:
if os.path.basename(zip_info.filename) == f"short_{image_name}":
extract_path = os.path.join(resource_packs_directory, os.path.splitext(file if file is not None else os.path.basename(root_folder))[0])
extracted_file_path = zip_ref.extract(zip_info, extract_path)
return extracted_file_path
if os.path.isfile(extract_path):
return extract_path
else:
extracted_file_path = zip_ref.extract(zip_info, extract_path)
return extracted_file_path

if os.path.basename(zip_info.filename) == image_name.replace("short_", ""):
extract_path = os.path.join(resource_packs_directory, os.path.splitext(file if file is not None else os.path.basename(root_folder))[0])
extracted_file_path = zip_ref.extract(zip_info, extract_path)
return extracted_file_path
if os.path.isfile(extract_path):
return extract_path
else:
extracted_file_path = zip_ref.extract(zip_info, extract_path)
return extracted_file_path

def find_image(image_name, root_folder):
if root_folder.endswith(('.zip', '.jar')):
Expand Down Expand Up @@ -652,7 +659,7 @@ def normal_texture_change(path, normal_texture_node, normal_map_node, PBSDF, ima
else:
normal_image_name = normal_texture_node.image.name

if predicted_texture_path := fast_find_image(new_image_path, normal_image_name) is None:
if predicted_texture_path := fast_find_image([new_image_path], normal_image_name) is None:
new_normal_image_path = find_image(normal_image_name, path)
else:
if r_props.use_i:
Expand Down
106 changes: 61 additions & 45 deletions Mcblend Source/Operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ def execute(self, context):
resource_packs = get_resource_packs()
if self.pack_name in resource_packs:
resource_packs[self.pack_name]["enabled"] = not resource_packs[self.pack_name]["enabled"]
set_resource_packs(resource_packs)
debugger(resource_packs[self.pack_name]["type"])
set_resource_packs(resource_packs)
return {'FINISHED'}

class MoveResourcePackUp(Operator):
Expand Down Expand Up @@ -169,63 +169,79 @@ class AddResourcePack(Operator):
Type: bpy.props.EnumProperty(items=[('Automatic', 'Automatic', ''), ('Texture & PBR', 'Texture & PBR', ''), ('Texture', 'Texture', ''), ('PBR', 'PBR', '')])

def execute(self, context):
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 list(filter(lambda x: x.endswith('.png'), 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 list(filter(lambda x: x.filename.endswith('.png'), zip_ref.infolist())):
if any(suffix in zip_info.filename for suffix in ('_n', '_s', '_e')):
def check_suffix(file):
parts = file.replace(".png", "").split('_')
return parts[-1] in ("n", "s", "e")

def define_type(filepath, self):
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(filepath):
for root, _, files in os.walk(filepath):
for file in filter(lambda x: x.endswith('.png'), files):
if check_suffix(file):
has_pbr = True
debugger(os.path.join(root, file))
else:
has_texture = True

except zipfile.BadZipFile:
print(f"Warning: '{self.filepath}' is not a valid zip file.")

else:
for root, _, files in os.walk(os.path.dirname(self.filepath)):
for file in list(filter(lambda x: x.endswith('.png'), files)):
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'

elif filepath.endswith(('.zip', '.jar')):
try:
with zipfile.ZipFile(filepath, 'r') as zip_ref:
for zip_info in filter(lambda x: x.filename.endswith('.png'), zip_ref.infolist()):
if check_suffix(zip_info.filename):
has_pbr = True
debugger(zip_info.filename)
else:
has_texture = True

except zipfile.BadZipFile:
print(f"Warning: '{filepath}' is not a valid zip file.")

else:
for root, _, files in os.walk(os.path.dirname(filepath)):
for file in filter(lambda x: x.endswith('.png'), files):
if check_suffix(file):
has_pbr = True
debugger(zip_info.filename)
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'

return resource_pack_type

resource_packs = get_resource_packs()

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": resource_pack_type, "enabled": True}
resource_packs[pack_name] = {"path": os.path.abspath(self.filepath), "type": define_type(os.path.abspath(self.filepath), self), "enabled": True}
else:
pack_name = os.path.basename(os.path.dirname(self.filepath))
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": resource_pack_type, "enabled": True}
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": define_type(os.path.dirname(self.filepath), self), "enabled": True}
else:
pack_name = os.path.basename(os.path.dirname(self.filepath))
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": resource_pack_type, "enabled": True}
resource_packs[pack_name] = {"path": os.path.dirname(self.filepath), "type": define_type(os.path.dirname(self.filepath), self), "enabled": True}

set_resource_packs(resource_packs)

return {'FINISHED'}
debugger(resource_packs[pack_name]["type"])
if resource_packs[pack_name]["path"].endswith(('.zip', '.jar')) or os.path.isdir(resource_packs[pack_name]["path"]):
set_resource_packs(resource_packs)
return {'FINISHED'}
else:
Absolute_Solver(error_name="Bad File Extension", description="Resource Pack Should be a folder or a file with .jar or .zip extension, while selected file has {Data} extension", mode="Full", data=os.path.splitext(resource_packs[pack_name]["path"])[1])
return {'CANCELLED'}

def invoke(self, context, event):
context.window_manager.fileselect_add(self)
Expand Down
3 changes: 3 additions & 0 deletions Mcblend Source/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def InitOnStart():

update_assets()

if bpy.context.preferences.addons[__package__].preferences.dev_tools:
bpy.ops.wm.console_toggle()

@persistent
def load_post_handler(dummy):
InitOnStart()
Expand Down
Binary file modified Mcblend.blend
Binary file not shown.

0 comments on commit f208cc9

Please sign in to comment.