Skip to content

Instantly share code, notes, and snippets.

@SalatielSauer
Last active May 5, 2024 03:50
Show Gist options
  • Save SalatielSauer/ba1d96e664b7accbeaae3a504acecdc0 to your computer and use it in GitHub Desktop.
Save SalatielSauer/ba1d96e664b7accbeaae3a504acecdc0 to your computer and use it in GitHub Desktop.
Blender python script to export object vertices to Sauerbraten

Blender + OGZ-Editor = beauty

This is a simple Blender script that exports the positions of an object's vertices to the format used by OGZ-Editor to generate .ogz (map) files.

If you want a preset scene, there is one available for blender 2.79 here: sauer_vertex_world_reference.blend (although it might work on newer versions).

The blend comes with scripts for both 2.79 and 2.80 versions and a default 1024x1024 map with skybox, the workflow consist of adding the object, changing its subdivisions if necessary and executing the script.

Tip: As an alternative to subdivision, better results can be achieved using the Remesh modifier with mode set to "Blocks", adjust the "Octree Depth" field for finer details. For text fonts, disable "Remove disconnected parts". Increase gridpower as needed to fill gaps.

# Sauer-Vertex for JSOCTA, by @SalatielSauer.
# This Blender 2.79 script saves all vertices of a selected object to a .json file which can then be imported into OGZ-Editor to generate a valid .ogz file.
# in blender: select an object and run this python script.
# optionally add subdivisions to get more vertices (in edit mode, press W key > Subdivide).
# in OGZ-Editor (salatielsauer.github.io/OGZ-Editor): click on "Import & Convert JSON", select the .json generated by Blender and save the ogz file.
# keep in mind:
# the default coordinate of the Blender's origin point is equivalent to the x0 y0 z0 coordinate of a Sauer map.
import bpy
import os
def write_vertices_to_js_file(obj, file_path):
gridpower = 1 # size of the cube
# get object's world matrix for global coordinates calculation
world_matrix = obj.matrix_world
with open(file_path, 'w') as file:
file.write('{"mapsize": 1024, "mapvars": {"maptitle": "Untitled map by Unknown (using Sauer-Vertex)", "skyboxcolour": [0, 0, 0]}, "geometry": [\n')
for vertex in obj.data.vertices:
global_coord = world_matrix * vertex.co
file.write('{"x": %f, "y": %f, "z": %f, "g": %d},\n' % (global_coord.y, global_coord.x, global_coord.z, gridpower))
file.write(']}\n')
# ensure you're in OBJECT mode and a mesh object is selected.
if bpy.context.active_object and bpy.context.active_object.type == 'MESH':
obj = bpy.context.active_object
file_path = os.path.join(bpy.path.abspath('//'), 'geometry_data.json') # save to the blend file's location
write_vertices_to_js_file(obj, file_path)
print('Vertices written to:', file_path)
else:
print('No mesh object selected or in wrong mode.')
# Sauer-Vertex for JSOCTA, by @SalatielSauer.
# This Blender >2.80 script saves all vertices of a selected object to a .json file which can then be imported into OGZ-Editor to generate a valid .ogz file.
# in blender: select an object and run this python script.
# optionally add subdivisions to get more vertices (in edit mode, press W key > Subdivide).
# in OGZ-Editor (salatielsauer.github.io/OGZ-Editor): click on "Import & Convert JSON", select the .json generated by Blender and save the ogz file.
# keep in mind:
# the default coordinate of the Blender's origin point is equivalent to the x0 y0 z0 coordinate of a Sauer map.
import bpy
import os
def write_vertices_to_js_file(obj, file_path):
gridpower = 1 # size of the cube
# get object's world matrix for global coordinates calculation
world_matrix = obj.matrix_world
with open(file_path, 'w') as file:
file.write('{"mapsize": 1024, "mapvars": {"maptitle": "Untitled map by Unknown (using Sauer-Vertex)", "skyboxcolour": [0, 0, 0]}, "geometry": [\n')
for vertex in obj.data.vertices:
global_coord = world_matrix @ vertex.co
file.write('{"x": %f, "y": %f, "z": %f, "g": %d},\n' % (global_coord.y, global_coord.x, global_coord.z, gridpower))
file.write("]}\n")
# ensure you're in OBJECT mode and a mesh object is selected.
if bpy.context.active_object and bpy.context.active_object.type == 'MESH':
obj = bpy.context.active_object
file_path = os.path.join(bpy.path.abspath("//"), 'geometry_data.json') # save to the blend file's location
write_vertices_to_js_file(obj, file_path)
print('Vertices written to:', file_path)
else:
print('No mesh object selected or in wrong mode.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment