Skip to content

Instantly share code, notes, and snippets.

@phenomen
Last active January 3, 2024 06:18
Show Gist options
  • Save phenomen/cef4fe7a0ab70efeecb0e96e8d26f7c1 to your computer and use it in GitHub Desktop.
Save phenomen/cef4fe7a0ab70efeecb0e96e8d26f7c1 to your computer and use it in GitHub Desktop.
Blender Script - STL to Foundry VTT Levels 3D
# Generally those values are the same for each collection from the same author
# SCALE. 1 square in Levels 3D = 1 square in Blender
var_scale = 0.05
# ROTATION. In degrees. Model should face Y axis.
var_rotation_x = 90
var_rotation_y = 0
var_rotation_z = 180
# MESH SIMPLIFICATION. Aim for 50k polygons for a human-sized model
var_decimate_ratio = 0.07
# DO NOT EDIT BELOW UNLESS YOU KNOW WHAT YOU ARE DOING
import bpy
from math import radians
obj = bpy.context.active_object
opsj = bpy.ops.object
# Apply scale
obj.scale = [var_scale,var_scale,var_scale]
# Apply rotation
obj.rotation_euler[0] = radians(var_rotation_x)
obj.rotation_euler[1] = radians(var_rotation_y)
obj.rotation_euler[2] = radians(var_rotation_z)
# Apply transforms
opsj.transform_apply(location=True, rotation=True, scale=True, isolate_users=True)
# Align to Z plane
bbox_min_z = min((obj.matrix_world @ v.co).z for v in obj.data.vertices)
obj.location.z -= bbox_min_z
# Align to X and Y center
opsj.align(align_mode='OPT_2', relative_to='OPT_2', align_axis={'X'})
opsj.align(align_mode='OPT_2', relative_to='OPT_2', align_axis={'Y'})
# Set origin
opsj.origin_set(type='ORIGIN_CURSOR', center='MEDIAN')
# Apply transforms again
opsj.transform_apply(location=True, rotation=True, scale=True, isolate_users=True)
# Apply Decimate modifier
opsj.modifier_add(type='DECIMATE')
obj.modifiers["Decimate"].use_collapse_triangulate = True
obj.modifiers["Decimate"].ratio = var_decimate_ratio
opsj.modifier_apply(modifier="Decimate")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment