File size: 8,310 Bytes
26d5eb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import pandas as pd
import numpy as np
from pathlib import Path

import contextlib
import fbx
from typing import List, Union

# Import custom data.
import globals


class MarkerData:
    # TODO: Model is currently built for training. Add testing mode.
    def __init__(self, fbx_file: Path):
        """
        Class that stores references to important nodes in an FBX file.
        Offers utility functions to quickly load animation data.
        :param fbx_file: `str` Path to the file to load.
        """
        self.time_modes = globals.get_time_modes()
        self.marker_names = globals.get_marker_names()

        self.markers = []
        self.actor_names = []
        self.actors = []

        self.volume_dim_x = 10.
        self.volume_dim_y = 4.

        self.fbx_file = fbx_file
        self.valid_frames = []

        self.__init_scene()
        self.__init_anim()
        self.__init_actors()
        self.__init_markers()

    def __init_scene(self):
        # Create an FBX manager and importer
        manager = fbx.FbxManager.Create()
        importer = fbx.FbxImporter.Create(manager, '')

        # Import the FBX file
        importer.Initialize(str(self.fbx_file))
        self.scene = fbx.FbxScene.Create(manager, '')
        importer.Import(self.scene)
        self.root = self.scene.GetRootNode()
        self.time_mode = self.scene.GetGlobalSettings().GetTimeMode()

        # Destroy importer to remove reference to imported file.
        # This will allow us to delete the uploaded file.
        importer.Destroy()

    def __init_anim(self):
        # Get the animation stack and layer.
        anim_stack = self.scene.GetCurrentAnimationStack()
        self.anim_layer = anim_stack.GetSrcObject(fbx.FbxCriteria.ObjectType(fbx.FbxAnimLayer.ClassId), 0)

        # Find the total number of frames to expect from the local time span.
        local_time_span = anim_stack.GetLocalTimeSpan()
        self.num_frames = int(local_time_span.GetDuration().GetFrameCount(self.time_mode))

    def __init_actors(self):

        # Find all parent nodes (/System, /_Unlabeled_Markers, /Actor1, etc).
        gen1_nodes = [self.root.GetChild(i) for i in range(self.root.GetChildCount())]
        for gen1_node in gen1_nodes:
            gen2_nodes = [gen1_node.GetChild(i) for i in
                          range(gen1_node.GetChildCount())]  # Actor nodes (/Mimi/Hips, /Mimi/ARIEL, etc)

            # If the first 3 marker names are children of this parent, it must be an actor.
            if all(name in [node.GetName() for node in gen2_nodes] for name in self.marker_names[:4]):
                self.actor_names.append(gen1_node.GetName())
                self.actors.append(gen1_node)

        self.actor_count = len(self.actors)
        self.valid_frames = [[] for _ in range(self.actor_count)]

    def __init_markers(self):
        for actor_node in self.actors:
            actor_markers = {}
            for marker_name in self.marker_names:
                for actor_idx in range(actor_node.GetChildCount()):
                    child = actor_node.GetChild(actor_idx)
                    child_name = child.GetName()
                    if child_name == marker_name:
                        actor_markers[child_name] = child

            assert len(actor_markers) == len(self.marker_names), f'{actor_node.GetName()} does not have all markers.'

            self.markers.append(actor_markers)

    def _check_actor(self, actor: int = 0):
        assert 0 <= actor <= self.actor_count, f'Actor number must be between 0 and {self.actor_count - 1}. ' \
                                               f'It is {actor}.'

    def _set_valid_frames_for_actor(self, actor: int = 0):
        self._check_actor(actor)

        frames = list(range(self.num_frames))
        for marker_name in self.marker_names:
            marker = self.markers[actor][marker_name]
            t_curve = marker.LclTranslation.GetCurve(self.anim_layer, 'X')
            keys = [t_curve.KeyGet(i).GetTime().GetFrameCount(self.time_mode) for i in range(t_curve.KeyGetCount())]
            for frame in frames:
                if frame not in keys:
                    with contextlib.suppress(ValueError):
                        frames.remove(frame)

        self.valid_frames[actor] = frames

    def _check_valid_frames(self, actor: int = 0):
        if not len(self.valid_frames[actor]):
            self._set_valid_frames_for_actor(actor)

    def _modify_pose(self, actor, frame) -> List[float]:
        # Set new frame to evaluate at.
        time = fbx.FbxTime()
        time.SetFrame(frame)
        # Prepare arrays for each axis.
        x, y, z = [], [], []

        # For each marker, store the x, y and z global position.
        for n, m in self.markers[actor].items():
            t = m.EvaluateGlobalTransform(time).GetRow(3)
            x += [t[0] * 0.01]
            y += [t[1] * 0.01]
            z += [t[2] * 0.01]

        # Move the point cloud to the center of the x and y axes. This will put the actor in the middle.
        x = self.center_axis(x)
        z = self.center_axis(z)

        # Move the actor to the middle of the volume floor by adding volume_dim_x/2 to x and z.
        x += self.volume_dim_x / 2.
        z += self.volume_dim_x / 2.

        # Squeeze the actor into the 1x1 plane for the neural network by dividing the axes.
        x /= self.volume_dim_x
        z /= self.volume_dim_x
        y = np.array(y) / self.volume_dim_y

        # TODO: Optionally: Add any extra modifications to the point cloud here.

        # Append all values to a new array, one axis at a time.
        # This way it will match the column names order.
        pose = []
        for i in range(len(x)):
            pose += [x[i]]
            pose += [y[i]]
            pose += [z[i]]
        return pose

    def get_marker_by_name(self, actor: int, name: str):
        self._check_actor(actor)
        return self.markers[actor][name]

    def get_valid_frames_for_actor(self, actor: int = 0):
        self._check_valid_frames(actor)
        return self.valid_frames[actor]

    def print_valid_frames_stats_for_actor(self, actor: int = 0):
        self._check_actor(actor)
        self._check_valid_frames(actor)

        len_valid = len(self.valid_frames[actor])
        ratio = (len_valid / self.num_frames) * 100
        print(f'Actor {self.actor_names[actor]}: Total: {self.num_frames}, valid: {len_valid}, missing: '
              f'{self.num_frames - len_valid}, ratio: {ratio:.2f}% valid.')

        return self.actor_names[actor], self.num_frames, len_valid, ratio

    def columns_from_joints(self):
        columns = []
        for name in self.marker_names:
            columns += [f'{name}x', f'{name}y', f'{name}z']

        return columns

    @staticmethod
    def center_axis(a) -> np.array:
        a = np.array(a)
        _min = np.min(a)
        _max = np.max(a)

        _c = _max - _min
        a -= _c
        return a

    def extract_translations_per_actor(self, actor: int = 0):
        self._check_actor(actor)
        self._check_valid_frames(actor)

        poses = []
        # Go through all valid frames for this actor.
        # Note that these frames can be different per actor.
        for frame in self.valid_frames[actor]:
            # Get the centered point cloud as an array.
            pose_at_frame = self._modify_pose(actor, frame)
            poses.append(pose_at_frame)

        return poses

    def extract_all_translations(self) -> pd.DataFrame:

        columns = self.columns_from_joints()

        all_poses = []

        for i in range(self.actor_count):
            all_poses.extend(self.extract_translations_per_actor(i))

        return pd.DataFrame(all_poses, columns=columns)

    def export(self, t: str = 'csv', output_file: Path = None) -> Union[bytes, Path]:
        # Get the dataframe with all animation data.
        df = self.extract_all_translations()

        if t == 'string':
            return df.to_csv(index=False).encode('utf-8')

        if output_file is None:
            output_file = self.fbx_file.with_suffix('.csv')

        if output_file.suffix != '.csv':
            raise ValueError(f'{output_file} needs to be a .csv file.')

        df.to_csv(output_file, index=False)
        return output_file