#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2024/05/05 06:42:54 +0900> """ blender line obj to csv blenderからlineデータを Wavefront obj形式でエクスポートして 座標値をcsvで列挙して出力する。 Usage: python lineobj2csv.py -i INPUT.obj > OUTPUT.csv Windows10 x64 22H2 + Python 3.10.10 64bit scipy 1.13.0 + numpy 1.26.4 """ import argparse import sys # import re # import numpy as np # from scipy import interpolate def load_obj(infile): vtx = [] lineindexes = [] with open(infile, "r") as file: for line in file: line = line.rstrip() s = line.split(" ") if s[0] == "#": continue elif s[0] == "mtllib": continue elif s[0] == "o": continue print(line) elif s[0] == "v": vtx.append([float(s[1]), float(s[2]), float(s[3])]) elif s[0] == "l": lineindexes.append((int(s[1]), int(s[2]))) return vtx, lineindexes def conv_csv(vtx, lineindexes, xyz, revz): fg = False for i in range(len(lineindexes) - 1): i00, i01 = lineindexes[i] i10, i11 = lineindexes[i + 1] if i01 != i10: print("Error:") print(f"l {i00} {i01}") print(f"l {i10} {i11}") fg = True if fg: print("Stop") return ret = [] for idx2 in lineindexes: x, y, z = vtx[idx2[0] - 1] if not revz: z *= -1 if xyz: ret.append([x, y, z]) else: ret.append([x, z]) return ret def dump_pos(data, xyz): if xyz: for v in data: print(f"{v[0]},{v[1]},{v[2]}") else: for v in data: print(f"{v[0]},{v[1]}") def main(): parser = argparse.ArgumentParser() parser.add_argument("-i", "--input", required=True, help="Input obj file") parser.add_argument("--xyz", action="store_true", help="Output x,y,z. default x,z") parser.add_argument("--revz", action="store_true", help="Reverse z") args = parser.parse_args() if args.input is None: sys.exit() vtx, lineindexes = load_obj(args.input) data = conv_csv(vtx, lineindexes, args.xyz, args.revz) dump_pos(data, args.xyz) if __name__ == "__main__": main()