Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/model.py
blob: 479af627ef163ec24c0ae0168add44ce4fcfb66d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import pickle

import theme
import kinematic

keys = []

class KeyFrame:
    def __init__(self):
        self.clear()

    def empty(self):
        return self.joints == None

    def assign(self, x):
        self.middle = x.middle
        self.parts = x.parts.copy()
        self.sticks = x.sticks.copy()
        self.joints = x.joints.copy()
        self.scaled_sticks = x.get_scaled_sticks()
        self.scaled_joints = x.get_scaled_joints(self.x,
                int(theme.KEYFRAMEHEIGHT/2.0))

    def clear(self):
        self.middle = None
        self.parts = None
        self.sticks = None
        self.scaled_sticks = None
        self.joints = None
        self.scaled_joints = None

    def move(self, dx):
        if self.scaled_joints:
            for jname in self.scaled_joints:
                (jx, jy) = self.scaled_joints[jname]
                self.scaled_joints[jname] = (jx+dx, jy)
        self.x += dx

def save(filename):
    out = []

    for i in keys:
        out.append({
            'x'             : i.x,
            'middle'        : i.middle,
            'parts'         : i.parts,
            'sticks'        : i.sticks,
            'joints'        : i.joints,
            'scaled_sticks' : i.scaled_sticks,
            'scaled_joints' : i.scaled_joints })

    file(filename, 'w').write(pickle.dumps(out))

def load(filename):
    inc = pickle.loads(file(filename, 'r').read())

    for i, data in enumerate(inc):
        key = keys[i]

        key.x = data['x']
        key.middle = data['middle']
        key.parts = data['parts']
        key.sticks = data['sticks']
        key.joints = data['joints']
        key.scaled_sticks = data['scaled_sticks']
        key.scaled_joints = data['scaled_joints']

def getparentsticks(stickname):
    if stickname in ['RIGHT SHOULDER','LEFT SHOULDER','NECK','TORSO']:
        return []
    if stickname in ['HEAD']:
        return ['NECK']
    if stickname == 'UPPER RIGHT ARM':
        return ['RIGHT SHOULDER']
    if stickname == 'LOWER RIGHT ARM':
        return ['UPPER RIGHT ARM','RIGHT SHOULDER']
    if stickname == 'UPPER LEFT ARM':
        return ['LEFT SHOULDER']
    if stickname == 'LOWER LEFT ARM':
        return ['UPPER LEFT ARM','LEFT SHOULDER']
    if stickname == 'RIGHT HIP':
        return ['TORSO']
    if stickname == 'UPPER RIGHT LEG':
        return ['RIGHT HIP','TORSO']
    if stickname == 'LOWER RIGHT LEG':
        return ['UPPER RIGHT LEG','RIGHT HIP','TORSO']
    if stickname == 'RIGHT FOOT':
        return ['LOWER RIGHT LEG','UPPER RIGHT LEG','RIGHT HIP','TORSO']
    if stickname == 'LEFT HIP':
        return ['TORSO']
    if stickname == 'UPPER LEFT LEG':
        return ['LEFT HIP','TORSO']
    if stickname == 'LOWER LEFT LEG':
        return ['UPPER LEFT LEG','LEFT HIP','TORSO']
    if stickname == 'LEFT FOOT':
        return ['LOWER LEFT LEG','UPPER LEFT LEG','LEFT HIP','TORSO']

def getparentjoint(jname, joints, middle):
    if jname in ['rightshoulder','leftshoulder','groin','neck']:
        return middle

    parentjoints = {'rightelbow':'rightshoulder',
                    'righthand':'rightelbow',
                    'leftelbow':'leftshoulder',
                    'lefthand':'leftelbow',
                    'righthip':'groin',
                    'rightknee':'righthip',
                    'rightheel':'rightknee',
                    'righttoe':'rightheel',
                    'lefthip':'groin',
                    'leftknee':'lefthip',
                    'leftheel':'leftknee',
                    'lefttoe':'leftheel',
                    'head':'neck'}

    return joints[parentjoints[jname]]

def screen_shot(pixbuf):
    tmpdir = '/tmp'

    filename = 'fp%03d.png' % i
    filepath = os.path.join(tmpdir,filename)
    pixbuf.save(filepath,'png')

    from sugar.datastore import datastore
    mediaObject = datastore.create()
    mediaObject.metadata['title'] = 'FlipSticks PNG'
    thumbData = _get_base64_pixbuf_data(pixbuf)
    mediaObject.metadata['preview'] = thumbData
    #medaiObject.metadata['icon-color'] = ''
    mediaObject.metadata['mime_type'] = 'image/png'
    mediaObject.file_path = filepath
    datastore.write(mediaObject)

def _save_data_to_buffer_cb(buf, data):
    data[0] += buf
    return True

def _get_base64_pixbuf_data(pixbuf):
    data = [""]
    pixbuf.save_to_callback(_save_data_to_buffer_cb, "png", {}, data)
    import base64
    return base64.b64encode(str(data[0]))

for i in range(5):
    key = KeyFrame()
    keyframe_width = theme.KEYFRAMEWIDTH/5
    key.x = keyframe_width/2 + i*keyframe_width
    keys.append(key)