Three.js 中 Json 对象错误

116 阅读1分钟

在将 3DS Max 中创建的 .3ds 对象通过 python 脚本转换为 .js 对象的过程中,出现了错误。错误信息显示为 "Fault in Json object - three.js"。

2、解决方案

经过分析,发现 .js 文件中存在格式错误。正确格式应该是如下所示:

var model = {

    "version" : 2,

    "scale" : 1.000000,

    "materials": [
        {
            "DbgColor" : 15658734,
            "DbgIndex" : 0,
            "DbgName" : "_fueldefault",
            "colorAmbient" : [0.5843, 0.5843, 0.5843],
            "colorDiffuse" : [0.5843, 0.5843, 0.5843],
            "colorSpecular" : [0.0, 0.0, 0.0],
            "illumination" : 2,
            "opticalDensity" : 1.5,
            "specularCoef" : 10.0,
            "transparency" : 0.0
        }
    ],

    "vertices": [16.2 .................],

    "morphTargets": [],

    "morphColors": [],

    "normals": [-0.0001,-1,-.........],

    "colors": [],

    "uvs": [[0.3286,0.22], ........],

    "faces": [[43,0,1, 2, 0], .........],

    "edges" : []

};

postMessage( model );
close();

代码例子

import bpy
import mathutils
import os

def convert_obj_three(obj_path, js_path):
    # Load the OBJ file
    bpy.ops.import_scene.obj(filepath=obj_path)

    # Get the active object
    obj = bpy.context.active_object

    # Export the object to JSON
    with open(js_path, 'w') as f:
        f.write("var model = {\n")
        f.write('"version" : 2,\n')
        f.write('"scale" : 1.000000,\n')
        f.write('"materials": [\n')
        for material in obj.data.materials:
            f.write('{\n')
            f.write('"DbgColor" : %d,\n' % material.diffuse_color.to_rgb().to_hex())
            f.write('"DbgIndex" : 0,\n')
            f.write('"DbgName" : "%s",\n' % material.name)
            f.write('"colorAmbient" : [%f, %f, %f],\n' % material.ambient_color.to_rgb())
            f.write('"colorDiffuse" : [%f, %f, %f],\n' % material.diffuse_color.to_rgb())
            f.write('"colorSpecular" : [%f, %f, %f],\n' % material.specular_color.to_rgb())
            f.write('"illumination" : %d,\n' % material.specular_intensity)
            f.write('"opticalDensity" : %f,\n' % material.alpha)
            f.write('"specularCoef" : %f,\n' % material.specular_hardness)
            f.write('"transparency" : %f\n' % material.alpha)
            f.write('},\n')
        f.write('],\n')
        f.write('"vertices": [')
        for vertex in obj.data.vertices:
            f.write('[%f, %f, %f], ' % (vertex.co.x, vertex.co.y, vertex.co.z))
        f.write('],\n')
        f.write('"morphTargets": [],\n')
        f.write('"morphColors": [],\n')
        f.write('"normals": [')
        for normal in obj.data.normals:
            f.write('[%f, %f, %f], ' % (normal.co.x, normal.co.y, normal.co.z))
        f.write('],\n')
        f.write('"colors": [],\n')
        f.write('"uvs": [')
        for uv in obj.data.uv_layers.active.data:
            f.write('[%f, %f], ' % (uv.uv.x, uv.uv.y))
        f.write('],\n')
        f.write('"faces": [')
        for face in obj.data.polygons:
            f.write('[')
            for vertex_index in face.vertices:
                f.write('%d, ' % vertex_index)
            f.write('],\n')
        f.write('],\n')
        f.write('"edges": []\n')
        f.write('};\n')
        f.write('postMessage( model );\n')
        f.write('close();\n')

    # Remove the object from the scene
    bpy.ops.object.select_all(action='SELECT')
    bpy.ops.object.delete()

# Usage
obj_path = 'path/to/object.obj'
js_path = 'path/to/object.js'
convert_obj_three(obj_path, js_path)

注意:

  1. .js 文件中的数据必须严格按照 JSON 格式编写,否则会出现错误。
  2. 顶点、法线、纹理坐标和面必须按照正确的顺序排列。
  3. 材质必须定义齐全,否则模型将无法正确显示。