#!/usr/bin/python import re, os, string, sys, math pstring = 'cat '+sys.argv[1] + ' | grep \"v \" > tmp' pstring1 = 'cat tmp | tr -d \'v\' > tmp1' cstring = 'rm tmp; rm tmp1' os.system(pstring) os.system(pstring1) mx = my = mz = 0 count = 0.0; fp=open('tmp1'); for line in fp.readlines(): count = count + 1; lspl = line.split() x = float(lspl[0]) y = float(lspl[1]) z = float(lspl[2]) mx = mx + x; my = my + y; mz = mz + z; mx = mx / count; my = my / count; mz = mz / count; fp.close() print 'Mean : ',mx,my,mz maxd = 0; fp=open('tmp1'); for line in fp.readlines(): lspl = line.split() x = float(lspl[0]) y = float(lspl[1]) z = float(lspl[2]) tx = mx - x; ty = my - y; tz = mz - z; d= (tx*tx + ty*ty + tz*tz); if d > maxd: maxd = d; fp.close() maxd = math.sqrt(maxd) print 'Max d : ', maxd fp=open(sys.argv[1]); fpw=open('output.obj','w'); for line in fp.readlines(): if string.find(line,"v ") != -1: lspl = line.split() x = float(lspl[1]) y = float(lspl[2]) z = float(lspl[3]) nx = ( x - mx ) / maxd; ny = ( y - my ) / maxd; nz = ( z - mz ) / maxd; line = 'v ' + str(nx) + ' ' + str(ny) + ' ' + str(nz) + '\n' fpw.write(line) fpw.close() os.system(cstring)