www.xbdev.net
xbdev - software development
Monday July 15, 2024
Home | Contact | Support | 3D File Formats The bits and bytes... | JSON File Formats JSON Can Be Used For 3D Data
     
 

JSON File Formats

JSON Can Be Used For 3D Data

 

The json file format is a great! Not just for 3d data but for lots of things. It's very popular for web-based systems (storing and transfering data).

The file format itself is text-based and easy to read - but even greater - lots of languages (Java, Python, JavaScript, ..) all have builtin or free packages for parsing or creating json files! So you don't need to do the work of processing each character.

The file format is simple enough - as it's just text.

Blender Exporter (Vertices and Faces)


A few cool little things - in blender you can export your data as 'json'. Just open up Blender (3.x), select the object you want to export - select the
Scripting
tab at the top - and you can write/run small python programs.

The following is a juicy little python script for Blender that exports the mesh data.

import bpy
import json

def export_mesh_data(obj):
    
mesh_data = {}
    
    
vertices = []
    for 
vertex in obj.data.vertices:
        
vertices.append((vertex.co.xvertex.co.yvertex.co.z))
    
mesh_data['vertices'] = vertices
    
    faces 
= []
    for 
face in obj.data.polygons:
        
faces.append([for v in face.vertices])
    
mesh_data['faces'] = faces
    
    
return mesh_data

def export_json
(filename):
    
scene bpy.context.scene
    selected_objects 
= [obj for obj in scene.objects if obj.select_get() and obj.type == 'MESH']

    
exported_data = {}
    for 
obj in selected_objects:
        
exported_data[obj.name] = export_mesh_data(obj)

    
# Specify the full path where you have write permissions
    
with open(filename'w') as file:
        
json.dump(exported_datafileindent=4)


# Change the path to your desired export location
export_json("C:/exported_temp.json")



The json file for a cube.json from the above exported script looks like:

{"Cube":{
  
"vertices":[[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]],
  
"faces":   [[0,4,6,2],[3,2,6,7],[7,6,4,5],[5,1,3,7],[1,0,2,3],[5,4,0,1]]}}


The Blender window with the cube and the script should look something like this:


Full window screenshot showing Blender, script, cube model. Red circles show the cube mesh (selected), scripting tab, script an...
Full window screenshot showing Blender, script, cube model. Red circles show the cube mesh (selected), scripting tab, script and the run button.



Triangles - Only Triangles (NO QUADS)


The previous example worked - but the exported data kept the format that Blender was using - so if it was storing surfaces as quads the exported JSON contained quads! But most of the time we want triangles!

So a few extra lines - we convert any quads to triangles


import bpy
import json

def export_mesh_data(obj):
    
mesh_data = {}
    
    
vertices = []
    for 
vertex in obj.data.vertices:
        
vertices.append((vertex.co.xvertex.co.yvertex.co.z))
    
mesh_data['vertices'] = vertices
    
    faces 
= []
    for 
face in obj.data.polygons:
        if 
len(face.vertices) == 4:  # Check if the face is a quad
            # Convert quad to two triangles
            
faces.append([face.vertices[0], face.vertices[1], face.vertices[2]])
            
faces.append([face.vertices[0], face.vertices[2], face.vertices[3]])
        else:
            
faces.append([for v in face.vertices])
    
mesh_data['faces'] = faces
    
    
return mesh_data

def export_json
(filename):
    
scene bpy.context.scene
    selected_objects 
= [obj for obj in scene.objects if obj.select_get() and obj.type == 'MESH']

    
exported_data = {}
    for 
obj in selected_objects:
        
exported_data[obj.name] = export_mesh_data(obj)

    
# Specify the full path where you have write permissions
    
with open(filename'w') as file:
        
json.dump(exported_datafileindent=4)

# Change the path to your desired export location
export_json("C:/Pro2/exported_cube.json")



This is what the modified export look like now (3 indices per face - triangle):

{"Cube":{"vertices":[[1,1,1],[1,1,-1],[1,-1,1],[1,-1,-1],[-1,1,1],[-1,1,-1],[-1,-1,1],[-1,-1,-1]],
         
"faces":[[0,4,6],[0,6,2],[3,2,6],[3,6,7],[7,6,4],[7,4,5],[5,1,3],[5,3,7],[1,0,2],[1,2,3],[5,4,0],[5,0,1]]}}



Blender Exporter (Vertices/Faces/UV/Color)


Taking the simple example from previously further - you can export lots of other mesh data (e.g. UVs and Colors).

import bpy
import json

def export_mesh_data(obj):
    print(
"Exporting mesh data for object:"obj.name)
    
mesh_data = {}
    
    
vertices = []
    for 
vertex in obj.data.vertices:
        
vertices.append((vertex.co.xvertex.co.yvertex.co.z))
    
mesh_data['vertices'] = vertices
    
print("Vertices exported:"len(vertices))
    
    
faces = []
    for 
face in obj.data.polygons:
        
faces.append([for v in face.vertices])
    
mesh_data['faces'] = faces
    
print("Faces exported:"len(faces))
    
    
# Check if vertex colors are present
    
if obj.data.vertex_colors:
        
vertex_colors = {}
        for 
layer_namecolor_layer in obj.data.vertex_colors.items():
            
vertex_colors[layer_name] = []
            for 
loop_indexloop in enumerate(obj.data.loops):
                
color color_layer.data[loop_index].color
                vertex_colors
[layer_name].append((color.rcolor.gcolor.bcolor.a))
        
mesh_data['vertex_colors'] = vertex_colors
        
print("Vertex colors exported for layers:", list(vertex_colors.keys()))
    
    
# Check if UV coordinates are present
    
if obj.data.uv_layers:
        
uv_coordinates = {}
        for 
layer_nameuv_layer in obj.data.uv_layers.items():
            
uv_coordinates[layer_name] = []
            for 
loop_indexloop in enumerate(obj.data.loops):
                
uv_coordinate uv_layer.data[loop_index].uv
                uv_coordinates
[layer_name].append((uv_coordinate.xuv_coordinate.y))
        
mesh_data['uv_coordinates'] = uv_coordinates
        
print("UV coordinates exported for layers:", list(uv_coordinates.keys()))
    
    return 
mesh_data

def export_json
(filename):
    print(
"Starting JSON export...")
    
scene bpy.context.scene
    selected_objects 
= [obj for obj in scene.objects if obj.select_get() and obj.type == 'MESH']
    print(
"Selected objects for export:", [obj.name for obj in selected_objects])

    
exported_data = {}
    for 
obj in selected_objects:
        
exported_data[obj.name] = export_mesh_data(obj)

    
with open(filename'w') as file:
        
json.dump(exported_datafileindent=4)

    print(
"JSON export completed.")

# Change the path to your desired export location
export_json("C:/modelwithvfuvc.json")


If yoyu open the json file in your favorite text editor (like notpad++) you can view the structure/layout. For example:


Exported json file - viewed in notepad++ but have minimized the numbers so you can see the overall structure.
Exported json file - viewed in notepad++ but have minimized the numbers so you can see the overall structure.



You need normals! Without them how do you add lighting?


So let's extend the json file exporter so it includes normal data.

import bpy
import json

def export_mesh_data(obj):
    
mesh_data = {}
    
    
vertices = []
    
normals = []  # New list to store vertex normals
    
uv_coordinates = []  # New list to store UV coordinates
    
vertex_colors = []  # New list to store vertex colors
    
    
for vertex in obj.data.vertices:
        
vertices.append((vertex.co.xvertex.co.yvertex.co.z))
    
    
# Extract vertex normals
    
for loop in obj.data.loops:
        
normals.append((loop.normal.xloop.normal.yloop.normal.z))
    
    
# Extract UV coordinates
    
uv_layer obj.data.uv_layers.active.data if obj.data.uv_layers.active else None
    
if uv_layer:
        for 
loop in obj.data.loops:
            
uv_coordinate uv_layer[loop.index].uv
            uv_coordinates
.append((uv_coordinate.xuv_coordinate.y))
    
    
# Extract vertex colors
    
color_layer obj.data.vertex_colors.active.data if obj.data.vertex_colors.active else None
    
if color_layer:
        for 
loop in obj.data.loops:
            
color color_layer[loop.index].color
            vertex_colors
.append((color.rcolor.gcolor.bcolor.a))
    
    
mesh_data['vertices'] = vertices
    mesh_data
['normals'] = normals
    mesh_data
['uv_coordinates'] = uv_coordinates
    mesh_data
['vertex_colors'] = vertex_colors
    
    faces 
= []
    for 
face in obj.data.polygons:
        if 
len(face.vertices) == 4:  # Check if the face is a quad
            # Convert quad to two triangles
            
faces.append([face.vertices[0], face.vertices[1], face.vertices[2]])
            
faces.append([face.vertices[0], face.vertices[2], face.vertices[3]])
        else:
            
faces.append([for v in face.vertices])
    
mesh_data['faces'] = faces
    
    
return mesh_data

def export_json
(filename):
    
scene bpy.context.scene
    selected_objects 
= [obj for obj in scene.objects if obj.select_get() and obj.type == 'MESH']

    
exported_data = {}
    for 
obj in selected_objects:
        
exported_data[obj.name] = export_mesh_data(obj)

    
# Specify the full path where you have write permissions
    
with open(filename'w') as file:
        
json.dump(exported_datafileindent=4)

# Change the path to your desired export location
export_json("C:/modelwithvfuvcn.json")


Bringing it all together - Adding Export Button


In Blender, as you must already know, there is an 'export' option in the File menu. So let's take the 'json' exporter and build it into an export button.


JSON Exporter Button
JSON Exporter Button



import bpy
import json


def export_mesh_data(obj):
    
mesh_data = {}
    
    
vertices = []
    
normals = []  # New list to store vertex normals
    
uv_coordinates = []  # New list to store UV coordinates
    
vertex_colors = []  # New list to store vertex colors
    
    
for vertex in obj.data.vertices:
        
vertices.append((vertex.co.xvertex.co.yvertex.co.z))
    
    
# Extract vertex normals
    
for loop in obj.data.loops:
        
normals.append((loop.normal.xloop.normal.yloop.normal.z))
    
    
# Extract UV coordinates
    
uv_layer obj.data.uv_layers.active.data if obj.data.uv_layers.active else None
    
if uv_layer:
        for 
loop in obj.data.loops:
            
uv_coordinate uv_layer[loop.index].uv
            uv_coordinates
.append((uv_coordinate.xuv_coordinate.y))
    
    
# Extract vertex colors
    
color_layer obj.data.vertex_colors.active.data if obj.data.vertex_colors.active else None
    
if color_layer:
        for 
loop in obj.data.loops:
            
color color_layer[loop.index].color
            vertex_colors
.append((color.rcolor.gcolor.bcolor.a))
    
    
    
# Flatten the ararys - instead of = [ [2,3], [2,1], [2,4,5] ] - you have - [2,3,2,1,2,4,5]
    
mesh_data['vertices'] = [ list(i) for i in vertices ]
    
mesh_data['normals'] = [ list(i) for i in normals ]
    
mesh_data['uv_coordinates'] = [ list(i) for i in uv_coordinates 
    
mesh_data['vertex_colors'] = [ list(i) for i in vertex_colors 
    
    
mesh_data['vertices'] = summesh_data['vertices'], [] )
    
mesh_data['normals'] = summesh_data['normals'], [] )
    
mesh_data['uv_coordinates'] = summesh_data['uv_coordinates'], [] )
    
mesh_data['vertex_colors'] = summesh_data['vertex_colors'], [] )
    
    
    
faces = []
    for 
face in obj.data.polygons:
        if 
len(face.vertices) == 4:  # Check if the face is a quad
            # Convert quad to two triangles
            
faces.append([face.vertices[0], face.vertices[1], face.vertices[2]])
            
faces.append([face.vertices[0], face.vertices[2], face.vertices[3]])
        else:
            
faces.append([for v in face.vertices])
    
mesh_data['faces'] = [ list(i) for i in faces 
    
mesh_data['faces'] = summesh_data['faces'], [] )
    
    return 
mesh_data

def export_json
(filename):
    
scene bpy.context.scene
    selected_objects 
= [obj for obj in scene.objects if obj.select_get() and obj.type == 'MESH']

    
exported_data = {}
    for 
obj in selected_objects:
        
exported_data[obj.name] = export_mesh_data(obj)

    
# Specify the full path where you have write permissions
    
with open(filename'w') as file:
        
json.dump(exported_datafileindent=4)
        

def write_some_data(contextfilepathuse_some_setting):
    print(
"running write_some_data...")
    
export_jsonfilepath )

    return {
'FINISHED'}


# ExportHelper is a helper class, defines filename and
# invoke() function which calls the file selector.
from bpy_extras.io_utils import ExportHelper
from bpy
.props import StringPropertyBoolPropertyEnumProperty
from bpy
.types import Operator

class ExportSomeData(OperatorExportHelper):
    
"""This appears in the tooltip of the operator and in the generated docs"""
    
bl_idname "export_test.some_data"  # important since its how bpy.ops.import_test.some_data is constructed
    
bl_label "Export Some Data"

    
# ExportHelper mix-in class uses this.
    
filename_ext ".json"

    
filter_globStringProperty(
        default=
"*.json",
        
options={'HIDDEN'},
        
maxlen=255,  # Max internal buffer length, longer would be clamped.
    
)

    
# List of operator properties, the attributes will be assigned
    # to the class instance from the operator settings before calling.
    
use_settingBoolProperty(
        
name="Example Boolean",
        
description="Example Tooltip",
        default=
True,
    )

    
typeEnumProperty(
        
name="Example Enum",
        
description="Choose between two items",
        
items=(
            (
'OPT_A'"First Option""Description one"),
            (
'OPT_B'"Second Option""Description two"),
        ),
        default=
'OPT_A',
    )

    
def execute(selfcontext):
        return 
write_some_data(contextself.filepathself.use_setting)


# Only needed if you want to add into a dynamic menu
def menu_func_export(selfcontext):
    
self.layout.operator(ExportSomeData.bl_idnametext="JSON Exporter (XBDEV)")


# Register and add to the "file selector" menu (required to use F3 search "JSON Exporter" for quick access).
def register():
    
bpy.utils.register_class(ExportSomeData)
    
bpy.types.TOPBAR_MT_file_export.append(menu_func_export)


def unregister():
    
bpy.utils.unregister_class(ExportSomeData)
    
bpy.types.TOPBAR_MT_file_export.remove(menu_func_export)


if 
__name__ == "__main__":
    
register()

    
# test call
    # bpy.ops.export_test.some_data('INVOKE_DEFAULT')



The export example has some redundant options for the popup dialog - quick and dirty example to give you something that works and lets you focus on the JSON format for you cool demos! Feel free to use it and tidy it up - adding other features/options (e.g., add menu options to the exporter to enable/disable things in the output JSON file)

The cube example would look like this:

{"Cube":{
"vertices":[1,1,1,1,1,-1,1,-1,1,1,-1,-1,-1,1,1,-1,1,-1,-1,-1,1,-1,-1,-1],
"normals":[0,0,1,0,0,1,0,0,1,0,0,1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,0,0,-1,0,0,-1,0,0,-1,0,0,-1,1,0,0,1,0,0,1,0,0,1,0,0,0,1,0,0,1,0,0,1,0,0,1,0],
"uv_coordinates":[0.625,0.5,0.875,0.5,0.875,0.75,0.625,0.75,0.375,0.75,0.625,0.75,0.625,1,0.375,1,0.375,0,0.625,0,0.625,0.25,0.375,0.25,0.125,0.5,0.375,0.5,0.375,0.75,0.125,0.75,0.375,0.5,0.625,0.5,0.625,0.75,0.375,0.75,0.375,0.25,0.625,0.25,0.625,0.5,0.375,0.5],
"vertex_colors":[],
"faces":[0,4,6,0,6,2,3,2,6,3,6,7,7,6,4,7,4,5,5,1,3,5,3,7,1,0,2,1,2,3,5,4,0,5,0,1]}}


Example Loading/Using JSON File in JavaScript


To give you an example of how you might use the JSON 3D file format in WebGL or WebGPU API. Just the loading and getting the data ready.

// get some model data (use json as it's easy to load/setup)
let response await fetch('./var/scripts/cube.json');
// parse json file
let json     await response.json();

console.log('vertices:'json.vertices.length );
console.log('faces:'   json.faces.length    );
console.log('normals:' json.normals.length  );

const 
positions = new Float32Arrayjson.vertices );
let   normals   = new Float32Arrayjson.normals  );
const 
indices   = new Uint16Array(  json.faces    );

// ready to go! use vertices/faces in webl or webgpu api - so easy!!




Things to Try


Extra things to do:
• Add version and signiture to the json file (when the json file is loaded you can check which version/type it is - in case it's another json file that isn't supported by your loader)
• Add a dialog that pops up for the exporter to give extra options (e.g., triangles, UV coordinates, colors, ...)
• Debug information if no object is selected (e.g., dialog that informs the user) - basically extra help to make the export script more friendly :)


Other Resources


• Visit https://free3d.com/ for lots of free test models (load in Blender - import) [LINK]
• WebGPU Demo (WebGPULab) - loads json file and renders the model (in the Web Browser) [LINK]
• Example 'cow.json' [LINK]











JSON/Blender


• Triangles (Quads to Triangles)
• Vertices
• Faces
• Texture Coordinates


import bpy
import json


def export_mesh_data(obj):
    
vertices = []
    
normals = []  
    
coordinates = []
    
colors = []  
    
faces = []

    
uv_layer obj.data.uv_layers.active.data
    
    color_layer 
obj.data.vertex_colors.active.data if obj.data.vertex_colors.active else None
    
            
    
for face in obj.data.polygons:
        if 
len(face.vertices) == 4:  # Check if the face is a quad
            # Convert quad to two triangles
            
i0 face.vertices[0]
            
i1 face.vertices[1]
            
i2 face.vertices[2]
            
i3 face.vertices[3]
            
            
obj.data.vertices
            vertices
.extend( [ v[i0].co.x, -v[i0].co.zv[i0].co.] );
            
vertices.extend( [ v[i1].co.x, -v[i1].co.zv[i1].co.] );
            
vertices.extend( [ v[i2].co.x, -v[i2].co.zv[i2].co.] );
            
            
vertices.extend( [ v[i0].co.x, -v[i0].co.zv[i0].co.] );
            
vertices.extend( [ v[i2].co.x, -v[i2].co.zv[i2].co.] );
            
vertices.extend( [ v[i3].co.x, -v[i3].co.zv[i3].co.] );
            
            
faces.extend( [ len(faces)+0len(faces)+1len(faces)+] );
            
faces.extend( [ len(faces)+0len(faces)+1len(faces)+] );             

            
ti0 face.loop_indices[0]
            
ti1 face.loop_indices[1]
            
ti2 face.loop_indices[2]
            
ti3 face.loop_indices[3]
            
                        
            if 
1:
                
n0 obj.data.loops[ti0].normal
                n1 
obj.data.loops[ti1].normal
                n2 
obj.data.loops[ti2].normal
                n3 
obj.data.loops[ti3].normal
                
                normals
.extend( [ n0.x, -n0.zn0.] );
                
normals.extend( [ n1.x, -n1.zn1.] );
                
normals.extend( [ n2.x, -n2.zn2.] );
                
                
normals.extend( [ n0.x, -n0.zn0.] );
                
normals.extend( [ n2.x, -n2.zn2.] );
                
normals.extend( [ n3.x, -n3.zn3.] );
            
            
            if 
uv_layer:
                
t0 uv_layerti0 ].uv
                t1 
uv_layerti1 ].uv
                t2 
uv_layerti2 ].uv
                t3 
uv_layerti3 ].uv
                
                coordinates
.extend( [ t0.x1.0 t0.] )
                
coordinates.extend( [ t1.x1.0 t1.] )
                
coordinates.extend( [ t2.x1.0 t2.] )
                                              
                
coordinates.extend( [ t0.x1.0 t0.] )
                
coordinates.extend( [ t2.x1.0 t2.] )
                
coordinates.extend( [ t3.x1.0 t3.] )
            
            if 
color_layer:
                
c0 color_layerti0 ].color
                c1 
color_layerti1 ].color
                c2 
color_layerti2 ].color
                c3 
color_layerti3 ].color
                
                colors
.extend( [ c0.rc0.gc0.] )
                
colors.extend( [ c1.rc1.gc1,] )
                
colors.extend( [ c2.rc2.gc2.] )
                                              
                
colors.extend( [ c0.rc0.gc0.] )
                
colors.extend( [ c2.rc2.gc2.] )
                
colors.extend( [ c3.rc3.gc3.] )
            

        else:
            
i0 face.vertices[0]
            
i1 face.vertices[1]
            
i2 face.vertices[2]
            
            
obj.data.vertices
            vertices
.extend( [ v[i0].co.x, -v[i0].co.zv[i0].co.] );
            
vertices.extend( [ v[i1].co.x, -v[i1].co.zv[i1].co.] );
            
vertices.extend( [ v[i2].co.x, -v[i2].co.zv[i2].co.] );
            
            
faces.extend( [ len(faces)+0len(faces)+1len(faces)+] );
            
            
ti0 face.loop_indices[0]
            
ti1 face.loop_indices[1]
            
ti2 face.loop_indices[2]
            
            if 
1:
                
n0 obj.data.loops[ti0].normal
                n1 
obj.data.loops[ti1].normal
                n2 
obj.data.loops[ti2].normal
                
                normals
.extend( [ n0.x, -n0.zn0.] );
                
normals.extend( [ n1.x, -n1.zn1.] );
                
normals.extend( [ n2.x, -n2.zn2.] );
            
            if 
uv_layer:
                
t0 uv_layerti0 ].uv
                t1 
uv_layerti1 ].uv
                t2 
uv_layerti2 ].uv
                
                coordinates
.extend( [ t0.x1.0 t0.] )
                
coordinates.extend( [ t1.x1.0 t1.] )
                
coordinates.extend( [ t2.x1.0 t2.] )
            
            if 
color_layer:
                
c0 color_layerti0 ].color
                c1 
color_layerti1 ].color
                c2 
color_layerti2 ].color
                
                colors
.extend( [ c0.rc0.gc0.] )
                
colors.extend( [ c1.rc1.gc1,] )
                
colors.extend( [ c2.rc2.gc2.] )
         
    
    return { 
'vertices'    vertices,
             
'faces'       faces,
             
'coordinates' coordinates,
             
'normals'     normals,
             
'colors'      colors }


def export_json(filename):
    
scene bpy.context.scene
    selected_objects 
= [obj for obj in scene.objects if obj.select_get() and obj.type == 'MESH']

    
exported_data = {}
    for 
obj in selected_objects:
        
exported_data[obj.name] = export_mesh_data(obj)

    
# Specify the full path where you have write permissions
    
with open(filename'w') as file:
        
json.dump(exported_datafileindent=4)


def write_some_data(contextfilepathuse_some_setting):
    print(
"running write_some_data...")
    
export_json(filepath)

    return {
'FINISHED'}



if 
__name__ == "__main__":
    
write_some_data0,  'C:/Pro2/crate.json' );


Y-Axis


import bpy
import json


def export_mesh_data(obj):
    
vertices = []
    
normals = []  
    
coordinates = []
    
colors = []  
    
faces = []

    
uv_layer obj.data.uv_layers.active.data
    
    color_layer 
obj.data.vertex_colors.active.data if obj.data.vertex_colors.active else None
    
            
    
for face in obj.data.polygons:
        if 
len(face.vertices) == 4:  # Check if the face is a quad
            # Convert quad to two triangles
            
i0 face.vertices[0]
            
i1 face.vertices[1]
            
i2 face.vertices[2]
            
i3 face.vertices[3]
            
            
obj.data.vertices
            vertices
.extend( [ v[i0].co.xv[i0].co.zv[i0].co.] );
            
vertices.extend( [ v[i1].co.xv[i1].co.zv[i1].co.] );
            
vertices.extend( [ v[i2].co.xv[i2].co.zv[i2].co.] );
            
            
vertices.extend( [ v[i0].co.xv[i0].co.zv[i0].co.] );
            
vertices.extend( [ v[i2].co.xv[i2].co.zv[i2].co.] );
            
vertices.extend( [ v[i3].co.xv[i3].co.zv[i3].co.] );
            
            
faces.extend( [ len(faces)+0len(faces)+1len(faces)+] );
            
faces.extend( [ len(faces)+0len(faces)+1len(faces)+] );             

            
ti0 face.loop_indices[0]
            
ti1 face.loop_indices[1]
            
ti2 face.loop_indices[2]
            
ti3 face.loop_indices[3]
            
                        
            if 
1:
                
n0 obj.data.loops[ti0].normal
                n1 
obj.data.loops[ti1].normal
                n2 
obj.data.loops[ti2].normal
                n3 
obj.data.loops[ti3].normal
                
                normals
.extend( [ n0.xn0.zn0.] );
                
normals.extend( [ n1.xn1.zn1.] );
                
normals.extend( [ n2.xn2.zn2.] );
                
                
normals.extend( [ n0.xn0.zn0.] );
                
normals.extend( [ n2.xn2.zn2.] );
                
normals.extend( [ n3.xn3.zn3.] );
            
            
            if 
uv_layer:
                
t0 uv_layerti0 ].uv
                t1 
uv_layerti1 ].uv
                t2 
uv_layerti2 ].uv
                t3 
uv_layerti3 ].uv
                
                coordinates
.extend( [ t0.x1.0 t0.] )
                
coordinates.extend( [ t1.x1.0 t1.] )
                
coordinates.extend( [ t2.x1.0 t2.] )
                                              
                
coordinates.extend( [ t0.x1.0 t0.] )
                
coordinates.extend( [ t2.x1.0 t2.] )
                
coordinates.extend( [ t3.x1.0 t3.] )
            
            if 
color_layer:
                
c0 color_layerti0 ].color
                c1 
color_layerti1 ].color
                c2 
color_layerti2 ].color
                c3 
color_layerti3 ].color
                
                colors
.extend( [ c0.rc0.gc0.] )
                
colors.extend( [ c1.rc1.gc1,] )
                
colors.extend( [ c2.rc2.gc2.] )
                                              
                
colors.extend( [ c0.rc0.gc0.] )
                
colors.extend( [ c2.rc2.gc2.] )
                
colors.extend( [ c3.rc3.gc3.] )
            

        else:
            
i0 face.vertices[0]
            
i1 face.vertices[1]
            
i2 face.vertices[2]
            
            
obj.data.vertices
            vertices
.extend( [ v[i0].co.xv[i0].co.zv[i0].co.] );
            
vertices.extend( [ v[i1].co.xv[i1].co.zv[i1].co.] );
            
vertices.extend( [ v[i2].co.xv[i2].co.zv[i2].co.] );
            
            
faces.extend( [ len(faces)+0len(faces)+1len(faces)+] );
            
            
ti0 face.loop_indices[0]
            
ti1 face.loop_indices[1]
            
ti2 face.loop_indices[2]
            
            if 
1:
                
n0 obj.data.loops[ti0].normal
                n1 
obj.data.loops[ti1].normal
                n2 
obj.data.loops[ti2].normal
                
                normals
.extend( [ n0.xn0.zn0.] );
                
normals.extend( [ n1.xn1.zn1.] );
                
normals.extend( [ n2.xn2.zn2.] );
            
            if 
uv_layer:
                
t0 uv_layerti0 ].uv
                t1 
uv_layerti1 ].uv
                t2 
uv_layerti2 ].uv
                
                coordinates
.extend( [ t0.x1.0 t0.] )
                
coordinates.extend( [ t1.x1.0 t1.] )
                
coordinates.extend( [ t2.x1.0 t2.] )
            
            if 
color_layer:
                
c0 color_layerti0 ].color
                c1 
color_layerti1 ].color
                c2 
color_layerti2 ].color
                
                colors
.extend( [ c0.rc0.gc0.] )
                
colors.extend( [ c1.rc1.gc1,] )
                
colors.extend( [ c2.rc2.gc2.] )
         
    
    return { 
'vertices'    vertices,
             
'faces'       faces,
             
'coordinates' coordinates,
             
'normals'     normals,
             
'colors'      colors }


def export_json(filename):
    
scene bpy.context.scene
    selected_objects 
= [obj for obj in scene.objects if obj.select_get() and obj.type == 'MESH']

    
exported_data = {}
    for 
obj in selected_objects:
        
exported_data[obj.name] = export_mesh_data(obj)

    
# Specify the full path where you have write permissions
    
with open(filename'w') as file:
        
json.dump(exported_datafileindent=4)


def write_some_data(contextfilepathuse_some_setting):
    print(
"running write_some_data...")
    
export_json(filepath)

    return {
'FINISHED'}



if 
__name__ == "__main__":
    
write_some_data0,  'C:/Pro2/torus.json' );












 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.