Download backend/dsl_parser.py from humut/mvpkillcaddss: direct link, hf CLI and curl.
- Browser
- Download file 2.6 kB
-
https://huggingface.co/humut/mvpkillcaddss/resolve/main/backend/dsl_parser.py
- Command line
-
hf download hf://humut/mvpkillcaddss/backend/dsl_parser.py
-
curl -L -o dsl_parser.py https://huggingface.co/humut/mvpkillcaddss/resolve/main/backend/dsl_parser.py
2.6 kB
| """DSAFD DSL Parser — trimesh valós 3D mesh generálás""" | |
| import re | |
| import numpy as np | |
| import trimesh | |
| def to_vec(raw, default): | |
| try: | |
| parts = [float(x.strip()) for x in raw.strip('[]').split(',')[:3]] | |
| while len(parts) < 3: | |
| parts.append(0.0) | |
| return np.array(parts, dtype=np.float64) | |
| except Exception: | |
| return np.array(default, dtype=np.float64) | |
| def parse_dsl_to_mesh(dsl: str) -> dict: | |
| lower = dsl.lower() | |
| meshes = [] | |
| primitives = re.findall(r'primitive\s+(\w+)\s*\(([^)]*)\)', lower) | |
| for ptype, params_str in primitives: | |
| params = {} | |
| for part in params_str.split(','): | |
| part = part.strip() | |
| if ':' in part: | |
| k, v = part.split(':', 1) | |
| params[k.strip()] = v.strip() | |
| center = to_vec(params.get('center', '0,0,0'), [0,0,0]) | |
| m = None | |
| try: | |
| if ptype == 'box': | |
| size = to_vec(params.get('size', '10,10,10'), [10,10,10]) | |
| m = trimesh.creation.box(extents=size.tolist()) | |
| elif ptype == 'sphere': | |
| radius = float(params.get('radius', '5')) | |
| m = trimesh.creation.icosphere(radius=radius, subdivisions=3) | |
| elif ptype == 'cylinder': | |
| radius = float(params.get('radius', '3')) | |
| height = float(params.get('height', '15')) | |
| m = trimesh.creation.cylinder(radius=radius, height=height, sections=32) | |
| elif ptype == 'cone': | |
| radius = float(params.get('radius_bottom', params.get('radius', '5'))) | |
| height = float(params.get('height', '10')) | |
| m = trimesh.creation.cone(radius=radius, height=height, sections=32) | |
| elif ptype == 'torus': | |
| major = float(params.get('major_radius', '10')) | |
| minor = float(params.get('minor_radius', '2')) | |
| m = trimesh.creation.torus(major_radius=major, minor_radius=minor) | |
| if m is not None: | |
| m.vertices += center | |
| meshes.append(m) | |
| except Exception: | |
| continue | |
| if not meshes: | |
| mesh = trimesh.creation.box(extents=[2, 2, 2]) | |
| else: | |
| mesh = meshes[0] | |
| for m in meshes[1:]: | |
| try: | |
| mesh = mesh + m | |
| except Exception: | |
| pass | |
| vertices = mesh.vertices.flatten().tolist() | |
| indices = mesh.faces.flatten().tolist() | |
| return { | |
| "positions": vertices, | |
| "indices": indices, | |
| "vertex_count": len(mesh.vertices), | |
| } |