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
|
# Copyright 2018-2019 The glTF-Blender-IO authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import bpy
from mathutils import Vector
from ...io.imp.gltf2_io_binary import BinaryData
from .gltf2_blender_animation_utils import simulate_stash, make_fcurve
from .gltf2_blender_vnode import VNode
class BlenderNodeAnim():
"""Blender Object Animation."""
def __new__(cls, *args, **kwargs):
raise RuntimeError("%s should not be instantiated" % cls)
@staticmethod
def anim(gltf, anim_idx, node_idx):
"""Manage animation."""
node = gltf.data.nodes[node_idx]
vnode = gltf.vnodes[node_idx]
obj = vnode.blender_object
fps = bpy.context.scene.render.fps
animation = gltf.data.animations[anim_idx]
if anim_idx not in node.animations.keys():
return
for channel_idx in node.animations[anim_idx]:
channel = animation.channels[channel_idx]
if channel.target.path in ['translation', 'rotation', 'scale']:
break
else:
return
action = gltf.action_cache.get(obj.name)
if not action:
name = animation.track_name + "_" + obj.name
action = bpy.data.actions.new(name)
gltf.needs_stash.append((obj, animation.track_name, action))
gltf.action_cache[obj.name] = action
if not obj.animation_data:
obj.animation_data_create()
obj.animation_data.action = action
for channel_idx in node.animations[anim_idx]:
channel = animation.channels[channel_idx]
keys = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].input)
values = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].output)
if channel.target.path not in ['translation', 'rotation', 'scale']:
continue
if animation.samplers[channel.sampler].interpolation == "CUBICSPLINE":
# TODO manage tangent?
values = [values[idx * 3 + 1] for idx in range(0, len(keys))]
if channel.target.path == "translation":
blender_path = "location"
group_name = "Location"
num_components = 3
if vnode.parent is not None and gltf.vnodes[vnode.parent].type == VNode.Bone:
# Nodes with a bone parent need to be translated
# backwards by their bone length (always 1 currently)
off = Vector((0, -1, 0))
values = [gltf.loc_gltf_to_blender(vals) + off for vals in values]
else:
values = [gltf.loc_gltf_to_blender(vals) for vals in values]
elif channel.target.path == "rotation":
blender_path = "rotation_quaternion"
group_name = "Rotation"
num_components = 4
values = [gltf.quaternion_gltf_to_blender(vals) for vals in values]
# Manage antipodal quaternions
for i in range(1, len(values)):
if values[i].dot(values[i-1]) < 0:
values[i] = -values[i]
elif channel.target.path == "scale":
blender_path = "scale"
group_name = "Scale"
num_components = 3
values = [gltf.scale_gltf_to_blender(vals) for vals in values]
coords = [0] * (2 * len(keys))
coords[::2] = (key[0] * fps for key in keys)
for i in range(0, num_components):
coords[1::2] = (vals[i] for vals in values)
make_fcurve(
action,
coords,
data_path=blender_path,
index=i,
group_name=group_name,
interpolation=animation.samplers[channel.sampler].interpolation,
)
|