#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()
Python创建minecraft的空材质包
最新推荐文章于 2024-09-16 14:49:33 发布