From 0dea9a7b01f1fbd517c1233a916855e998f2e362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thea=20Sch=C3=B6bl?= Date: Wed, 17 Apr 2024 21:14:53 +0200 Subject: [PATCH] feat: blender import script --- enmacompat/blender_import.py | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 enmacompat/blender_import.py diff --git a/enmacompat/blender_import.py b/enmacompat/blender_import.py new file mode 100644 index 0000000..01db731 --- /dev/null +++ b/enmacompat/blender_import.py @@ -0,0 +1,74 @@ +import json +import bpy +import math + +scale = 0.0001 + +bpy.ops.object.select_all(action='SELECT') +bpy.ops.object.delete() + +with open(r'YOUR_FILE.json', 'r') as f: + data = json.load(f) + +vertices = [] +faces = [] +i = 0 + +for section in data['sections']: + x, y = section['position'] + nx, ny = section['normal'] + left = section['left'] + right = section['right'] + + vertices.append(((x + nx * right) * scale, (y + ny * right) * scale, section['height'] * scale)) + vertices.append(((x + nx * left) * scale, (y + ny * left) * scale, section['height'] * scale)) + + if i != 0: + faces.append((i - 1, i - 2, i, i + 1)) + + i += 2 + +mesh = bpy.data.meshes.new("mesh") +mesh.from_pydata(vertices, [], faces) +obj = bpy.data.objects.new(data['name'], mesh) + +bpy.context.scene.collection.objects.link(obj) + +# ==================== +# Bank +# ==================== + +closed = True +for (n, p) in [('left', -1), ('right', 1)]: + vertices = [] + faces = [] + i = 0 + + for bank in data['bank_' + n]: + section = data['sections'][bank['section']] + x, y = section['position'] + nx, ny = section['normal'] + left = section[n] + + # I don't think this is quite right yet + distance = bank['width'] + + vertices.append(( + (x + nx * left) * scale, + (y + ny * left) * scale, + section['height'] * scale)) + vertices.append(( + (x + nx * (left - distance)) * scale, + (y + ny * (left - distance)) * scale, + (section['height'] - distance * p * bank['bank']) * scale)) + + if bank['connect']: + faces.append((i, i + 1, i + 3, i + 2)) + + i += 2 + + mesh = bpy.data.meshes.new("mesh") + mesh.from_pydata(vertices, [], faces) + obj = bpy.data.objects.new(data['name'] + '_bank_' + n, mesh) + + bpy.context.scene.collection.objects.link(obj)