def main(): parser = argparse.ArgumentParser() parser.add_argument('--set-min-y', dest='desired_min_y', type=float, default=None, help='Move sketch up/down to match the passed y value') parser.add_argument('files', nargs='+') args = parser.parse_args() for filename in args.files: tilt = Tilt(filename) sketch = tilt.sketch print '=== %s ===' % filename if args.desired_min_y is not None: min_y = min(cp.position[1] for stroke in sketch.strokes for cp in stroke.controlpoints) delta = args.desired_min_y - min_y for stroke in sketch.strokes: for cp in stroke.controlpoints: cp.position[1] += delta print filename print 'Moved by %.3f' % delta tilt.write_sketch()
def main(): import argparse parser = argparse.ArgumentParser( description= "Create a normalized version of the sketch (with 'Normalized' appended to\ the file name) which is scaled, rotated, and translated so that resetting the\ transform will bring you back to the initial size, orientation, and position.\ But the environment size, orientation, and position will also be reset.") parser.add_argument('files', type=str, nargs='+', help="Sketches to normalize") args = parser.parse_args() for filename in args.files: name, ext = os.path.splitext(filename) filename_normalized = name + 'Normalized' + ext shutil.copy(filename, filename_normalized) tilt_file = Tilt(filename_normalized) normalize_tilt_file(tilt_file) tilt_file.write_sketch() print 'WARNING: Environment position has changed in ' + filename + '.'
def main(): parser = argparse.ArgumentParser() parser.add_argument( "--set-min-y", dest="desired_min_y", type=float, default=None, help="Move sketch up/down to match the passed y value", ) parser.add_argument("files", nargs="+") args = parser.parse_args() for filename in args.files: tilt = Tilt(filename) sketch = tilt.sketch print("=== %s ===" % filename) if args.desired_min_y is not None: min_y = min(cp.position[1] for stroke in sketch.strokes for cp in stroke.controlpoints) delta = args.desired_min_y - min_y for stroke in sketch.strokes: for cp in stroke.controlpoints: cp.position[1] += delta print(filename) print("Moved by %.3f" % delta) tilt.write_sketch()
def main(): import argparse parser = argparse.ArgumentParser( description="View information about a .tilt") parser.add_argument('--strokes', action='store_true', help="Dump the sketch strokes") parser.add_argument('--metadata', action='store_true', help="Dump the metadata") parser.add_argument('--json', action='store_true', help="Dump JSON for unity parsing") parser.add_argument('files', type=str, nargs='+', help="Files to examine") args = parser.parse_args() if not (args.strokes or args.metadata or args.json): print "You should pass at least one of --strokes , --metadata, or --json" for filename in args.files: t = Tilt(filename) if args.strokes: dump_sketch(t.sketch) if args.metadata: pprint.pprint(t.metadata) if args.json: print(dump_json(t.sketch))
def test_as_directory(self): # Test Tilt.as_directory with copy_of_tilt(as_filename=True) as tilt_filename: with Tilt.as_directory(tilt_filename): self.assertTrue(os.path.isdir(tilt_filename)) self.assertTrue( os.path.exists(os.path.join(tilt_filename, 'metadata.json')))
def process_tilt(filename, args): msg("Load tilt") tilt = Tilt(filename) msg("Load strokes") # TODO: this seems to do nothing; is there a function that's supposed to be called here? tilt.sketch.strokes # pylint: disable=pointless-statement msg("") if args.debug: msg("Clone strokes") before_strokes = [s.clone() for s in tilt.sketch.strokes] # Do this before color quantization, because it removes strokes (and their colors) if args.convert_brushes: convert_brushes(tilt, BRUSH_REPLACEMENTS) if args.remove_stray_strokes is not None: remove_stray_strokes(tilt, args.remove_stray_strokes, BrushLookup.get().get_unique_guid("Wire")) if args.pos_error_tolerance > 0: reduce_control_points(tilt, args.pos_error_tolerance) if args.simplify_colors is not None: simplify_colors(tilt, num_colors=args.simplify_colors, preserve_colors=args.preserve_colors) if args.debug: final_strokes = [] # interleave them so it renders semi-nicely... for before, after in itertools.zip_longest(before_strokes, tilt.sketch.strokes): if before is not None: for cp in before.controlpoints: cp.position[1] += 10 final_strokes.append(before) if after is not None: final_strokes.append(after) tilt.sketch.strokes[:] = final_strokes tilt.write_sketch() msgln("Wrote %s" % os.path.basename(tilt.filename))
def main(args=None): import argparse parser = argparse.ArgumentParser( description="View and modify .tilt file metadata") parser.add_argument('--list', action='store_true', help='Print metadata') parser.add_argument('--author', action='append', type=as_unicode, help='Set author (may be passed multiple times)', default=None) parser.add_argument('files', nargs='+', type=str, help="File to examine") args = parser.parse_args(args) for filename in args.files: print('-- %s -- ' % filename) sketch = Tilt(filename) with sketch.mutable_metadata() as meta: if args.author is not None: meta['Authors'] = args.author if args.list: pprint.pprint(meta)
def main(args=None): parser = argparse.ArgumentParser( description="View and modify .tilt file metadata") parser.add_argument("--list", action="store_true", help="Print metadata") parser.add_argument( "--author", action="append", type=as_unicode, help="Set author (may be passed multiple times)", default=None, ) parser.add_argument("files", nargs="+", type=str, help="File to examine") args = parser.parse_args(args) for filename in args.files: print("-- %s -- " % filename) sketch = Tilt(filename) with sketch.mutable_metadata() as meta: if args.author is not None: meta["Authors"] = args.author if args.list: pprint.pprint(meta)
def test_can_mutate_metadata(self): import uuid random_guid = str(uuid.uuid4()) with copy_of_tilt() as tilt: with tilt.mutable_metadata() as dct: # Check that they are different references dct['EnvironmentPreset'] = random_guid self.assertNotEqual(tilt.metadata['EnvironmentPreset'], dct['EnvironmentPreset']) # Check that it's copied back on exit from mutable_metadata self.assertEqual(tilt.metadata['EnvironmentPreset'], random_guid) # Check that the mutations persist tilt2 = Tilt(tilt.filename) self.assertEqual(tilt2.metadata['EnvironmentPreset'], random_guid)
def copy_of_tilt(tilt_file='data/sketch1.tilt', as_filename=False): """Returns a mutate-able copy of tilt_file, and removes it when done.""" base = os.path.abspath(os.path.dirname(__file__)) full_filename = os.path.join(base, tilt_file) tmp_filename = os.path.splitext(full_filename)[0] + '_tmp.tilt' shutil.copy(src=full_filename, dst=tmp_filename) try: if as_filename: yield tmp_filename else: yield Tilt(tmp_filename) finally: if os.path.exists(tmp_filename): os.unlink(tmp_filename)
def test_stroke_extension(self): # Test that control point extensions can be added and removed with copy_of_tilt() as tilt: stroke = tilt.sketch.strokes[0] # This sketch was made before stroke scale was a thing self.assertEqual(stroke.flags, 0) self.assertRaises(AttributeError, (lambda: stroke.scale)) # Test adding some extension data stroke.scale = 1.25 self.assertEqual(stroke.scale, 1.25) # Test removing extension data del stroke.flags self.assertRaises(AttributeError(lambda: stroke.flags)) # Test that the changes survive a save+load tilt.write_sketch() stroke2 = Tilt(tilt.filename).sketch.strokes[0] self.assertEqual(stroke2.scale, 1.25) self.assertRaises(AttributeError(lambda: stroke2.flags))
def main(args): import argparse parser = argparse.ArgumentParser( description= "Converts .tilt files to a Collada .dae containing spline data.") parser.add_argument('files', type=str, nargs='*', help="Files to convert to dae") args = parser.parse_args(args) for filename in args.files: t = Tilt(filename) outf_name = os.path.splitext(os.path.basename(filename))[0] + '.dae' dae = ColladaFile() for stroke in t.sketch.strokes: dae.add_stroke(stroke) dae.write(outf_name) print 'Wrote', outf_name