Ejemplo n.º 1
0
    def test_save(self):
        root = cfg.ConfigNode("root", "root")
        sub1 = cfg.ConfigNode("sub1", "val1")
        sub2a = cfg.ConfigNode("sub2a", "val2a")
        sub2b = cfg.ConfigNode("sub2b", "val2b")
        sub3 = cfg.ConfigNode("sub3", "val3")

        root.attribs["sub1"] = sub1
        sub1.attribs["sub2a"] = sub2a
        sub1.attribs["sub2b"] = sub2b
        sub2b.attribs["sub3"] = sub3

        conf = cfg.ConfigFile()
        conf.root_node = root
        conf.save("cfg.tmp")

        conf.load("cfg.tmp")

        assert conf.root_node.get("sub1").value == "val1"
        assert conf.root_node.get("sub1.sub2a").value == "val2a"
        assert conf.root_node.get("sub1.sub2b").value == "val2b"
        assert conf.root_node.get("sub1.sub2b.sub3").value == "val3"

        try:
            conf.root_node.get("sub1.sub2a.sub3")
            assert False
        except:
            assert True
Ejemplo n.º 2
0
    def test_set(self):
        root = cfg.ConfigNode("root", "root")
        sub1 = cfg.ConfigNode("sub1", "val1")
        sub2a = cfg.ConfigNode("sub2a", "val2a")
        sub2b = cfg.ConfigNode("sub2b", "val2b")
        sub3 = cfg.ConfigNode("sub3", "val3")

        root.attribs["sub1"] = sub1
        sub1.attribs["sub2a"] = sub2a
        sub1.attribs["sub2b"] = sub2b
        sub2b.attribs["sub3"] = sub3

        root.set("sub1.sub2a", "val2Set")
        assert root.get("sub1.sub2a").value == "val2Set"

        root.set("sub1.subNew", 18)
        assert int(root.get("sub1.subNew").value) == 18
Ejemplo n.º 3
0
def generate_sprite(configname,
                    spritefilename,
                    blenderfilename,
                    scale,
                    count,
                    scene=None,
                    center=None):
    node = cfg.ConfigNode(configname)
    node.value = "SpriteFilm"

    tmpfilename = "assets/tmp/" + path.basename(spritefilename)
    tmpfilename = tmpfilename.replace(".", "_%04d.")

    if should_update(tmpfilename % 1, [blenderfilename]):
        for i in range(1, count + 1):
            print "generating", tmpfilename % i
            outname = tmpfilename[0:tmpfilename.rfind("_")]
            if scene is None:
                os.system("blender -b " + blenderfilename + " -F PNG -o " +
                          outname + "_ -f " + str(i))
            else:
                os.system("blender -b " + blenderfilename + " -S " + scene +
                          " -F PNG -o " + outname + "_ -f " + str(i))

    if should_update(spritefilename,
                     [tmpfilename % i for i in range(1, count + 1)]):
        print "generating sprite", spritefilename

        filenames = [tmpfilename % i for i in range(1, count + 1)]
        images = [Image.open(filename) for filename in filenames]

        images = [
            im.resize((int(im.size[0] * scale), int(im.size[1] * scale)),
                      Image.ANTIALIAS) for im in images
        ]

        box = max_bounding_box(images)

        if center is not None:
            node.set("center_x", int(round(center[0] * scale - box[0])))
            node.set("center_y", int(round(center[1] * scale - box[1])))
        node.set("frame_width", int(box[2] - box[0]))
        node.set("frame_height", int(box[3] - box[1]))
        node.set("div_x", min(len(images), 10))
        node.set("div_y", (len(images) / 10) + 1)

        images = [im.crop(box) for im in images]
        for im in images:
            im.load()  # for lazy crop

        out_image = pack_images(images)
        out_image.save(spritefilename)

        return node
Ejemplo n.º 4
0
    def test_get(self):
        root = cfg.ConfigNode("root", "root")
        sub1 = cfg.ConfigNode("sub1", "val1")
        sub2a = cfg.ConfigNode("sub2a", "val2a")
        sub2b = cfg.ConfigNode("sub2b", "val2b")
        sub3 = cfg.ConfigNode("sub3", "val3")

        root.attribs["sub1"] = sub1
        sub1.attribs["sub2a"] = sub2a
        sub1.attribs["sub2b"] = sub2b
        sub2b.attribs["sub3"] = sub3

        assert root.get("sub1").value == "val1"
        assert root.get("sub1.sub2a").value == "val2a"
        assert root.get("sub1.sub2b").value == "val2b"
        assert root.get("sub1.sub2b.sub3").value == "val3"

        try:
            root.get("sub1.sub2a.sub3")
            assert False
        except:
            assert True
Ejemplo n.º 5
0
def generate_image(configname,
                   imagefilename,
                   blenderfilename,
                   scale,
                   scene=None,
                   offset=None):
    node = cfg.ConfigNode(configname)
    node.value = "Surface"
    #    node.set( "file", imagefilename )

    tmpfilename = "assets/tmp/" + path.basename(imagefilename)
    tmpfilename = tmpfilename.replace(".", "_0001.")

    if should_update(tmpfilename, [blenderfilename]):
        print "generating", tmpfilename
        outfilename = tmpfilename[0:tmpfilename.rfind("_")]
        if scene is None:
            os.system("blender -b " + blenderfilename + " -F PNG -o " +
                      outfilename + "_ -f 1")
        else:
            os.system("blender -b " + blenderfilename + " -S " + scene +
                      " -F PNG -o " + outfilename + "_ -f 1")

    if should_update(imagefilename, [tmpfilename]):
        print "generating image", imagefilename
        im = Image.open(tmpfilename)

        im = im.resize((int(im.size[0] * scale), int(im.size[1] * scale)),
                       Image.ANTIALIAS)

        box = get_bounding_box(im)

        if offset is not None and box is not None:
            node.set("offset_x", int(round(offset[0] * scale - box[0])))
            node.set("offset_y", int(round(offset[1] * scale - box[1])))

        im = im.crop(box)
        im.load()  # for lazy crop

        im.save(imagefilename)

        return node