Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NameLookup Type to make looking up entities by name easier #5976

Closed
wants to merge 3 commits into from

Conversation

the10thWiz
Copy link

@the10thWiz the10thWiz commented Sep 13, 2022

Objective

Looking up an entity by name is somewhat difficult, and only done within bevy_animation. The actual code is difficult to read, and outside crates also have a need to lookup entities by name. To facilitate an easier interface, I've created a NameLookup type (which implements SystemParam), and provided a few methods for looking up entities.

Motivation

I'm currently working on a Inverse Kinematics solver for Bevy, which requires the ability to lookup bones in an armature by name (in the same way an animation does).

Solution

I've provided an implementation of a NameLookup type, which implements SystemParam.

The interface is roughly:

impl NameLookup {
    pub fn lookup_any(&self, path: &EntityPath) -> Result<Entity>;
    pub fn lookup(&self, root: Entity, path: &EntityPath) -> Result<Entity>;
}

This also avoids the need to use loop labels (sort of a goto), be refactoring into multiple methods.

There is a note in the current code that lookup by EntityPath can be optimized for better performance. By refactoring it into an independent method, we isolate the lookup itself, and allow benchmarking & testing of the lookup code independent of the rest of the animation logic. Because this is also a public type, external crates can also benefit from performance improvements. Finally, this is also a unified place to implement caching (if possible), to avoid repeated lookups.

@the10thWiz
Copy link
Author

I'm not sure if animations is the right place to put this, since it might belong in core or ecs (given that is could be used more generally, even if animations aren't enabled).

@alice-i-cecile
Copy link
Member

Yep, I'd put this in core :)

@mockersf
Copy link
Member

I'm not sure if animations is the right place to put this, since it might belong in core or ecs (given that is could be used more generally, even if animations aren't enabled).

In or next to https://github.com/bevyengine/bevy/blob/main/crates/bevy_core/src/name.rs please!

I would also like a few tests on those functions.

@mockersf mockersf added A-Core Common functionality for all bevy apps C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Sep 13, 2022
crates/bevy_animation/src/lib.rs Outdated Show resolved Hide resolved
crates/bevy_animation/src/lib.rs Outdated Show resolved Hide resolved
@the10thWiz
Copy link
Author

After some analysis, I think it might be possible to remove EntityPath altogether. My initial assumption was that the EntityPath matched the gltf format, but after looking into the format, it doesn't appear to be the case. Rather, by reordering some code within the GLTF file loader, it's actually possible for AnimationClip to store the actual Entity id, rather than the path. This also provides support for animating nodes without names (since we don't need them). This also removes the need for external crates to lookup by path, since AnimationClip would provide the actual id, rather than the path.

@alice-i-cecile
Copy link
Member

Rather, by reordering some code within the GLTF file loader, it's actually possible for AnimationClip to store the actual Entity id, rather than the path. This also provides support for animating nodes without names (since we don't need them). This also removes the need for external crates to lookup by path, since AnimationClip would provide the actual id, rather than the path.

Fantastic, I like the sounds of that direction. Can you make sure to update the PR description and title to match the actual work done in the PR when you're ready?

@the10thWiz the10thWiz changed the title Add type to conviently lookup entities by name Remove EntityPath and use Entity as a direct id Sep 14, 2022
@mockersf
Copy link
Member

it's actually possible for AnimationClip to store the actual Entity id, rather than the path

That's actually not possible because an AnimationClip can be applied to several entity hierarchies, which is why we are using path with names.

The scenario you're describing would only work for an animation on only one scene, but would break if one was to spawn the same scene multiple time and reuse the same animation. Also, it's often the case to distribute animations and models in different gltf files, in which case there are no knowledge shared of entities.

@the10thWiz
Copy link
Author

the10thWiz commented Sep 14, 2022

it's actually possible for AnimationClip to store the actual Entity id, rather than the path

That's actually not possible because an AnimationClip can be applied to several entity hierarchies, which is why we are using path with names.

The scenario you're describing would only work for an animation on only one scene, but would break if one was to spawn the same scene multiple time and reuse the same animation. Also, it's often the case to distribute animations and models in different gltf files, in which case there are no knowledge shared of entities.

I'm not sure how to distribute animations and models between multiple gltf files, especially since the gltf format uses indicies internally. I guess maybe you could duplicate the bones into a separate files, but I'm not sure how common this actually is.

I'm not a fan of the current name based approach, and I don't think applying the same animation to several entity hierarchies is necessarily worth while. That being said, it is something I'm going to have think about.

Spawning the same scene multiple times is a major issue though. I see things like SkinnedMesh work because they are components (so they can update to point at the new entities). It might be interesting to move the AnimationPlayer to be on the SceneInstance, which holds the internal references within the scene. This is going to require some more thought. This might also protect against applying an animation clip to the wrong player, which currently just results in a console warning.

Overall, I don't like the name approach, and I would like to come up with a better option. I'm going to mark this as a draft for now, but I'd like to tackle this.

@the10thWiz the10thWiz marked this pull request as draft September 14, 2022 20:04
@mockersf
Copy link
Member

You should look at bevyengine/rfcs#49 (and bevyengine/rfcs#51 while you're there)

@james7132 james7132 self-requested a review September 14, 2022 21:54
@the10thWiz the10thWiz changed the title Remove EntityPath and use Entity as a direct id Add NameLookup Type to make looking up entities by name easier Sep 20, 2022
@the10thWiz
Copy link
Author

After careful reflection, I've chosen to revert back to my original goal with this PR, and open a new PR when and if I have a more complete solution. For now, I'm looking at contributing to the discussion around the RFCs listed above.

@mockersf
Copy link
Member

CI seems to be lost with your force pushes. Could you push a new commit to trigger it?
And put the PR out of draft if you're happy enough with its state 🙂

@the10thWiz
Copy link
Author

CI seems to be lost with your force pushes. Could you push a new commit to trigger it? And put the PR out of draft if you're happy enough with its state 🙂

At this point, I'm still looking into writing tests, so it's not quite ready. I think the actual issue is that my branch is out of date, and CI is does a merge with master before running tests.

@james7132
Copy link
Member

@the10thWiz do you have the time to drive this PR to completion? This would be super useful in simplifying our animation code, and I'd like to see it completed. If not, we can mark this PR up for adoption, your commit history and credit to you will be retained.

@alice-i-cecile alice-i-cecile added S-Adopt-Me The original PR author has no intent to complete this work. Pick me up! A-Animation Make things move and change over time labels Jan 30, 2024
@james7132 james7132 removed the A-Animation Make things move and change over time label Mar 29, 2024
@james7132
Copy link
Member

Removing the Animation label since animation is no longer reliant on name lookups after #11707.

@bas-ie
Copy link
Contributor

bas-ie commented Oct 5, 2024

Backlog cleanup: closing due to inactivity, and in favour of #11842 which itself is a little stalled right now. If Discord wills it, I'll create a tracking issue.

@bas-ie bas-ie closed this Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Core Common functionality for all bevy apps C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Adopt-Me The original PR author has no intent to complete this work. Pick me up!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants