ページへ戻る

− Links

 印刷 

cocos2d-x​/ver2系テクスチャアトラスをpngファイルに分割する :: NJF Wiki

xpwiki:cocos2d-x/ver2系テクスチャアトラスをpngファイルに分割する

数年前、Animate CCでcocos2dx ver.2系のスプライトシートを書き出したら、その直後にファイルが壊れたことがありました。 そのままにして最近になってアップデートしようとすると、いくつか画像が足りなかったので、仕方なく書き出したスプライトシートからpngファイルを取り出したので、そのやり方です。

まず、スプライトシートのplistは以下のようなxmlファイルになっています。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
	<dict>
		<key>frames</key>
		<dict>
			<key>MLRSPanel0000</key>
			<dict>
				<key>frame</key>
				<string>{{4,4},{126,111}}</string>
				<key>offset</key>
				<string>{0,0}</string>
				<key>rotated</key>
				<false/>
				<key>sourceColorRect</key>
				<string>{{0,0},{126,111}}</string>
				<key>sourceSize</key>
				<string>{126,111}</string>
			</dict>

この中のdict二階層下の「<key>MLRSPanel0000</key>」とかの部分がフレーム名で、dict三階層目の「<string>{{4,4},{126,111~}}</string>」とかが、スプライトシート上のその画像の左上の座標と幅、高さをピクセルで表したものです。

まず、この部分を取り出すPythonのスクリプトを書きました。

from bs4 import BeautifulSoup
import sys
import os
import re

argvs = sys.argv


if len(argvs) < 2 :
    print "no file name"
    quit()

fileName = argvs[1]

if not os.path.exists(fileName):
    print "file does not exist"
    quit()

p = re.compile(u"[{|}|]")
p2 = re.compile(u",+")

with open(fileName, 'r') as f:
    bf = BeautifulSoup(f.read(),"html.parser")
    dictAll = bf.dict.dict
    frameName = ""
    for d in dictAll:
        if d.name == "key":
            frameName = d.get_text()
        elif d.name == "dict":
            for dd in d:
                if dd.name == "string":
                    print frameName, p2.sub(u" ",p.sub(u"",dd.get_text()))
                    break

このスクリプトの名前を「readSpCocos2ToCut編集[1].py」とすると、plistファイルを引数として

python readSpCocos2ToCut.py texture.plist > texture.list

を実行すれば、「texture.list」というファイルの中に、フレーム名、左上x座標、左上y座標、幅、高さのみを一覧にした以下のようなデータが書き込まれます。

MLRSPanel0000 4 4 126 111
UpgradeIcon0000 134 4 28 18
artilleryPanel0000 166 4 118 68

次に、ImagiMagick編集[2]のconvertコマンドを使った以下のシェルを書き、「cropTexture.sh 」という名前で保存します

cat $1 | while read FN TL TT W H
do
	convert $2 -crop "$W"x"$H"+$TL+$TT  $FN.png
done

そして先ほどのリストのファイルとtextureのpngファイルと実行すると、フレーム名のついたpngファイルが製作されます。

sh cropTexture.sh texture.list texture.png

Last-modified: 2018-05-06 (日) 19:39:06 (JST) (2171d) by njf