Ydd To Obj [new] -

# Or batch convert # converter.batch_convert(['file1.ydd', 'file2.ydd'], './output_folder/')

def convert_to_obj(self, output_path: str, include_normals: bool = True, include_tex_coords: bool = True) -> None: """Convert loaded YDD data to OBJ format""" with open(output_path, 'w') as f: # Write header f.write("# Converted from YDD to OBJ\n") f.write(f"# Vertices: {len(self.vertices)}\n") f.write(f"# Faces: {len(self.faces)}\n\n") # Write vertices (OBJ uses 1-indexing) for v in self.vertices: f.write(f"v {v[0]} {v[1]} {v[2]}\n") # Write texture coordinates if include_tex_coords and self.tex_coords: f.write("\n# Texture coordinates\n") for vt in self.tex_coords: if len(vt) == 2: f.write(f"vt {vt[0]} {vt[1]}\n") elif len(vt) == 3: f.write(f"vt {vt[0]} {vt[1]} {vt[2]}\n") # Write normals if include_normals and self.normals: f.write("\n# Normals\n") for vn in self.normals: f.write(f"vn {vn[0]} {vn[1]} {vn[2]}\n") # Write faces (convert to 1-indexed OBJ format) f.write("\n# Faces\n") for face in self.faces: if include_tex_coords and include_normals and self.tex_coords and self.normals: # v/vt/vn format f.write(f"f {face[0]+1}/{face[0]+1}/{face[0]+1} " f"{face[1]+1}/{face[1]+1}/{face[1]+1} " f"{face[2]+1}/{face[2]+1}/{face[2]+1}\n") elif include_tex_coords and self.tex_coords: # v/vt format f.write(f"f {face[0]+1}/{face[0]+1} " f"{face[1]+1}/{face[1]+1} " f"{face[2]+1}/{face[2]+1}\n") elif include_normals and self.normals: # v//vn format f.write(f"f {face[0]+1}//{face[0]+1} " f"{face[1]+1}//{face[1]+1} " f"{face[2]+1}//{face[2]+1}\n") else: # v only f.write(f"f {face[0]+1} {face[1]+1} {face[2]+1}\n") ydd to obj

def batch_convert(self, input_files: List[str], output_dir: str) -> None: """Convert multiple YDD files to OBJ""" import os for input_file in input_files: try: self.load_ydd_file(input_file) base_name = os.path.basename(input_file).replace('.ydd', '').replace('.json', '') output_file = os.path.join(output_dir, f"{base_name}.obj") self.convert_to_obj(output_file) print(f"✓ Converted: {input_file} -> {output_file}") except Exception as e: print(f"✗ Failed: {input_file} - {str(e)}") def create_sample_ydd(): """Create a sample YDD file for testing""" sample_data = { "vertices": [ [0.0, 0.0, 0.0], [1.0, 0.0, 0.0], [1.0, 1.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 1.0, 1.0], [0.0, 1.0, 1.0] ], "faces": [ [0, 1, 2], [0, 2, 3], # bottom face [4, 7, 6], [4, 6, 5], # top face [0, 4, 5], [0, 5, 1], # front face [1, 5, 6], [1, 6, 2], # right face [2, 6, 7], [2, 7, 3], # back face [3, 7, 4], [3, 4, 0] # left face ], "normals": [ [0, 0, -1], [0, 0, 1], [0, -1, 0], [1, 0, 0], [0, 1, 0], [-1, 0, 0] ] } # Or batch convert # converter

Deja un comentario