extract obj
This commit is contained in:
parent
492a4b3196
commit
931817bb3a
3 changed files with 88 additions and 5 deletions
2
viewer/src/utils/python/.gitignore
vendored
2
viewer/src/utils/python/.gitignore
vendored
|
|
@ -1 +1,3 @@
|
||||||
*.ifcx
|
*.ifcx
|
||||||
|
*.obj
|
||||||
|
ifc-data-horse
|
||||||
|
|
|
||||||
35
viewer/src/utils/python/extract.py
Normal file
35
viewer/src/utils/python/extract.py
Normal 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")
|
||||||
|
|
||||||
|
|
@ -44,13 +44,11 @@ def show_nodes():
|
||||||
else:
|
else:
|
||||||
print(item)
|
print(item)
|
||||||
|
|
||||||
def add_site(site_ref):
|
def add_site(childrens):
|
||||||
ref = str(uuid.uuid4())
|
ref = str(uuid.uuid4())
|
||||||
site = {
|
site = {
|
||||||
"path" : ref,
|
"path" : ref,
|
||||||
"children" : {
|
"children" : childrens
|
||||||
"Site" : site_ref
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return site,ref
|
return site,ref
|
||||||
|
|
||||||
|
|
@ -76,6 +74,45 @@ def add_plane():
|
||||||
|
|
||||||
return plane,ref
|
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():
|
def generate():
|
||||||
ostream = None
|
ostream = None
|
||||||
try:
|
try:
|
||||||
|
|
@ -88,7 +125,16 @@ def generate():
|
||||||
plane, plane_id = add_plane()
|
plane, plane_id = add_plane()
|
||||||
items.append(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)
|
items.append(site)
|
||||||
|
|
||||||
model["data"] = items
|
model["data"] = items
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue