diff --git a/viewer/src/utils/python/.gitignore b/viewer/src/utils/python/.gitignore index 742b2ff..1ca2f05 100644 --- a/viewer/src/utils/python/.gitignore +++ b/viewer/src/utils/python/.gitignore @@ -1 +1,3 @@ *.ifcx +*.obj +ifc-data-horse diff --git a/viewer/src/utils/python/extract.py b/viewer/src/utils/python/extract.py new file mode 100644 index 0000000..aba08a9 --- /dev/null +++ b/viewer/src/utils/python/extract.py @@ -0,0 +1,35 @@ +def export_obj_raw(obj_path, points_out="points.txt", faces_out="faces.txt"): + vertices = [] + faces = [] + + with open(obj_path, "r") as f: + for line in f: + # Vertex line: v x y z + if line.startswith("v "): + parts = line.strip().split() + # parts = ["v", x, y, z] + vertices.append(parts[1:4]) + + # Face line: f i/j/k i/j/k ... + elif line.startswith("f "): + parts = line.strip().split()[1:] + # Extract only vertex indices before any "/" + face_indices = [p.split("/")[0] for p in parts] + faces.append(face_indices) + + # Write vertices + with open(points_out, "w") as f: + for v in vertices: + f.write(" ".join(v) + "\n") + + # Write faces + with open(faces_out, "w") as f: + for face in faces: + f.write(" ".join(face) + "\n") + + print(f"Export completed:\n {points_out}\n {faces_out}") + + +# Example usage: +export_obj_raw("cube.obj") + diff --git a/viewer/src/utils/python/hack.py b/viewer/src/utils/python/hack.py index ae8f0d4..0eb1b24 100644 --- a/viewer/src/utils/python/hack.py +++ b/viewer/src/utils/python/hack.py @@ -44,13 +44,11 @@ def show_nodes(): else: print(item) -def add_site(site_ref): +def add_site(childrens): ref = str(uuid.uuid4()) site = { "path" : ref, - "children" : { - "Site" : site_ref - } + "children" : childrens } return site,ref @@ -76,6 +74,45 @@ def add_plane(): return plane,ref +def add_obj(path): + + ref = str(uuid.uuid4()) + vertices = [] + faces = [] + + with open(path, "r") as f: + for line in f: + if line.startswith("v "): + parts = line.strip().split() + vertices.append(parts[1:4]) + + elif line.startswith("f "): + parts = line.strip().split()[1:] + face_indices = [p.split("/")[0] for p in parts] + faces.append(face_indices) + + f = [int(x) for sub in faces for x in sub] + v = [[float(x) for x in sub] for sub in vertices] + + mesh = { + "faceVertexIndices" : f, + "points" : v + } + + obj = { + "path" : ref, + "attributes" : { + "usd::usdgeom::mesh": mesh + } + } + + return obj, ref + + +# Example usage: +# export_obj_raw("model.obj") + + def generate(): ostream = None try: @@ -88,7 +125,16 @@ def generate(): plane, plane_id = add_plane() items.append(plane) - site, site_id = add_site(plane_id) + obj, obj_id = add_obj("sphere.obj") + items.append(obj) + + childrens = { + "Site" : plane_id, + "Obj" : obj_id + + } + + site, site_id = add_site(childrens) items.append(site) model["data"] = items