extract obj

This commit is contained in:
Milovann Yanatchkov 2025-11-22 21:57:41 +01:00
parent 492a4b3196
commit 931817bb3a
3 changed files with 88 additions and 5 deletions

View file

@ -1 +1,3 @@
*.ifcx
*.obj
ifc-data-horse

View file

@ -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")

View file

@ -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