Page 1 of 1

Simple reading/modifying the new file format with Python

Posted: Wed Oct 14, 2015 1:09 pm
by Lebostein
With the new file format *.anime it is very easy to check/modify the whole animation structure with Python (using the standard libraries zipfile and json).

1) Here is a simple code that demonstrates how you can change the dimensions of your animation project for example:

Code: Select all

import zipfile, json

filename = "test.anime"

# 1) extract the zip file
archive = zipfile.ZipFile(filename, "r")
animeprev = archive.read("preview.jpg")
animeproj = archive.read("Project.animeproj")
archive.close()

# 2) edit the json structure
anime = json.loads(animeproj)
anime["project_data"]["width"] = 1920
anime["project_data"]["height"] = 1080
animeproj = json.dumps(anime)

# 3) resave the zip file
archive = zipfile.ZipFile("mod_" + filename, "w", zipfile.ZIP_DEFLATED)
archive.writestr("preview.jpg", animeprev)
archive.writestr("Project.animeproj", animeproj)
archive.close()
2) Here an example to list all layer names and layer types in your file:

Code: Select all

import zipfile, json

filename = "Dragon.anime"

# recursive function to print layer info
def layerprint(path, depth):
	for layer in path:
		print depth * 2 * "-" + "|", layer["name"], "=", layer["type"]
		if layer.has_key("layers"): layerprint(layer["layers"], depth + 1)

# 1) extract the zip file
archive = zipfile.ZipFile(filename, "r")
animeproj = archive.read("Project.animeproj")
archive.close()

# 2) read the json structure
anime = json.loads(animeproj)
layerprint(anime["layers"], 0)
output:
| Dragon = BoneLayer
--| Wing R = BoneLayer
----| Layer 9 = MeshLayer
--| Arm R = BoneLayer
----| Layer 6 = MeshLayer
--| Leg Right = BoneLayer
----| Layer 4 = MeshLayer
--| Body = BoneLayer
----| Body = MeshLayer
----| inside = MeshLayer
----| lines 2 = MeshLayer
----| shading back 2 = MeshLayer
--| Leg Left = BoneLayer
----| Calf = BoneLayer
------| Layer 2 = MeshLayer
------| Layer 2 = MeshLayer
----| foot = MeshLayer
----| thigh = BoneLayer
------| thigh = MeshLayer
------| lines = MeshLayer
------| shading back = MeshLayer
--| Arm L = BoneLayer
----| Layer 6 = MeshLayer
--| Wing = BoneLayer
----| Layer 8 = MeshLayer
--| head = BoneLayer
----| Horn = BoneLayer
------| Layer 5 = MeshLayer
------| Layer 2 = MeshLayer
----| Horn 2 = BoneLayer
------| Layer 5 = MeshLayer
------| Layer 2 = MeshLayer
----| head = MeshLayer
----| ear = MeshLayer
----| eye = MeshLayer

Re: Simple reading/modifying the new file format with Python

Posted: Thu Oct 15, 2015 1:16 am
by Lebostein
To fix the decimal problem while dumping a json structure into a file with Python (example: 0.229591 <> 0.22959099999999999 in Python) you can use a self made encoder in combination with Decimal objects:

Code: Select all

import zipfile, json, decimal

class fakefloat(float):
	def __init__(self, value): self._value = value
	def __repr__(self): return str(self._value)

class DecimalEncoder(json.JSONEncoder):
	def default(self, obj):
		if isinstance(obj, decimal.Decimal): return fakefloat(obj)
		return json.JSONEncoder.default(self, obj)

# decoding
anime = json.loads(animeproj, parse_float=decimal.Decimal)

# encoding
animeproj = json.dumps(anime, cls=DecimalEncoder)

Re: Simple reading/modifying the new file format with Python

Posted: Fri Oct 16, 2015 10:08 am
by neeters_guy
Very useful information. This really needs to be expanded as a resource.

Re: Simple reading/modifying the new file format with Python

Posted: Fri Oct 16, 2015 11:47 am
by ernpchan
Thanks for sharing! Very helpful.

Re: Simple reading/modifying the new file format with Python

Posted: Sun Oct 18, 2015 2:57 pm
by JaMike
I don't know much about Python, but this got me interested in it, and my first bit of research led me to find there are two versions of Python! Which one does this work with?

Re: Simple reading/modifying the new file format with Python

Posted: Mon Oct 19, 2015 7:38 am
by Lebostein
I am using Python 2.7. But there is a new Python generation Python 3.x.
The code is normally compatible between 2 and 3. The only huge difference between Python 2 and Python 3 is the print statement:
* In Python 2 it is a command: print "test"
* In Python 3 it is a function: print("test")
Other thing is the integer division. In Python 2 you get an integer (5/2=2) and in Python 3 you get a float (5/2=2.5). To get a float in Python 2, you have to work with floats (5/2.=2.5)

Here is a good summary:
http://sebastianraschka.com/Articles/20 ... _diff.html