def update_s3o(path, commit, optimize): txt_exists = os.path.exists(path[:-3] + 'txt') if (commit or not optimize) and not txt_exists: print "No txt file, skipping." return with open(path, 'rb+') as input_s3o: data = input_s3o.read() model = S3O(data) pieces_changed = 0 if txt_exists: with open(path[:-3] + 'txt', 'rb') as input_txt: pieces_changed = load_txt(input_txt, model.root_piece, commit) if optimize: recursively_optimize_pieces(model.root_piece) new_data = model.serialize() write = False changed = False if pieces_changed > 0: print " %s piece changes" % (pieces_changed, ) write |= commit changed = True if optimize and len(new_data) < len(data): print " %s bytes optimised" % (len(data) - len(new_data), ) write = True changed = True if write: input_s3o.seek(0) input_s3o.truncate() input_s3o.write(new_data) return changed
def get_s3o_sizes(path): with open(path, 'rb') as input_s3o: data = input_s3o.read() model = S3O(data) sizes = recursive_get_sizes(model.root_piece) x_diff, y_diff, z_diff = sizes["max"][0] - sizes["min"][0], sizes["max"][1] - sizes["min"][1], sizes["max"][2] - sizes["min"][2] name = os.path.splitext(os.path.basename(path))[0] print "%s,%s,%s,%s" % (name, x_diff, y_diff, z_diff)
def dump_s3o(path): print "Dumping %s" % (path, ) with open(path, 'rb') as input_s3o: data = input_s3o.read() model = S3O(data) with open(path[:-3] + 'txt', 'wb') as output_txt: save_txt(output_txt, model.root_piece)
def wreckunit(self): self.destroy(0, 0.05 + random.random() / 10, 0.5, random.random() * 100) self.writebos() self.wreckeds3o.texture_paths = (self.tex1.get(), self.tex2.get()) optimized_data = self.wreckeds3o.serialize() output_file = open(self.outputdir + '/' + self.unitname + '_dead.s3o', 'wb') output_file.write(optimized_data) output_file.close() for i in range(len(self.unitdef)): if 'object' in self.unitdef[i] and self.unitname.lower( ) + '_dead' in self.unitdef[i].lower(): if '.s3o' not in self.unitdef[i].lower(): self.unitdef[i] = self.unitdef[i].lower().replace( '_dead', '_dead.s3o') elif 'object' in self.unitdef[i] and 'x' in self.unitdef[i].lower( ): line = self.unitdef[i].lower() parts = line.split('\"') heapname = parts[1] print 'heapname', heapname xindex = heapname.index('x') try: int(heapname[xindex - 1]) int(heapname[xindex + 1]) if 'cor' in heapname: if 'Arm' in self.s3o.texture_paths[0]: heapname = heapname.replace('cor', 'arm') else: if 'Arm' in self.s3o.texture_paths[0]: heapname = 'arm' + heapname + '.s3o' else: heapname = 'cor' + heapname + '.s3o' print 'newheapname', heapname self.unitdef[i] = '\"'.join([parts[0], heapname, parts[2]]) except: print 'this is not a heap!' pass luaf = open(self.outputdir + '/' + self.unitname + '.lua', 'w') luaf.write(''.join(self.unitdef)) luaf.close() print 'Successfully written, validating S30' valid = S3O( open(self.outputdir + '/' + self.unitname + '_dead.s3o', 'rb').read()) #valid.S3OtoOBJ(self.outputdir+'/'+self.unitname+'_dead.obj') print 'validation OK!' return
def loadunit(self, default=''): self.deleteseverityframes() self.makeseverityframes() self.piecelist = [] self.killscript = [] self.keeplist = [] self.piecetree = [] self.severitylevels = [25, 50, 99, -1] self.bos = 0 self.s3o = 0 if default == '': self.unitdefpath = tkFileDialog.askopenfilename( initialdir=self.initialdir, filetypes=[('Spring Model def (Lua)', '*.lua'), ('Any file', '*')], multiple=False) else: self.unitdefpath = default print 'loading', self.unitdefpath if '.lua' in self.unitdefpath: self.modpath = self.unitdefpath.partition('units')[0] self.unitname = self.unitdefpath.rpartition('/')[2].partition( '.')[0] self.labelvar.set(self.unitname.upper()) self.bospath = self.modpath + 'scripts/Units/' + self.unitname + '.bos' self.s3opath = self.modpath + 'objects3d/Units/' + self.unitname + '.s3o' self.outputdir = self.modpath + self.outputbasedir try: self.s3o = S3O(open(self.s3opath, 'rb').read()) try: self.bos = open(self.bospath, 'r').readlines() except: print 'Could not open .bos file, using blank instead' self.bos = [] self.unitdef = open(self.unitdefpath, 'r').readlines() if 'Arm' in self.s3o.texture_paths[0]: self.tex1.set("Arm_wreck_color.dds") self.tex2.set("Arm_wreck_other.dds") else: self.tex1.set("Core_color_wreck.dds") self.tex2.set("Core_other_wreck.dds") except: raise print 'loaded', self.unitname, 'successfully' self.updatetree(self.s3o, self.piecelist) print self.piecelist self.killscript = self.loadbos(self.bos) print self.killscript self.createui(self.killscript) self.keeplist = copy.deepcopy(self.piecelist) return
def optimizeS3O(filename): datafile = open(filename, 'rb') data = datafile.read() model = S3O(data) pre_vertex_count = countvertices(model.root_piece) recursively_optimize_pieces(model.root_piece) optimized_data = model.serialize() datafile.close() print 'Number of vertices before optimization:', pre_vertex_count, ' after optimization:', countvertices( model.root_piece) output_file = open(filename, 'wb') output_file.write(optimized_data) output_file.close() print "Succesfully optimized", filename
def rescale(path, scale): with open(path, 'rb+') as input_s3o: data = input_s3o.read() model = S3O(data) recursively_scale_pieces(model.root_piece, scale) model.midpoint = scale_vertex(model.midpoint, scale) model.collision_radius = model.collision_radius * scale model.height = model.height * scale new_data = model.serialize() input_s3o.seek(0) input_s3o.truncate() input_s3o.write(new_data)
def wreckunits3o(self): self.destroy(0, 0.05 + random.random() / 10, 0.5, random.random() * 100) self.wreckeds3o.texture_paths = (self.tex1.get(), self.tex2.get()) optimized_data = self.wreckeds3o.serialize() output_file = open( self.modpath + 'objects3d/' + self.unitname + '_dead.s3o', 'wb') output_file.write(optimized_data) output_file.close() print 'Successfully written, validating S30' valid = S3O( open(self.modpath + 'objects3d/' + self.unitname + '_dead.s3o', 'rb').read()) #valid.S3OtoOBJ(self.outputdir+'/'+self.unitname+'_dead.obj') print 'validation OK!' return
def OBJtoS3O(objfile, transform, outputfilename, a, b, c, d): if '.obj' in objfile.lower(): data = open(objfile).readlines() if transform == 1: for line in range(len(data)): if data[line][0:2] == 'vt': s = data[line].split(' ') data[line] = ' '.join([ s[0], str(float(s[1]) * a + b), str(float(s[2]) * c + d) ]) isobj = True model = S3O(data, isobj) recursively_optimize_pieces(model.root_piece) optimized_data = model.serialize() output_file = open(outputfilename, 'wb') output_file.write(optimized_data) output_file.close() print "Succesfully converted", objfile, 'to', outputfilename
parser.error("insufficient arguments") dry = options.is_dry silence_output = options.silence_output if len(args) == 1: filenames = glob(args[0]) else: filenames = args delta_total = 0 for filename in filenames: with open(filename, 'rb+') as input_file: data = input_file.read() model = S3O(data) recursively_optimize_pieces(model.root_piece) optimized_data = model.serialize() delta_size = len(optimized_data) - len(data) delta_total += delta_size if delta_size < 0: if not silence_output: print("modified %s: " "size change: %d bytes" % (filename, delta_size)) if not dry: input_file.seek(0) input_file.truncate() input_file.write(optimized_data)
def S3OtoOBJ(filename, outputfilename): if '.s3o' in filename.lower(): data = open(filename, 'rb').read() model = S3O(data) model.S3OtoOBJ(outputfilename) print "Succesfully converted", filename, 'to', outputfilename