Source code for ddg.conversion.obj.ifs

from ddg.indexedfaceset import IndexedFaceSet


[docs]def ifs_to_obj(ifs, obj_name, file_location, attribute_name="co"): """ Convert an IndexedFaceSet to an obj wavefront format. Parameters ---------- ifs : ddg.indexedfaceset.IndexedFaceSet obj_name : string Name of the surface in the obj file file_location : string Path to where the obj file should be saved attribute_name : string (default="co") Name of the IndexedFaceSet vertex attribute storing the coordinates Returns ------- None """ if not isinstance(ifs, IndexedFaceSet): raise TypeError("Input must be an IndexedFaceSet") if attribute_name not in ifs.vertex_attributes: raise AttributeError(f'IFS must have "{attribute_name}" vertex attributes.') lines = ["# generated with pyddg library."] lines.append("o " + obj_name) if ifs.vertex_attributes[attribute_name].shape[1] != 3: raise ValueError("The coordinate attribute must be 3 dimensional.") for attr in ifs.vertex_attributes[attribute_name]: lines.append(f"v {attr[0]:.4f} {attr[1]:.4f} {attr[2]:.4f}") for face in ifs.face_list(): lines.append("f " + " ".join([str(vert + 1) for vert in face])) with open(file_location, "w") as f: f.write("\n".join(lines))