diff --git a/Mcblend Source/MCB_API.py b/Mcblend Source/MCB_API.py index 0a9c3570..66676c36 100644 --- a/Mcblend Source/MCB_API.py +++ b/Mcblend Source/MCB_API.py @@ -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: diff --git a/Mcblend Source/Materials/Materials.py b/Mcblend Source/Materials/Materials.py index 7588d25a..b2940201 100644 --- a/Mcblend Source/Materials/Materials.py +++ b/Mcblend Source/Materials/Materials.py @@ -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')): @@ -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: diff --git a/Mcblend Source/Operators.py b/Mcblend Source/Operators.py index b8ead62e..36bbe773 100644 --- a/Mcblend Source/Operators.py +++ b/Mcblend Source/Operators.py @@ -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): @@ -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) diff --git a/Mcblend Source/__init__.py b/Mcblend Source/__init__.py index 2d9458de..f02a1cb3 100644 --- a/Mcblend Source/__init__.py +++ b/Mcblend Source/__init__.py @@ -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() diff --git a/Mcblend.blend b/Mcblend.blend index 081a2bf1..0a1beeab 100644 Binary files a/Mcblend.blend and b/Mcblend.blend differ