Python创建minecraft的空材质包

#PyMc_zip.py
import os
import shutil
import time

class Source_create:
    def create(self):
        global source_name
        I_chs = ['/','\\','<','>','*','│',':','?']
        while True:
            try:
                source_name = input('请命名材质包:')
                if source_name and not source_name.isspace():
                    if any(items in source_name for items in I_chs):
                        print('非法命名(?:<>│*\/)')
                    elif source_name in os.listdir(os.getcwd()):
                        print(f'已存在文件{source_name}')
                    else:
                        des = input('添加描述:')
                        pack_text = ("{\n"
      "  \"pack\": {\n"
        "    \"pack_format\": 1,\n"
        f"    \"description\": \"{des}\"\n"
      "  }\n"
    "}\n"
    )
                        try:
                            os.mkdir(os.getcwd()+'\\'+f'{source_name}')
                            print(f'创建{source_name}成功')
                            os.mkdir(os.getcwd()+'\\'+f'{source_name}'+'\\'+'assets')
                            print('创建assets成功')
                            os.mkdir(os.getcwd()+'\\'+f'{source_name}'+'\\'+'assets'+'\\'+'minecraft')
                            print('创建minecraft成功')
                            os.mkdir(os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft'+'\\'+'textures')
                            print('创建textures成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'font')
                            print('创建font成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures'+ '\\' +'blocks')
                            print('创建blocks成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures'+ '\\' +'colormap')
                            print('创建clormap成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures'+ '\\' +'effect')
                            print('创建effect成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures'+ '\\' +'entity')
                            print('创建entity成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'environment')
                            print('创建environment成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'font')
                            print('创建font成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'gui')
                            print('创建gui成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'items')
                            print('创建items成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'misc')
                            print('创建misc成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'map')
                            print('创建map成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'models')
                            print('创建models成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'painting')
                            print('创建painting成功')
                            os.mkdir(
                                os.getcwd() + '\\' + f'{source_name}' + '\\' + 'assets' + '\\' + 'minecraft' + '\\' + 'textures' + '\\' + 'particle')
                            print('创建particle成功')

                            with open(os.getcwd()+'\\'+f'{source_name}'+'\\'+'pack.mcmeta','wb') as pack_file:
                                pack_file.write(pack_text.encode())
                                print('pack.mcmeta创建成功')
                                time.sleep(1)
                                print('材质包文件已创建完成.')
                                break
                        except:
                            print('程序故障.')
                            break
            except:
                break
                print('程序出现故障.')

Source_create = Source_create()

class Source_png:
    def png(self):
        try:
            photo_path = input('添加图片路径:')
            if os.getcwd()+'\\'+f'{source_name}':
                if os.path.splitext(photo_path)[1] == '.png':
                    try:
                        if 'pack.png' in os.listdir(os.getcwd()+'\\'+f'{source_name}'):
                            try:
                                shutil.rmtree((os.getcwd()+'\\'+f'{source_name}'+'\\'+'pack.png'))
                                shutil.copy(photo_path, os.getcwd() + '\\' + f'{source_name}'+'\\'+'pack.png')
                                print('图片添加成功')
                                time.sleep(1)
                                print('材质包创建成功')
                                time.sleep(2)
                            except:
                                print('添加图片失败')
                                time.sleep(1)
                                print('材质包创建成功')
                                time.sleep(2)
                        else:
                            try:
                                shutil.copy(photo_path, os.getcwd() + '\\' + f'{source_name}'+'\\'+'pack.png')
                                print('图片添加成功')
                                time.sleep(1)
                                print('材质包创建成功')
                                time.sleep(2)
                            except:
                                print('添加图片失败')
                                time.sleep(1)
                                print('材质包创建成功')
                                time.sleep(2)
                    except:
                        print('图片加载失败')
                        time.sleep(1)
                        print('材质包创建成功')
                        time.sleep(2)
                else:
                    print("添加失败,目标不是'.png'图片格式")
                    time.sleep(1)
                    print('材质包创建成功')
                    time.sleep(2)
            else:
                print('无效路径.')
                time.sleep(1)
                print('材质包创建成功')
                time.sleep(2)
        except:
            print('图片路径有误或图片不存在.')
            time.sleep(1)
            print('材质包创建成功')
            time.sleep(2)

Source_png = Source_png()

class zip:
    def zip_pack(self):
        import zipfile
        waiting_pack = os.getcwd()+'\\'+f'{source_name}'
        aiming_pack = os.getcwd()+'\\'+f'{source_name}.zip'
        folder_path = waiting_pack
        zip_file = aiming_pack
        with zipfile.ZipFile(zip_file, 'w') as zipf:
            # 使用递归函数遍历文件夹及其子文件夹
            def traverse_folder(folder):
                for item in os.listdir(folder):
                    item_path = os.path.join(folder, item)
                    if os.path.isdir(item_path):
                        # 将空文件夹也添加到压缩包中
                        zipf.write(item_path, os.path.relpath(item_path, folder_path))
                        traverse_folder(item_path)
                    elif os.path.isfile(item_path):
                        zipf.write(item_path, os.path.relpath(item_path, folder_path))

            traverse_folder(folder_path)
        print('添加压缩包成功.')
zip = zip()


#Mc_mainfile.py
from PyMc import PyMc_zip

PyMc_zip.Source_create.create()
PyMc_zip.Source_png.png()
PyMc_zip.zip.zip_pack()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值