Simple reading/modifying the new file format with Python

General Moho topics.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
Lebostein
Posts: 111
Joined: Sun Jul 12, 2009 10:24 am

Simple reading/modifying the new file format with Python

Post 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
Last edited by Lebostein on Thu Oct 15, 2015 7:08 pm, edited 1 time in total.
Lebostein
Posts: 111
Joined: Sun Jul 12, 2009 10:24 am

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

Post 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)
User avatar
neeters_guy
Posts: 1619
Joined: Mon Sep 14, 2009 7:33 pm
Contact:

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

Post by neeters_guy »

Very useful information. This really needs to be expanded as a resource.
ernpchan
Posts: 156
Joined: Thu Aug 13, 2015 7:18 pm
Contact:

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

Post by ernpchan »

Thanks for sharing! Very helpful.
My opinions and comments do not represent those of my employer.
http://www.ernestpchan.com
http://www.zazzle.com/gopuggo
JaMike
Posts: 357
Joined: Sat Jul 16, 2011 6:36 pm

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

Post 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?
Lebostein
Posts: 111
Joined: Sun Jul 12, 2009 10:24 am

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

Post 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
Post Reply