Skip to content

Commit

Permalink
Merge pull request KhronosGroup#1931 from KhronosGroup/fix_sk
Browse files Browse the repository at this point in the history
Fix sk export
  • Loading branch information
julienduroure authored May 24, 2023
2 parents 8dce2e3 + 65b423e commit ca85bdf
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 47 deletions.
10 changes: 10 additions & 0 deletions addons/io_scene_gltf2/blender/com/gltf2_blender_data_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ def get_delta_modes(target_property: str) -> str:

def is_bone_anim_channel(data_path: str) -> bool:
return data_path[:10] == "pose.bones"

def get_sk_exported(key_blocks):
return [
key_block
for key_block in key_blocks
if not skip_sk(key_block)
]

def skip_sk(k):
return k == k.relative_key or k.mute
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import bpy
import typing
from .....io.exp.gltf2_io_user_extensions import export_user_extensions
from .....blender.com.gltf2_blender_data_path import skip_sk
from .....io.com import gltf2_io_debug
from .....io.com import gltf2_io
from ....exp.gltf2_blender_gather_cache import cached
Expand Down Expand Up @@ -85,7 +86,7 @@ def get_channel_groups(obj_uuid: str, blender_action: bpy.types.Action, export_s
type_ = "BONE"
if blender_object.type == "MESH" and object_path.startswith("key_blocks"):
shape_key = blender_object.data.shape_keys.path_resolve(object_path)
if shape_key.mute is True:
if skip_sk(shape_key):
continue
target = blender_object.data.shape_keys
type_ = "SK"
Expand All @@ -95,7 +96,7 @@ def get_channel_groups(obj_uuid: str, blender_action: bpy.types.Action, export_s
if blender_object.type == "MESH":
try:
shape_key = blender_object.data.shape_keys.path_resolve(object_path)
if shape_key.mute is True:
if skip_sk(shape_key):
continue
target = blender_object.data.shape_keys
type_ = "SK"
Expand Down Expand Up @@ -190,9 +191,7 @@ def __get_channel_group_sorted(channels: typing.Tuple[bpy.types.FCurve], blender
shapekeys_idx = {}
cpt_sk = 0
for sk in blender_object.data.shape_keys.key_blocks:
if sk == sk.relative_key:
continue
if sk.mute is True:
if skip_sk(sk):
continue
shapekeys_idx[sk.name] = cpt_sk
cpt_sk += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import bpy
import typing
from .....blender.com.gltf2_blender_data_path import skip_sk
from ....com.gltf2_blender_data_path import get_target_object_path
from ...gltf2_blender_gather_cache import cached
from ..gltf2_blender_gather_keyframes import Keyframe
Expand Down Expand Up @@ -175,9 +176,7 @@ def __gather_non_keyed_values(
shapekeys_idx = {}
cpt_sk = 0
for sk in blender_object.data.shape_keys.key_blocks:
if sk == sk.relative_key:
continue
if sk.mute is True:
if skip_sk(sk):
continue
shapekeys_idx[cpt_sk] = sk.name
cpt_sk += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ....blender.com.gltf2_blender_data_path import get_sk_exported, skip_sk
from ...com.gltf2_blender_data_path import get_target_object_path
from ..gltf2_blender_gather_cache import skdriverdiscovercache

Expand Down Expand Up @@ -52,11 +53,7 @@ def get_sk_drivers(blender_armature_uuid, export_settings):

shapekeys_idx = {}
cpt_sk = 0
for sk in child.data.shape_keys.key_blocks:
if sk == sk.relative_key:
continue
if sk.mute is True:
continue
for sk in get_sk_exported(child.data.shape_keys.key_blocks):
shapekeys_idx[sk.name] = cpt_sk
cpt_sk += 1

Expand All @@ -74,8 +71,7 @@ def get_sk_drivers(blender_armature_uuid, export_settings):
sk_name = child.data.shape_keys.path_resolve(get_target_object_path(sk_c.data_path)).name
except:
continue
# Do not take into account this driver if corresponding SK is disabled
if child.data.shape_keys.key_blocks[sk_name].mute is True:
if skip_sk(child.data.shape_keys.key_blocks[sk_name]):
continue
idx_channel_mapping.append((shapekeys_idx[sk_name], sk_c))
existing_idx = dict(idx_channel_mapping)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import mathutils
import bpy
import typing
from .....blender.com.gltf2_blender_data_path import get_sk_exported
from ...gltf2_blender_gather_cache import datacache
from ...gltf2_blender_gather_tree import VExportNode
from ..gltf2_blender_gather_drivers import get_sk_drivers
Expand Down Expand Up @@ -155,7 +156,7 @@ def get_cache_data(path: str,
data[obj_uuid][blender_obj.data.shape_keys.animation_data.action.name] = {}
data[obj_uuid][blender_obj.data.shape_keys.animation_data.action.name]['sk'] = {}
data[obj_uuid][blender_obj.data.shape_keys.animation_data.action.name]['sk'][None] = {}
data[obj_uuid][blender_obj.data.shape_keys.animation_data.action.name]['sk'][None][frame] = [k.value if k.mute is False else 0.0 for k in blender_obj.data.shape_keys.key_blocks][1:]
data[obj_uuid][blender_obj.data.shape_keys.animation_data.action.name]['sk'][None][frame] = [k.value for k in get_sk_exported(blender_obj.data.shape_keys.key_blocks)]

elif export_settings['gltf_morph_anim'] and blender_obj.type == "MESH" \
and blender_obj.data is not None \
Expand All @@ -170,7 +171,7 @@ def get_cache_data(path: str,
if 'sk' not in data[obj_uuid][action_name].keys():
data[obj_uuid][action_name]['sk'] = {}
data[obj_uuid][action_name]['sk'][None] = {}
data[obj_uuid][action_name]['sk'][None][frame] = [k.value if k.mute is False else 0.0 for k in blender_obj.data.shape_keys.key_blocks][1:]
data[obj_uuid][action_name]['sk'][None][frame] = [k.value for k in get_sk_exported(blender_obj.data.shape_keys.key_blocks)]



Expand All @@ -184,7 +185,7 @@ def get_cache_data(path: str,
elif 'sk' not in data[obj_uuid][obj_uuid].keys():
data[obj_uuid][obj_uuid]['sk'] = {}
data[obj_uuid][obj_uuid]['sk'][None] = {}
data[obj_uuid][obj_uuid]['sk'][None][frame] = [k.value if k.mute is False else 0.0 for k in blender_obj.data.shape_keys.key_blocks][1:]
data[obj_uuid][obj_uuid]['sk'][None][frame] = [k.value for k in get_sk_exported(blender_obj.data.shape_keys.key_blocks)]

# caching driver sk meshes
# This will avoid to have to do it again when exporting SK animation
Expand All @@ -200,20 +201,20 @@ def get_cache_data(path: str,
data[dr_obj][obj_uuid + "_" + blender_obj.animation_data.action.name] = {}
data[dr_obj][obj_uuid + "_" + blender_obj.animation_data.action.name]['sk'] = {}
data[dr_obj][obj_uuid + "_" + blender_obj.animation_data.action.name]['sk'][None] = {}
data[dr_obj][obj_uuid + "_" + blender_obj.animation_data.action.name]['sk'][None][frame] = [k.value if k.mute is False else 0.0 for k in driver_object.data.shape_keys.key_blocks][1:]
data[dr_obj][obj_uuid + "_" + blender_obj.animation_data.action.name]['sk'][None][frame] = [k.value for k in get_sk_exported(driver_object.data.shape_keys.key_blocks)]
if blender_obj.animation_data \
and export_settings['gltf_animation_mode'] in ["NLA_TRACKS"]:
if obj_uuid + "_" + action_name not in data[dr_obj]:
data[dr_obj][obj_uuid + "_" + action_name] = {}
data[dr_obj][obj_uuid + "_" + action_name]['sk'] = {}
data[dr_obj][obj_uuid + "_" + action_name]['sk'][None] = {}
data[dr_obj][obj_uuid + "_" + action_name]['sk'][None][frame] = [k.value if k.mute is False else 0.0 for k in driver_object.data.shape_keys.key_blocks][1:]
data[dr_obj][obj_uuid + "_" + action_name]['sk'][None][frame] = [k.value for k in get_sk_exported(driver_object.data.shape_keys.key_blocks)]
else:
if obj_uuid + "_" + obj_uuid not in data[dr_obj]:
data[dr_obj][obj_uuid + "_" + obj_uuid] = {}
data[dr_obj][obj_uuid + "_" + obj_uuid]['sk'] = {}
data[dr_obj][obj_uuid + "_" + obj_uuid]['sk'][None] = {}
data[dr_obj][obj_uuid + "_" + obj_uuid]['sk'][None][frame] = [k.value if k.mute is False else 0.0 for k in driver_object.data.shape_keys.key_blocks][1:]
data[dr_obj][obj_uuid + "_" + obj_uuid]['sk'][None][frame] = [k.value for k in get_sk_exported(driver_object.data.shape_keys.key_blocks)]

frame += step
return data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import numpy as np
from ......blender.com.gltf2_blender_data_path import get_sk_exported
from ....gltf2_blender_gather_cache import cached
from ...gltf2_blender_gather_keyframes import Keyframe
from ..gltf2_blender_gather_animation_sampling_cache import get_cache_data
Expand All @@ -32,7 +33,7 @@ def gather_sk_sampled_keyframes(obj_uuid,
step = export_settings['gltf_frame_step']
blender_obj = export_settings['vtree'].nodes[obj_uuid].blender_object
while frame <= end_frame:
key = Keyframe([None] * (len(blender_obj.data.shape_keys.key_blocks)-1), frame, 'value')
key = Keyframe([None] * (len(get_sk_exported(blender_obj.data.shape_keys.key_blocks))), frame, 'value')
key.value_total = get_cache_data(
'sk',
obj_uuid,
Expand Down
17 changes: 3 additions & 14 deletions addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import bpy
from typing import Optional, Dict, List, Any, Tuple
from ...io.com import gltf2_io
from ...blender.com.gltf2_blender_data_path import get_sk_exported
from ...io.com.gltf2_io_debug import print_console
from ...io.exp.gltf2_io_user_extensions import export_user_extensions
from ..com.gltf2_blender_extras import generate_extras
Expand Down Expand Up @@ -118,12 +119,7 @@ def __gather_extras(blender_mesh: bpy.types.Mesh,
if export_settings['gltf_morph'] and blender_mesh.shape_keys:
morph_max = len(blender_mesh.shape_keys.key_blocks) - 1
if morph_max > 0:
target_names = []
for blender_shape_key in blender_mesh.shape_keys.key_blocks:
if blender_shape_key != blender_shape_key.relative_key:
if blender_shape_key.mute is False:
target_names.append(blender_shape_key.name)
extras['targetNames'] = target_names
extras['targetNames'] = [k.name for k in get_sk_exported(blender_mesh.shape_keys.key_blocks)]

if extras:
return extras
Expand Down Expand Up @@ -166,11 +162,4 @@ def __gather_weights(blender_mesh: bpy.types.Mesh,
if morph_max <= 0:
return None

weights = []

for blender_shape_key in blender_mesh.shape_keys.key_blocks:
if blender_shape_key != blender_shape_key.relative_key:
if blender_shape_key.mute is False:
weights.append(blender_shape_key.value)

return weights
return [k.value for k in get_sk_exported(blender_mesh.shape_keys.key_blocks)]
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import numpy as np
from ...io.com import gltf2_io, gltf2_io_constants, gltf2_io_extensions
from ...io.com.gltf2_io_debug import print_console
from ...blender.com.gltf2_blender_data_path import get_sk_exported
from ...io.exp import gltf2_io_binary_data
from .gltf2_blender_gather_cache import cached, cached_by_key
from . import gltf2_blender_gather_primitives_extract
Expand Down Expand Up @@ -197,12 +198,7 @@ def __gather_targets(blender_primitive, blender_mesh, modifiers, export_settings
targets = []
if blender_mesh.shape_keys is not None:
morph_index = 0
for blender_shape_key in blender_mesh.shape_keys.key_blocks:
if blender_shape_key == blender_shape_key.relative_key:
continue

if blender_shape_key.mute is True:
continue
for blender_shape_key in get_sk_exported(blender_mesh.shape_keys.key_blocks):

target_position_id = 'MORPH_POSITION_' + str(morph_index)
target_normal_id = 'MORPH_NORMAL_' + str(morph_index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import numpy as np
from mathutils import Vector
from ...blender.com.gltf2_blender_data_path import get_sk_exported
from ...io.com.gltf2_io_debug import print_console
from ...io.com.gltf2_io_constants import NORMALS_ROUNDING_DIGIT
from ...io.exp.gltf2_io_user_extensions import export_user_extensions
Expand Down Expand Up @@ -121,12 +122,9 @@ def prepare_data(self):
self.armature = None

self.key_blocks = []
# List of SK that are going to be exported, actually
if self.blender_mesh.shape_keys and self.export_settings['gltf_morph']:
self.key_blocks = [
key_block
for key_block in self.blender_mesh.shape_keys.key_blocks
if not (key_block == key_block.relative_key or key_block.mute)
]
self.key_blocks = get_sk_exported(self.blender_mesh.shape_keys.key_blocks)

# Fetch vert positions and bone data (joint,weights)

Expand Down
Binary file added tests/scenes/28_various_sk.blend
Binary file not shown.

0 comments on commit ca85bdf

Please sign in to comment.