Created
August 31, 2023 09:43
-
-
Save Eiyeron/b08d55c69b070bffa7b5753b5fd67a24 to your computer and use it in GitHub Desktop.
Basic geometry debug REPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
def add_mesh(name, verts, faces, edges=None, col_name="Collection"): | |
if edges is None: | |
edges = [] | |
mesh = bpy.data.meshes.new(name) | |
obj = bpy.data.objects.new(mesh.name, mesh) | |
col = bpy.data.collections[col_name] | |
col.objects.link(obj) | |
bpy.context.view_layer.objects.active = obj | |
mesh.from_pydata(verts, edges, faces) | |
obj.show_name = True | |
return obj | |
def to_number(*args): | |
return (float(v) for v in args) | |
class GeometryManager: | |
def __init__(self): | |
self.objects = [] | |
def triangle(self, x0, y0, z0, x1, y1, z1, x2, y2, z2, name="Triangle"): | |
x0, y0, z0, x1, y1, z1, x2, y2, z2 = to_number(x0, y0, z0, x1, y1, z1, x2, y2, z2) | |
verts = [(x0, y0, z0), (x1, y1, z1), (x2, y2, z2)] | |
face = [[0, 1, 2]] | |
self.objects.append(add_mesh(name, verts, face)) | |
def segment(self, x0, y0, z0, x1, y1, z1, name="Segment"): | |
x0, y0, z0, x1, y1, z1 = to_number(x0, y0, z0, x1, y1, z1) | |
verts = [(x0, y0, z0), (x1, y1, z1)] | |
edge = [(0, 1)] | |
self.objects.append(add_mesh(name, verts, [], edge)) | |
def clear(self): | |
for object in self.objects: | |
bpy.data.objects.remove(object) | |
class Repl: | |
def __init__(self) -> None: | |
self.functions = {} | |
self.exit = False | |
self.register(lambda *args : self.stop(), "exit") | |
self.register(lambda *args : self.stop(), "q") | |
pass | |
def register(self, fun, name): | |
self.functions[name] = fun | |
def execute(self, command:str): | |
args = command.split() | |
if not args: | |
return | |
fun_name = args.pop(0) | |
if fun_name not in self.functions.keys(): | |
print("?") | |
return | |
self.functions[fun_name](*args) | |
def stop(self): | |
self.exit = True | |
def launch_repl(): | |
geometry = GeometryManager() | |
repl = Repl() | |
repl.register(lambda *args : geometry.triangle(*args), "t") | |
repl.register(lambda *args : geometry.segment(*args), "s") | |
repl.register(lambda *args : geometry.clear(*args), "c") | |
while not repl.exit: | |
repl.execute(input(">")) | |
launch_repl() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment