python json通过关键错误,即使似乎没有任何原因导致它

问题描述

我在我的 Blender 插件中从 json 文件加载数据时遇到问题,这给我带来了致命错误,我不知道如何解决。我多次查找错误,但这些问题从未适用于我的代码,有人可以帮我解决吗?

这是json文件(default.json)

{
    "viewport": "solid","lighting": {
        "type": "studio","lighting file": "Basic","world space lighting": {
            "enabled": false
        }
    },"color": "OBJECT","background": "THEME","backface culling": false,"x-ray": {
        "enabled": false
    },"shadow": {
        "enabled": false
    },"cavity": {
        "enabled": true,"type": "world","ridge": "${BindValue: RIDGE}","valley": "${BindValue: VALLEY}"
    },"depth of field": false,"outline": {
        "enabled": false
    },"specular lighting": false
}

这是加载它的函数

def loadPreset(location):
    with open(location) as json_file:
        print(json.load(json_file))
        data = json.load(json_file)

        with data["lighting"] as lighting:
            if lighting["type"] == "studio":
                bpy.context.space_data.shading.light = 'STUdio'
                bpy.context.space_data.shading.studio_light = lighting["lighting file"]

                with lighting["world space lighting"] as world_space_lighting:
                    bpy.context.space_data.shading.use_world_space_lighting = world_space_lighting
                    if world_space_lighting:
                        bpy.context.space_data.shading.studiolight_rotate_z = world_space_lighting["rotation"]

        bpy.context.space_data.shading.color_type = data["color"]
        bpy.context.space_data.shading.background_type = data["background"]
        bpy.context.space_data.shading.show_backface_culling = data["backface culling"]

        bpy.context.space_data.shading.show_xray =  data["x-ray"]["enabled"]
        if data["x-ray"]["enabled"]:
            bpy.context.space_data.shading.xray_alpha = data["x-ray"]["alpha"]

和这个运算符调用

class VPM_DP_DEFAULT(bpy.types.Operator):
    bl_label = "default"
    bl_idname = "view.default_display"
    
    def execute(self,context):
        data.currentViewport = "default"

        import pathlib

        loadPreset(str(pathlib.Path(__file__).parent.absolute()) + "\\viewportPresets\\basic\\default.json")
        return {'FINISHED'}

但是当我单击链接到操作员的按钮时,它给了我这个错误...

enter image description here

我环顾四周想看看如何解决这个问题,问题通常是检查不存在的密钥,但我的代码从不检查名为“预设”的密钥

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)