def validate_project(project): acceptable_formats = [ "go", "collections", "gui", "atlas", "material", "tilemap", "spinescene", "texture_profile", "render", "font", "animationset", "cubemap", "collectionfactory", "factory", "collectionproxy", "collisionobject", "label", "inputbinding", "model", "particlefx", "sound", "spinemodel", "sprite", "tilesource" ] acceptable_formats.extend(["defold"]) print("Starting Validation of project") for path_root, folders, files in os.walk(project): for f in files: if "{0}build{0}".format( os.sep) not in path_root and os.path.splitext( f)[-1][1:] in acceptable_formats: print("Validating", f) defold_file = os.path.join(path_root, f) try: tree = deftree.parse(defold_file) root = tree.get_root() except deftree.ParseError: print(" Couldn't parse: ", f) continue if not deftree.validate(deftree.to_string(root), defold_file): print(" Error in: {}".format(defold_file)) print("Validation of project ended")
def image_in_file(atlas): # Returns all images in an atlas tree = deftree.parse(atlas) root = tree.get_root() images = [] for x in root.iter_attributes("image"): images.append(x) return images
def test_writing_with_changed_attribute(self): path = os.path.join(self.root_path, "simple.defold") output_path = os.path.join(self.root_path, "_copy", "edit.defold") tree = deftree.parse(path) root = tree.get_root() root.set_attribute("extrude_borders", 99) tree.write(output_path) self.assertTrue(deftree.validate(deftree.to_string(root), output_path))
def test_rewrite_file(self): path = os.path.join(self.root_path, "simple.defold") output_path = os.path.join(self.root_path, "_copy", "rewrite.defold") shutil.copy(path, output_path) tree = deftree.parse(output_path) tree.write(output_path) self.assertTrue(is_valid(output_path), "Failed rewriting file")
def image_in_cubemap(cubemap): # Returns all images in a cubemap tree = deftree.parse(cubemap) root = tree.get_root() images = [] for child in root.attributes(): if child.value.endswith(".png"): images.append(child) return images
def atlas(atlas_path, paths): tree = deftree.parse(atlas_path) root = tree.get_root() for path in paths: images = deftree.Element("images") root.insert(-4, images) images.add_attribute("image", path.as_posix()) tree.write()
def test_writing_to_original_source(self): test_value = 55 path = os.path.join(self.root_path, "_copy", "simple.defold") shutil.copy(os.path.join(self.root_path, "simple.defold"), path) # Read in copied document and fps to 60, write it tree = deftree.parse(path) root = tree.get_root() a = root.get_element("animations") attribute = a.get_attribute("fps") attribute.value = test_value tree.write() # Read in the document again and verify that the value have been changed tree = deftree.parse(path) root = tree.get_root() a = root.get_element("animations") attribute = a.get_attribute("fps") self.assertTrue(attribute.value == test_value)
def clean_up_atlas(project_root, atlas): """Remove all missing image in entries in the atlas""" tree = deftree.parse(atlas) root = tree.get_root() print(" Cleaning", atlas.relative_to(project_root)) for missing in missing_images_in_atlas(project_root, root): image_element = missing.get_parent() image_element.get_parent().remove(image_element) print(" Removing", image_element.value) for animation in get_empty_animations(root): root.remove(animation) tree.write()
def resize_in_gui(filename): print("Parsing " + filename) tree = deftree.parse(filename) root = tree.get_root() for el in root.iter_elements("nodes"): scale_vec(el.get_element("size"), scale_factor); scale_vec(el.get_element("position"), scale_factor); scale_vec4(el.get_element("slice9"), scale_factor); tree.write(filename)
def resize_in_gui(filename): print("Parsing " + filename) tree = deftree.parse(filename) root = tree.get_root() for el in root.iter_elements("nodes"): attr_type = el.get_attribute("type") attr_font = el.get_attribute("font") if attr_type.value == "TYPE_TEXT" and attr_font != None and attr_font.value == font_name: scale_vec(el.get_element("scale"), scale_factor) scale_vec(el.get_element("size"), 1.0 / scale_factor) tree.write(filename)
def main(): filename = sys.argv[1] print("Auto setup layers for file", filename) tree = deftree.parse(filename) root = tree.get_root() layers = [] for texture in root.iter_elements("textures"): layers.append(texture.get_attribute("name").value) for fonts in root.iter_elements("fonts"): layers.append(fonts.get_attribute("name").value) to_remove_layers = [] for layer in root.iter_elements("layers"): to_remove_layers.append(layer) for layer in to_remove_layers: root.remove(layer) for layer in layers: new_layer = root.add_element("layers") new_layer.add_attribute("name", layer) for node in root.iter_elements("nodes"): texture = node.get_attribute("texture") font = node.get_attribute("font") if texture: layer = texture.value.split("/")[0] node.set_attribute("layer", layer) if font: layer = font.value node.set_attribute("layer", layer) tree.write()
def test_document_path(self): path = os.path.join(self.root_path, "embedded.defold") tree = deftree.parse(path) self.assertTrue(tree.get_document_path() == path)
def is_valid(path): tree = deftree.parse(path) root = tree.get_root() return deftree.validate(deftree.to_string(root), path)
def set_gui_script_path(gui_path, script_path): tree = deftree.parse(gui_path) root = tree.get_root() root.set_attribute("script", script_path.as_posix()) tree.write()