Ad

数年前、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編集.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編集の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

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード印刷に適した表示   ページ新規作成 全ページ一覧 単語検索 最新ページの一覧   ヘルプ   最新ページのRSS 1.0 最新ページのRSS 2.0 最新ページのRSS Atom Powered by xpWiki
Counter: 1670, today: 1, yesterday: 0
初版日時: 2018-05-06 (日) 19:20:12
最終更新: 2018-05-06 (日) 19:39:06 (JST) (2173d) by njf
MenuBar
広告

ログイン

ユーザー名:


パスワード:





パスワード紛失

Portuguese | English | German | Greek | Japanese | Korean | Russian | T-Chinese top
NJF