python写的一个生成cocos2dx的plist的小工具

最近研究了一下cocos2dx,想知道大师的杰作和自己的之前的设计差在哪。以后贴出自己的感悟,用来以后回顾。现在先贴出一个python小工具。自己写的,用来生成一个plist文件,因为手写plist文件实在是太麻烦了。

limit : anim.plist只支持format 2  而且  frame的plist也是只支持format2 .

目前写的test()函数是把一个spritesheets分割成为i*j个不同的小frame,同时自动生成的anim必须满足是同一行的属于一个anim动画。

比如三国志曹操传的人物资源就是。

如果想要做其他的改变,比如修改参数啥的,必须要输入大量的参数。很多都是默认的,只是一个模板而已,如果要修改,也是很简单就可以满足你的功能,因为主要的逻辑我以及表达清楚了,详细的看python代码。

参数 图像的宽。图像高。输入文件。等分的hn个,wn个。

import sys
def level_str(s , level) : 
	ret = ""
	for i in range(level) : 
		ret = ret + "\t"
	return ret + s
def write(f , s , level) : 
	f.write(level_str(s , level))
def print_dir(f , d , level) : 
	write(f , "<dict>\n" , level)
	tl = level + 1
	for key in d : 
		val = d[key] 
		write(f , '<key>%s</key>\n' % (key) , tl)
		if(type(val) == type("a")) :
			write(f , "<string>%s</string>\n"%(val) , tl)
		elif (type(val) == type({})) : 
			print_dir(f , val , tl)
		elif (type(val) == type([])) : 
			print_array(f , val , tl)
	write(f , "</dict>\n" , level)

def print_array(f , a , level) : 
	tl = level + 1
	write(f , "<array>\n" , level)
	for val in a :
		if(type(val) == type("a")) :
			write(f , "<string>%s</string>\n"%(val) , tl)
		elif (type(val) == type({})) : 
			print_dir(f , val , tl)
		elif (type(val) == type([])) :
			print_array(f , val , tl)
	write(f , "</array>\n" , level)


def print_plist(outfile , root) : 
	with open(outfile , "wb") as f : 
		f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
		f.write("<plist>\n")
		print_dir(f , root , 1)
		f.write("</plist>\n")
######################################################################
#upper is the tools 
#below is the tool_user
######################################################################
def generator_frame_directory(w , h , filename , hn , wn) : 
	root = {} ;
	root["metadata"] = {"format":"2" , "size":"{%d,%d}"%(w,h) , "textureFileName":filename}
	frameDir = {}
	root["frames"] = frameDir
	
####generator the frame data ; 
	sw = w / wn
	sh = h / hn
	for i in range(hn) : 
		for j in range(wn) : 
			tmpDir = {} 
			noww = sw * j
			nowh = sh * i
			tmpDir["frame"] = "{{%d,%d},{%d,%d}}" % (noww , nowh , sw , sh)
			tmpDir["offset"] = "{0,0}"
			tmpDir["rotated"] = "0"
			tmpDir["sourceSize"] = "{%d,%d}" % (sw , sh)
			frameDir[filename+str(i)+str(j)] = tmpDir

	print_plist(filename+".plist" , root)
	return root

def generator_animation_directory(filename , hn , wn) : 
	root = {}
	root["properties"] = {"format":"2" , "spritesheets":[filename+".plist"]}
	animsDir = {}
	root["animations"] = animsDir
	for i in range(hn) : 
		_animDir = {"loops":"10" , "restoreOriginalFrame":filename+str(i)+"0" , "delayPerUnit":"0.3"}
		animsDir[filename+".anim"+str(i)] = _animDir
		frameName = []
		_animDir["frames"] = frameName
		for j in range(wn) : 
			frameInfo = {"spriteframe":filename + str(i) + str(j) , "delayUnits":"1" , "notification":{}}
			frameName.append(frameInfo)
		
	print_plist(filename+".anim.plist" , root) 
	return root

def test() : 
	generator_frame_directory(256,256,"knight.png",4,4)
	generator_animation_directory("knight.png" ,4 ,4)


#####main function
test()


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值