美文网首页
CocosCreator3.x中,借助python获知精灵图被哪

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

作者: 全新的饭 | 来源:发表于2023-08-16 17:10 被阅读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)

相关文章

  • 行中获知

    我晨起临帖,妻爨炊也。 随聊及两个孩子的手机问题。我告诉妻子,手机是时代的文明产物,过于禁止适得其反,应适当引导使...

  • Python笔记——记录程序运行时间

    记录一个python中的小模块 在python程序中如何记录程序运行的时间呢? 可以借助python中的time模...

  • 鬼才把python当成PS用,抠图合成图片样样精通!

    本文约1500字,阅读需要5分钟 关键词:Python P图 OpenCV 本文讲述了借助Python语言和Ope...

  • 小精灵去哪里!大屏互动带你穿梭精灵世界

    生活在大人的世界被烦恼包围,那为什么不穿梭精灵世界感受小精灵们的纯真可爱!还在想六一去哪玩?越泰科技精灵世界带你度...

  • 08-精灵图的利弊

    CSS Sprites 、精灵图、 雪碧图 都是一个概念 * 我对精灵图的简单看法 关于精灵图我简单的介绍一下:...

  • 20CSS中的高级技巧

    技术交流QQ群:1027579432,欢迎你的加入! 1.精灵图 1.1 为什么需要精灵图? 一个网页中往往会应用...

  • 9、精灵图(重点)及字体图标

    1. 精灵图(重点) 1.1 为什么需要精灵图 一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多时...

  • 网络编程(四)之 CSS(3)

    一、 精灵图 1.1 为什么需要精灵图 一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多时,服务器...

  • 三十二、精灵图&字体图标

    一、精灵图 1.1 为什么需要精灵图 一个网页中往往会应用很多小的背景图像作为修饰,当网页中的图像过多时,服务器就...

  • Python Day39

    python大大的图 作业图: import math——math模块 math模块是在python标准库中的,不...

网友评论

      本文标题:CocosCreator3.x中,借助python获知精灵图被哪

      本文链接:https://www.haomeiwen.com/subject/dozkmdtx.html