use super::track::ValueType;
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ParsedPath {
pub directory_name: String,
pub node_name: Option<String>,
pub object_name: Option<String>,
pub object_index: Option<String>,
pub property_name: String,
pub property_index: Option<String>,
}
fn reserved(c: char) -> bool {
matches!(c, '[' | ']' | '.' | ':' | '/')
}
fn word(c: char) -> bool {
!reserved(c)
}
fn word_or_dot(c: char) -> bool {
!matches!(c, '[' | ']' | ':' | '/')
}
const SUPPORTED_OBJECT_NAMES: [&str; 4] = ["material", "materials", "bones", "map"];
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError(pub String);
impl std::fmt::Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "PropertyBinding: Cannot parse trackName: {}", self.0)
}
}
impl std::error::Error for ParseError {}
pub fn sanitize_node_name(name: &str) -> String {
name.chars()
.filter_map(|c| {
if c.is_whitespace() {
Some('_')
} else if reserved(c) {
None
} else {
Some(c)
}
})
.collect()
}
type Prop = (String, Option<String>);
fn match_property(r: &[char]) -> Option<Prop> {
if r.first() != Some(&'.') {
return None;
}
let wmax = r[1..].iter().take_while(|&&c| word(c)).count();
for wl in (1..=wmax).rev() {
let name: String = r[1..1 + wl].iter().collect();
let after = &r[1 + wl..];
if after.len() >= 3 && after[0] == '[' && after[after.len() - 1] == ']' {
let inner: String = after[1..after.len() - 1].iter().collect();
if !inner.contains(['\n', '\r', '\u{2028}', '\u{2029}']) {
return Some((name, Some(inner)));
}
}
if after.is_empty() {
return Some((name, None));
}
}
None
}
type Tail = (Option<String>, Option<String>, Prop);
fn match_tail(r: &[char]) -> Option<Tail> {
if r.first() == Some(&'.') {
let wmax = r[1..].iter().take_while(|&&c| word(c)).count();
for wl in (1..=wmax).rev() {
let obj: String = r[1..1 + wl].iter().collect();
let after = &r[1 + wl..];
if after.first() == Some(&'[') {
let mut close = after.len();
while close > 2 {
close -= 1;
if after[close] != ']' {
continue;
}
let inner = &after[1..close];
if inner.is_empty()
|| inner
.iter()
.any(|c| matches!(c, '\n' | '\r' | '\u{2028}' | '\u{2029}'))
{
continue;
}
if let Some(p) = match_property(&after[close + 1..]) {
return Some((Some(obj), Some(inner.iter().collect()), p));
}
}
}
if let Some(p) = match_property(after) {
return Some((Some(obj), None, p));
}
}
}
match_property(r).map(|p| (None, None, p))
}
pub fn parse_track_name(track_name: &str) -> Result<ParsedPath, ParseError> {
let chars: Vec<char> = track_name.chars().collect();
let err = || ParseError(track_name.to_string());
let dir_end = chars
.iter()
.rposition(|&c| c == '/' || c == ':')
.map(|i| i + 1)
.unwrap_or(0);
let dir = &chars[..dir_end];
if dir_end > 0 {
let mut seg = 0usize;
for &c in dir {
if c == '/' || c == ':' {
if seg == 0 {
return Err(err());
}
seg = 0;
} else if word(c) {
seg += 1;
} else {
return Err(err());
}
}
}
let rest = &chars[dir_end..];
let node_max = rest.iter().take_while(|&&c| word_or_dot(c)).count();
let mut found: Option<(Option<String>, Tail)> = None;
for l in (1..=node_max).rev() {
if let Some(t) = match_tail(&rest[l..]) {
found = Some((Some(rest[..l].iter().collect()), t));
break;
}
}
if found.is_none()
&& let Some(t) = match_tail(rest)
{
found = Some((None, t));
}
let (node_name, (object_name, object_index, (property_name, property_index))) =
found.ok_or_else(err)?;
let mut results = ParsedPath {
directory_name: dir.iter().collect(),
node_name,
object_name,
object_index,
property_name,
property_index,
};
if let Some(node) = results.node_name.clone()
&& let Some(last_dot) = node.rfind('.')
{
let object_name = &node[last_dot + 1..];
if SUPPORTED_OBJECT_NAMES.contains(&object_name) {
results.object_name = Some(object_name.to_string());
results.node_name = Some(node[..last_dot].to_string());
}
}
if results.property_name.is_empty() {
return Err(ParseError(format!(
"can not parse propertyName from trackName: {track_name}"
)));
}
Ok(results)
}
pub trait HostKind {
type Node: Clone + PartialEq + std::fmt::Debug;
type Target: std::fmt::Debug;
}
pub trait AnimationHost<K: HostKind> {
fn node_uuid(&self, node: &K::Node) -> String;
fn find_node(&mut self, root: &K::Node, node_name: Option<&str>) -> Option<K::Node>;
fn bind(
&mut self,
node: &K::Node,
parsed: &ParsedPath,
path: &str,
value_type: ValueType,
value_size: usize,
) -> Option<K::Target>;
fn get_value(&mut self, target: &K::Target, out: &mut [f64]);
fn set_value(&mut self, target: &K::Target, values: &[f64]);
fn get_strings(&mut self, _target: &K::Target, _out: &mut [Option<String>]) {}
fn set_strings(&mut self, _target: &K::Target, _values: &[Option<String>]) {}
fn release(&mut self, _target: K::Target) {}
}
pub trait NodeTree {
type Id: Clone + PartialEq;
fn name_is(&self, node: &Self::Id, name: &str) -> bool;
fn uuid_is(&self, node: &Self::Id, uuid: &str) -> bool;
fn skeleton_bones(&self, node: &Self::Id) -> Option<Vec<Self::Id>>;
fn children(&self, node: &Self::Id) -> Vec<Self::Id>;
}
pub fn find_node<T: NodeTree>(tree: &T, root: &T::Id, node_name: Option<&str>) -> Option<T::Id> {
let name = match node_name {
None | Some("") | Some(".") => return Some(root.clone()),
Some(n) => n,
};
if tree.name_is(root, name) || tree.uuid_is(root, name) {
return Some(root.clone());
}
if let Some(bones) = tree.skeleton_bones(root)
&& let Some(b) = bones.into_iter().find(|b| tree.name_is(b, name))
{
return Some(b);
}
fn search<T: NodeTree>(tree: &T, children: Vec<T::Id>, name: &str) -> Option<T::Id> {
for child in children {
if tree.name_is(&child, name) || tree.uuid_is(&child, name) {
return Some(child);
}
if let Some(r) = search(tree, tree.children(&child), name) {
return Some(r);
}
}
None
}
search(tree, tree.children(root), name)
}
#[derive(Debug)]
enum BindState<T> {
Unbound,
Unavailable,
Bound(T),
}
#[derive(Debug)]
pub struct PropertyBinding<K: HostKind> {
pub path: String,
pub parsed_path: ParsedPath,
pub root_node: K::Node,
node: Option<K::Node>,
value_type: ValueType,
value_size: usize,
state: BindState<K::Target>,
}
impl<K: HostKind> PropertyBinding<K> {
pub fn new<H: AnimationHost<K>>(
host: &mut H,
root_node: K::Node,
path: &str,
parsed_path: Option<ParsedPath>,
value_type: ValueType,
value_size: usize,
) -> Result<Self, ParseError> {
let parsed_path = match parsed_path {
Some(p) => p,
None => parse_track_name(path)?,
};
let node = host.find_node(&root_node, parsed_path.node_name.as_deref());
Ok(Self {
path: path.to_string(),
parsed_path,
root_node,
node,
value_type,
value_size,
state: BindState::Unbound,
})
}
pub fn node(&self) -> Option<&K::Node> {
self.node.as_ref()
}
pub fn target(&self) -> Option<&K::Target> {
match &self.state {
BindState::Bound(t) => Some(t),
_ => None,
}
}
pub fn is_bound(&self) -> bool {
!matches!(self.state, BindState::Unbound)
}
pub fn bind<H: AnimationHost<K>>(&mut self, host: &mut H) {
if self.node.is_none() {
self.node = host.find_node(&self.root_node, self.parsed_path.node_name.as_deref());
}
if let BindState::Bound(t) = std::mem::replace(&mut self.state, BindState::Unavailable) {
host.release(t);
}
let Some(node) = self.node.clone() else {
log::warn!(
"THREE.PropertyBinding: No target node found for track: {}.",
self.path
);
return;
};
if let Some(t) = host.bind(
&node,
&self.parsed_path,
&self.path,
self.value_type,
self.value_size,
) {
self.state = BindState::Bound(t);
}
}
pub fn unbind<H: AnimationHost<K>>(&mut self, host: &mut H) {
self.node = None;
if let BindState::Bound(t) = std::mem::replace(&mut self.state, BindState::Unbound) {
host.release(t);
}
}
pub fn get_value<H: AnimationHost<K>>(&mut self, host: &mut H, out: &mut [f64]) {
if let BindState::Unbound = self.state {
self.bind(host);
}
if let BindState::Bound(t) = &self.state {
host.get_value(t, out);
}
}
pub fn set_value<H: AnimationHost<K>>(&mut self, host: &mut H, values: &[f64]) {
if let BindState::Unbound = self.state {
self.bind(host);
}
if let BindState::Bound(t) = &self.state {
host.set_value(t, values);
}
}
pub fn get_strings<H: AnimationHost<K>>(&mut self, host: &mut H, out: &mut [Option<String>]) {
if let BindState::Unbound = self.state {
self.bind(host);
}
if let BindState::Bound(t) = &self.state {
host.get_strings(t, out);
}
}
pub fn set_strings<H: AnimationHost<K>>(&mut self, host: &mut H, values: &[Option<String>]) {
if let BindState::Unbound = self.state {
self.bind(host);
}
if let BindState::Bound(t) = &self.state {
host.set_strings(t, values);
}
}
pub fn release<H: AnimationHost<K>>(&mut self, host: &mut H) {
if let BindState::Bound(t) = std::mem::replace(&mut self.state, BindState::Unbound) {
host.release(t);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn p(s: &str) -> ParsedPath {
parse_track_name(s).unwrap()
}
#[test]
fn parses_three_examples() {
let a = p("Box.position");
assert_eq!(a.node_name.as_deref(), Some("Box"));
assert_eq!(a.property_name, "position");
let a = p(".morphTargetInfluences[smile]");
assert_eq!(a.node_name, None);
assert_eq!(a.property_name, "morphTargetInfluences");
assert_eq!(a.property_index.as_deref(), Some("smile"));
let a = p("Box.material.color");
assert_eq!(a.node_name.as_deref(), Some("Box"));
assert_eq!(a.object_name.as_deref(), Some("material"));
assert_eq!(a.property_name, "color");
let a = p(".bones[hip].position");
assert_eq!(a.object_name.as_deref(), Some("bones"));
assert_eq!(a.object_index.as_deref(), Some("hip"));
assert_eq!(a.property_name, "position");
let a = p("mesh.materials[1].opacity");
assert_eq!(a.node_name.as_deref(), Some("mesh"));
assert_eq!(a.object_name.as_deref(), Some("materials"));
assert_eq!(a.object_index.as_deref(), Some("1"));
let a = p("dir/sub:Arm.L.quaternion");
assert_eq!(a.directory_name, "dir/sub:");
assert_eq!(a.node_name.as_deref(), Some("Arm.L"));
assert_eq!(a.property_name, "quaternion");
let a = p("uuid-1234.scale[0]");
assert_eq!(a.node_name.as_deref(), Some("uuid-1234"));
assert_eq!(a.property_index.as_deref(), Some("0"));
assert!(parse_track_name("position").is_err());
assert!(parse_track_name("").is_err());
}
#[test]
fn sanitizes() {
assert_eq!(sanitize_node_name("a b.c[d]:e/f"), "a_bcdef");
}
}