Skip to content

Commit

Permalink
Expose all graph outputs in the animation player (#68)
Browse files Browse the repository at this point in the history
Currently there is no easy way of accessing any outputs of the animation
graph other than the main `"pose"` output.

This PR exposes all of the configured graph outputs in the `get_outputs`
method of `AnimationGraphPlayer`.
  • Loading branch information
mbrea-c authored Sep 24, 2024
1 parent 13485e9 commit ccdee35
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
15 changes: 10 additions & 5 deletions crates/bevy_animation_graph/src/core/animation_graph/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
prelude::{
DataSpec, DataValue, DeferredGizmos, OptDataSpec, PassContext, SpecContext, SystemResources,
},
utils::{ordered_map::OrderedMap, unwrap::UnwrapVal},
utils::ordered_map::OrderedMap,
};
use bevy::{
prelude::*,
Expand Down Expand Up @@ -819,7 +819,7 @@ impl AnimationGraph {
root_entity: Entity,
entity_map: &HashMap<BoneId, Entity>,
deferred_gizmos: &mut DeferredGizmos,
) -> Result<Pose, GraphError> {
) -> Result<HashMap<PinId, DataValue>, GraphError> {
self.query_with_overlay(
time_update,
context_arena,
Expand All @@ -841,7 +841,7 @@ impl AnimationGraph {
root_entity: Entity,
entity_map: &HashMap<BoneId, Entity>,
deferred_gizmos: &mut DeferredGizmos,
) -> Result<Pose, GraphError> {
) -> Result<HashMap<PinId, DataValue>, GraphError> {
context_arena.next_frame();
let mut ctx = PassContext::new(
context_arena.get_toplevel_id(),
Expand All @@ -856,8 +856,13 @@ impl AnimationGraph {
|c| c.set_time_update_back(TargetPin::OutputTime, time_update),
CacheWriteFilter::Primary,
);
self.get_data(TargetPin::OutputData(DEFAULT_OUTPUT_POSE.into()), ctx)
.map(|d| d.val())
let mut outputs = HashMap::new();
for k in self.output_parameters.keys() {
let out = self.get_data(TargetPin::OutputData(k.clone()), ctx.clone())?;
outputs.insert(k.clone(), out);
}

Ok(outputs)
}
// ----------------------------------------------------------------------------------------
}
16 changes: 10 additions & 6 deletions crates/bevy_animation_graph/src/core/animation_graph_player.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{
animation_graph::{AnimationGraph, InputOverlay, TimeState, TimeUpdate},
animation_graph::{AnimationGraph, InputOverlay, PinId, TimeState, TimeUpdate},
context::{BoneDebugGizmos, DeferredGizmos, PassContext},
edge_data::{AnimationEvent, DataValue, EventQueue, SampledEvent},
errors::GraphError,
pose::{BoneId, Pose},
pose::BoneId,
prelude::GraphContextArena,
skeleton::Skeleton,
};
Expand All @@ -27,7 +27,7 @@ pub struct AnimationGraphPlayer {
pub(crate) debug_draw_bones: Vec<BoneId>,
pub(crate) entity_map: HashMap<BoneId, Entity>,
pub(crate) queued_events: EventQueue,
pub(crate) pose: Option<Pose>,
pub(crate) outputs: HashMap<PinId, DataValue>,

input_overlay: InputOverlay,
/// Error that ocurred during graph evaluation in the last frame
Expand Down Expand Up @@ -96,6 +96,7 @@ impl AnimationGraphPlayer {

/// Query the animation graph with the latest time update and inputs
pub(crate) fn update(&mut self, system_resources: &SystemResources, root_entity: Entity) {
self.outputs.clear();
self.input_overlay.parameters.insert(
Self::USER_EVENTS.into(),
std::mem::take(&mut self.queued_events).into(),
Expand All @@ -118,13 +119,12 @@ impl AnimationGraphPlayer {
&self.entity_map,
&mut self.deferred_gizmos,
) {
Ok(pose) => {
Ok(outputs) => {
self.error = None;
self.pose = Some(pose);
self.outputs = outputs;
}
Err(error) => {
self.error = Some(error);
self.pose = None;
}
};
}
Expand Down Expand Up @@ -205,4 +205,8 @@ impl AnimationGraphPlayer {
pub fn get_error(&self) -> Option<GraphError> {
self.error.clone()
}

pub fn get_outputs(&self) -> &HashMap<PinId, DataValue> {
&self.outputs
}
}
5 changes: 3 additions & 2 deletions crates/bevy_animation_graph/src/core/systems.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::{
animation_clip::EntityPath,
animation_graph::{TimeUpdate, UpdateTime},
animation_graph::{TimeUpdate, UpdateTime, DEFAULT_OUTPUT_POSE},
animation_graph_player::AnimationGraphPlayer,
pose::BoneId,
prelude::DataValue,
};
use crate::prelude::SystemResources;
use bevy::{
Expand Down Expand Up @@ -111,7 +112,7 @@ pub fn apply_animation_to_targets(
let Ok(player) = graph_players.get(target.player) else {
continue;
};
let Some(pose) = &player.pose else {
let Some(DataValue::Pose(pose)) = player.outputs.get(DEFAULT_OUTPUT_POSE) else {
continue;
};

Expand Down

0 comments on commit ccdee35

Please sign in to comment.