Best way to test if spatial contains animation control?

I’m looping through each spatial in my scene and checking if they have animation controls. Currently this is the working code I have for the test.

for (Spatial x : sceneNode.descendantMatches(Spatial.class, null)) {
        int i = x.getNumControls();
        for (int n = 0;n<i;n++){
            if (x.getControl(n).getClass().equals(AnimControl.class)){
                System.out.println("animation control");

I’m curious if there is a more efficient way or an equivalent to descendantMatches for animation controls.

Well, this weirdness is unnecessary. Use:

That would simplify your code down to 3-4 lines.

@pspeed My spatials have multiple controls and some may not have any controls. If I call getControl and the spatial doesn’t contain one it crashes the app. Hence why I have that test.


for (Spatial x : sceneNode.descendantMatches(Spatial.class, null)) {
    AnimationControl control = x.getControl(AnimationControl.class);
    if( control != null ) {
        System.out.println("Animation control");

If a spatial has more than one animation control then it’s seriously broken.

I have models that have separate animation controls for each limb. I didn’t rig them they came this way. I’m looping through the spatial for each subset and getting the control.

Then each limb is a different spatial? My code still works in that case.

And even so…

if( x.getControl(n) instanceof AnimationControl ) {

…is at least less silly.

To explain what I’m doing is I’m creating a master control for the spatial. That when you run the animation for one limb it runs it for all limbs. For example when the “walk” animation is called then all limbs on the model play the walk animation. In order to do this I’m creating a List of controls for the model.

Is each limb a separate spatial or not?

Yes , the model bob has a spatial for body , head , sword etc. They all come together to make the bob model.

So then the code I showed you should work perfectly. No reason to loop through all controls when there is already a method to do that for you.

It’s not just the bob model I’m looping through. I’m going through every node in my scene. Most of which do not have animation controls. If I call getControl(AnimControl.class) on a node without an animation control it just crashes with a null exception.


If you ACCESS the control without checking it first then you get an NPE of course. MY CODE DOESN"T DO THAT.

Anyway, good luck with your game as this isn’t fun anymore.

See getControl Java doc

getControl(AnimControl.class) returns null if no animation control is present on the spatial. Then, you just have to test if the returned value is null.

This may crash if the return of getControl is null, obviously :


This will work :

Control c = getControl(AnimControl.class);
if(c != null)

And note that one spatial should have only one control of a single type, even if nothing prevents you adding two controls of the same type.

@methusalah Thank you very much , that was exactly what I was looking for. I don’t have duplicate controls for a mesh. Each mesh is divided into parts for example legs , arms , body etc. have their own geometries and each of those geometries has it’s own control. I succeeded in making my master control , finally :smiley:

So no one else is confused by this thread here is a screen shot of bob.

Note that bob has multiple parts with multiple controls.

Off the top of my head, putting it all together.

public <T extends Control> List<T> getControls( Node start, Class<T> controlType ) {
    List<T> results = new ArrayList<>();
    for (Spatial x : start.descendantMatches(Spatial.class, null)) {
        T control = x.getControl(controlType);
        if( control != null ) {
    return results;

A method to easily get all of any type of control in a hierarchy of spatials.

Careful, it seems that the parameter ‘root’ become ‘sceneNode’ in your code

Fixed. That’s what I get for typing code right from my head. I’m always surprised if it compiles at all, frankly.

Maybe graft a cerebral compiler? :smile:


Maybe graft a cerebral compiler? :smile:

Judgement day and the rise of the machines is coming.

Sorry , for all of those who I frustrated but I know I’m not crazy lol :smiley:

I must say forum wise you guys are still the best most responsive team on the internet. Heck , you even put up with me which proves you are numero uno ! :blush: