【MC】我的世界schematic方块坐标提取转为json
前言
主包最近在搞mc广州塔灯光效果的复刻计划(请看合集),由于主包没学过着色器编程,但是目前而言,最大的问题已经转化为只要控制每个方块的灯光颜色就够了。
所以想着用three.js做个轻量化的广州塔灯光模拟器,然而广州塔的造型比较复杂,用简单的双曲线模拟的效果和应用在游戏中的广州塔的效果完全不同。
但又觉得用代码在canvas中画方块很麻烦,所以就想有什么办法将所有mc的方块坐标导出成json格式,让three.js读取来搭建出来。
因此才有了这篇文章
步骤
- 用创世神mod把想要导出的区域导出为schem或schematic格式,见详细教程
- 在Schemat导入上一步的schem或schematic文件,这个文件一般在
版本目录\config\worldedit\schematics
- 按F12进入控制台,输入以下代码,即可输出当前原理(schematic)的方块坐标数据
const schematic = renderer.schematicManager.getFirstSchematic();
const schematicWrapper = schematic.getSchematicWrapper();
console.log(schematicWrapper.blocks());
- 右键点击输出内容,“复制为object”,把复制内容粘贴到一个新的txt文件保存,再将txt文件扩展名改为
.json
- 需要用以下python代码删除空气方块和其他不必要的属性,即可完成schematic方块坐标提取为json文件,之后就能利用这个json文件做更多的事情了
点击查看代码
import json
import os
def process_json_file(file_path):
try:
# 读取JSON文件
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
# 检查数据是否为数组
if not isinstance(data, list):
print("错误:JSON文件内容不是一个数组。")
return
# 过滤掉name为"minecraft:air"的元素
filtered_data = [item for item in data if item.get('name') != 'minecraft:air']
# 移除每个元素的properties属性,因为对于后续导入到three.js中没什么必要
for item in filtered_data:
if 'properties' in item:
del item['properties']
# 覆盖原文件保存
with open(file_path, 'w', encoding='utf-8') as file:
json.dump(filtered_data, file, indent=2)
print(f"文件处理成功,已保存到: {file_path}")
except FileNotFoundError:
print(f"错误:文件未找到 - {file_path}")
except json.JSONDecodeError:
print("错误:文件内容不是有效的JSON格式。")
except Exception as e:
print(f"发生未知错误: {str(e)}")
def main():
print("JSON文件处理器")
file_path = input("请输入JSON文件路径: ")
# 检查文件是否存在
if not os.path.exists(file_path):
print(f"错误:文件不存在 - {file_path}")
return
process_json_file(file_path)
if __name__ == "__main__":
main()