CocosCreator3.x中,借助python获知精灵图被哪

2023-08-16  本文已影响0人  全新的饭

目标

生成对应关系:图片路径 - 引用了该图片的预制体路径列表

参考

Python读写json和csv

使用vscode开发python
超详细的Python安装和环境搭建教程
神器 VS Code,超详细Python配置使用指南

代码

getSpriteAndPrefabRelations.py

# CocosCreator3.x中,借助python获得精灵图对应的预制体(被哪些预制体引用)

import os
import sys

sys.path.append('ReadAndWriteTable.py的路径')
from ReadAndWriteTable import readCsv, writeCsv, readJson, writeJson, isInt


def getPathList(path, suffix):
    '''获取path下所有后缀为suffix的文件的路径'''
    pathList = []
    for mainDir, subDir, fileNameList in os.walk(path):
        for fileName in fileNameList:
            currentPath = os.path.join(mainDir, fileName)
            if currentPath.endswith(suffix):
                pathList.append(currentPath)
    return pathList


def createDictIdAndPngPath(pngFilesPath):
    '''生成字典:uuid-图片路径'''
    retDict = {}
    for path in pngFilesPath:
        metaFilePath = path + '.meta'
        # 打开对应的meta文件,若其"userData"下的"type": "sprite-frame",则获取其id:"redirect"
        metaFileJson = readJson(metaFilePath)
        if metaFileJson['userData']['type'] == 'sprite-frame':
            retDict[metaFileJson['userData']['redirect']] = path
    return retDict


def createDictPrefabPathAndSpriteFrameIds(prefabFilesPath):
    '''生成字典:预制体路径-spriteFrameId列表'''
    retDict = {}
    for path in prefabFilesPath:
        prefabFileJson = readJson(path)
        ids = []
        for data in prefabFileJson:
            if data['__type__'] == 'cc.Sprite' and '_spriteFrame' in data and data[
                    '_spriteFrame'] != None:
                curId = data['_spriteFrame']['__uuid__']
                ids.append(curId)
        if len(ids) > 0:
            ids = list(set(ids))
            retDict[path] = ids
    return retDict


def prefabPathAndSpriteIdsToSpriteIdAndPrefabPaths(prefabPathAndSpriteIds):
    '''预制体路径-图片Id列表 转 图片Id-预制体路径列表'''
    retDict = {}
    for prefabPath in prefabPathAndSpriteIds:
        spriteIds = prefabPathAndSpriteIds[prefabPath]
        for id in spriteIds:
            if id not in retDict:
                retDict[id] = []
            retDict[id].append(prefabPath)
    return retDict


def spriteIdToPath(id, refDict):
    '''图片id-图片路径'''
    path = ''
    if id in refDict:
        path = refDict[id]
    return path


if __name__ == "__main__":
    # 1.获得指定目录下所有被设置为spriteFrame图片(.png)及其uuid
    # 获取所有png文件路径
    pngFilesPath = getPathList(r'工程路径\assets',
                               '.png')
    # 生成字典:uuid-图片路径
    idAndPngPathDict = createDictIdAndPngPath(pngFilesPath)

    # 2. 获得指定目录下所有预制体路径 及其 spriteFrame 的uuid列表
    prefabFilesPath = getPathList(
        r'工程路径\assets', '.prefab')
    prefabPathAndSpriteFrameIdsDict = createDictPrefabPathAndSpriteFrameIds(
        prefabFilesPath)
    spriteIdAndPrefabPathsDict = prefabPathAndSpriteIdsToSpriteIdAndPrefabPaths(
        prefabPathAndSpriteFrameIdsDict)
    spritePathAndPrefabPathsDict = {}
    for s in spriteIdAndPrefabPathsDict:
        spritePathAndPrefabPathsDict[spriteIdToPath(
            s, idAndPngPathDict)] = spriteIdAndPrefabPathsDict[s]

    outputPath = r'输出路径\图片和预制体的对应关系.txt'
    outputJson = writeJson(outputPath, spritePathAndPrefabPathsDict)
上一篇 下一篇

猜你喜欢

热点阅读