python TexturePacker 自动将散图打包成图集

 

背景:在网上当下了好多的图片但是是整图,我用了一个工具讲整图给切分了小图,这个工具就是shoebox,切分成小图之后,需要将这些小图进行打包,这里用到了TextruePacker的命令行打包,至于为什么需要命令行,是因为太多散图,手动打包太累了,所以选择了用python帮我自动打包了。

import os;
from os import path;
import shutil;

def doTask(dirPath):
    absDir = path.abspath(dirPath);
    
    print("absDir");
    print("*" * 20);
    print("     start search    ");
    print("*" * 20);

    teststring = "helloworld";

    testIndex = teststring.index("l");
    print("testIndex is %d"%testIndex)

    for root,dirs,files in os.walk(absDir):
        santuArr = [];
        absRoot = path.abspath(root);
        # print("dirs is ",dirs)
        for fileItem in files:
            
            fileItemObj = {
                "file": fileItem,
                "ext" : ".png",
                "name": "test"
            };
            extname = os.path.splitext(fileItem)[1];
            filename = os.path.splitext(fileItem)[0];
            if(extname == '.py'):
                continue;
            fileItemObj["ext"] = extname;
            fileItemObj["name"] = filename;
            # print(type(filename));
            sanTuIndex = filename.find("_0");
            # print("sanTuIndex is ",sanTuIndex);
            # print("santuArr is ",santuArr);
            if sanTuIndex >= 0:
                # print("filename is ",filename);
                santuArr.append(fileItemObj);
                continue;
            else:
                # print("santuArr:",santuArr);
                if not os.path.exists(absRoot + "/" + filename):
                    os.mkdir(absRoot + "/" + filename);
                
        print("sanTuArr is ",santuArr);
        for santuItme in santuArr:
            last_Index = santuItme["name"].rindex("_");
            # print("last_Index is ",last_Index);
            targetName = santuItme["name"][:last_Index];
            
            sourceFilePath = absRoot + "/" + santuItme["name"] + santuItme["ext"];
            targetFilePath = absRoot + "/" + targetName;
            shutil.copy(sourceFilePath,targetFilePath);
                
def createSheets(dirPath):
    print("*" * 30);
    print("    start create sheets    ");
    print("*" * 30);
    
    absDir = path.abspath(dirPath);
    for root,dirs,files in os.walk(absDir):
        # print("dirs is ",dirs);
        for dir in dirs:
            for rootDir,dirss,filess in os.walk(absDir + "/" + dir):
                print("rootDir is ",rootDir);
                if len(files) > 0:
                    basename = path.basename(rootDir);
                    os.system("TexturePacker --smart-update --format cocos2d --texture-format pvr2ccz --data " + 
                    rootDir + "/" + basename + "{n}.plist " + 
                    "--sheet " + rootDir + "/" + basename + "{n}.png --enable-rotation --scale 1.0 --opt RGBA8888 " + basename + "/*.png");

if __name__ == '__main__':
    # doTask("./");
    createSheets("./");
    

主要的代码就是:

os.system("TexturePacker --smart-update --format cocos2d --texture-format pvr2ccz --data " + 
                    rootDir + "/" + basename + "{n}.plist " + 
                    "--sheet " + rootDir + "/" + basename + "{n}.png --enable-rotation --scale 1.0 --opt RGBA8888 " + basename + "/*.png");

最后如果有小伙伴需要在cocos creator 生成资源映射文件,在指定节点上添加很多动画的需求的话欢迎下载我写的creator插件:

插件地址

你可能感兴趣的:(cocos,creator)