Skip to content

Commit

Permalink
Added AnimLayers module to mGear Core
Browse files Browse the repository at this point in the history
- added more layer functionality
  • Loading branch information
SimonBenAnderson committed Oct 17, 2023
1 parent 124953c commit 027b076
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 28 deletions.
69 changes: 66 additions & 3 deletions release/scripts/mgear/core/animLayers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
import maya.api.OpenMaya as om

def animation_layer_exists(layer_name):
"""Checks ig animation layer exists.
:param str layer_name: Name of the layer that will be checked.
:return: True if the animation layer exists
:rtype: bool
"""
is_anim_layer = False

if not layer_name:
Expand All @@ -15,6 +21,12 @@ def animation_layer_exists(layer_name):


def base_animation_layer_name():
"""Finds the name of the base animation layer, as this base layer might not
be named the default "BaseAnimation".
:return: name of the base animation layer.
:rtype: str
"""
if animation_layer_exists("BaseAnimation"):
return "BaseAnimation"

Expand All @@ -28,6 +40,12 @@ def base_animation_layer_name():


def find_anim_layer_base_nodes():
"""Finds the animation base layer, as this base layer might not
be named the default "BaseAnimation".
:return: list of Maya Objects.
:rtype: list[om.MObject]
"""
anim_layer_nodes = []

# Create an iterator to iterate through all nodes
Expand Down Expand Up @@ -56,8 +74,8 @@ def find_anim_layer_base_nodes():
def all_anim_layers_ordered(include_base_animation=True):
"""Recursive function that returns all available animation layers within current scene.
Returns:
list[str]: list of animation layers.
:return: list of animation layers.
:rtype: list[str]
"""

def _add_node_recursive(layer_node):
Expand All @@ -78,4 +96,49 @@ def _add_node_recursive(layer_node):
if "BaseAnimation" in all_layers:
all_layers.remove("BaseAnimation")

return all_layers
return all_layers


def get_layer_weights():
"""
Gets all the animation layer weights.
:return: Dictionary with the name of the animation layer, followed by the weight.
:rtype: dict
"""
anim_layer_weights = {}
anim_layers = all_anim_layers_ordered(include_base_animation=False)

for anim_layer in anim_layers:
anim_layer_weights[anim_layer] = cmds.animLayer(anim_layer, query=True, weight=True)

return anim_layer_weights


def set_layer_weights(anim_layer_weights):
"""
Sets the animation layer weights.
:param dict anim_layer_weights: Dictionary containing all the animation layer names, and the weights to be set for each anim layer name.
"""
for name, weight in anim_layer_weights.items():
cmds.animLayer(name, edit=True, weight=weight)


def set_layer_weight(name, value=1.0, toggle_other_off=False, include_base=False):
"""
Set a specific AnimationLayers weight.
:param str name: Name of the animation layer to have its weight modified.
:param float value: weight of the animation layer
:param bool toggle_other_off: Turn all other layers off
:param bool include_base: include the base animation layer when toggling off layers.
"""
anim_layers = all_anim_layers_ordered(include_base_animation=include_base)

if toggle_other_off:
for layer_name in anim_layers:
cmds.animLayer(layer_name, edit=True, weight=0.0)

cmds.animLayer(name, edit=True, weight=value)

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from mgear.vendor.Qt import QtWidgets, QtCore, QtGui

from mgear.core import pyqt, utils as coreUtils
from mgear.core import pyqt, utils as coreUtils, animLayers
from mgear.shifter.game_tools_fbx import fbx_export_node, utils


Expand Down Expand Up @@ -253,7 +253,7 @@ def refresh(self):
with pyqt.block_signals(self._anim_layer_combo):
self._anim_layer_combo.clear()
# TODO: Maybe we should filter display layers that are set with override mode?
anim_layers = utils.all_anim_layers_ordered()
anim_layers = animLayers.all_anim_layers_ordered()
self._anim_layer_combo.addItems(["None"] + anim_layers)
self._anim_layer_combo.setCurrentText(
anim_clip_data.get("anim_layer", "None")
Expand Down Expand Up @@ -398,7 +398,7 @@ def showPopup(self):

self.clear()

anim_layers = utils.all_anim_layers_ordered()
anim_layers = animLayers.all_anim_layers_ordered()
self.addItems(["None"] + anim_layers)

self.setCurrentText(currentText)
Expand Down
29 changes: 7 additions & 22 deletions release/scripts/mgear/shifter/game_tools_fbx/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,13 @@ def export_animation_clip(config_data, clip_data):
clip_data: Information about the clip to be exported.
"""
# Clip Data
start_frame = clip_data.get("startFrame",
start_frame = clip_data.get("start_frame",
cmds.playbackOptions(query=True, minTime=True))
end_frame = clip_data.get("endFrame",
end_frame = clip_data.get("end_frame",
cmds.playbackOptions(query=True, maxTime=True))
title = clip_data.get("title", "")
frame_rate = clip_data.get("frameRate", coreUtils.get_frame_rate())
anim_layer = clip_data.get("animLayer", "")
frame_rate = clip_data.get("geo_root", coreUtils.get_frame_rate())
anim_layer = clip_data.get("anim_layer", "")

# Config Data
root_joint = config_data.get("joint_root", "")
Expand Down Expand Up @@ -351,26 +351,12 @@ def export_animation_clip(config_data, clip_data):
original_end_frame = cmds.playbackOptions(query=True, maxTime=True)
temp_mesh = None
temp_skin_cluster = None
original_anim_layer_weights = None
original_anim_layer_weights = animLayers.get_layer_weights()

try:
# set anim layer to enable
if animLayers.animation_layer_exists(anim_layer):
to_activate = None
to_deactivate = []
anim_layers = animLayers.all_anim_layers_ordered(include_base_animation=False)
original_anim_layer_weights = {
anim_layer: cmds.animLayer(anim_layer, query=True, weight=True)
for anim_layer in anim_layers
}
for found_anim_layer in anim_layers:
if found_anim_layer != anim_layer:
to_deactivate.append(found_anim_layer)
else:
to_activate = found_anim_layer
for anim_layer_to_deactivate in to_deactivate:
cmds.animLayer(anim_layer_to_deactivate, edit=True, weight=0.0)
cmds.animLayer(to_activate, edit=True, weight=1.0)
animLayers.set_layer_weight(anim_layer, toggle_other_off=True)

# disable viewport
mel.eval("paneLayout -e -manage false $gMainPane")
Expand Down Expand Up @@ -463,8 +449,7 @@ def export_animation_clip(config_data, clip_data):
finally:
# setup again original anim layer weights
if anim_layer and original_anim_layer_weights:
for name, weight in original_anim_layer_weights.items():
cmds.animLayer(name, edit=True, weight=weight)
animLayers.set_layer_weights(original_anim_layer_weights)

if temp_skin_cluster and cmds.objExists(temp_skin_cluster):
cmds.delete(temp_skin_cluster)
Expand Down

0 comments on commit 027b076

Please sign in to comment.