• Projects
  • 3Dconnexion Space Mouse Support in Godot

I'm guessing you were in version 3.4? In 4.0 there's no spatial_editor_plugin.cpp file, but there's a node_3d_editor_plugin.cpp which contains mouse and keyboard controls.
I found that back when I first started looking into this, I could probably easily add hard coded support for my own input library, but it won't help anyone else (and especially not non-windows users). But fine for myself.

Well, if I can get scons to let me. Can't say I'm a fan.

Yes, I'm on 3.4.4. But 4.0 is similar in the file your found (Spatial was renamed to Node3D in Godot 4.0, but works almost the same). I know I could get it working with a recompile, but I don't wish to do that as it will only work for me. Might still be worth trying just to see how it feels, but the idea was to release it as a plugin to help other people.

That said, I still have full control over user created Nodes. Meaning the Space Navigator could be used to position/orient objects, or move user cameras. This is less common, but still could be useful. I already tried it, I can get the selected object like this:

var editor = get_editor_interface()
var selection = editor.get_selection()

And then do whatever I want. However, I really wanted to use it to fly around the scene.

One other option is to spoof input, which would get passed to the editor. I know I can trigger fake mouse/keyboard events, but this will greatly limit the kind of 6DOF controls I want. However, it could be a compromise.

Okay, so I decided I have to finish this. I started editing the Godot source code, it's actually not that complicated (I guess those 15 years of learning C++ finally came in handy). I have the position pretty close to working. Right now it is in global space, so I have to convert it into the camera space, but I solved the issue of the mouse/keyboard interference. The rotation should not be difficult either. It's getting late now, but I think I can have it all working tomorrow.

It will take longer to get the space navigator to work cross-platform, though I can submit my commit and hope it gets merged in the meantime. My changes have nothing to do with the space navigator, it just allows altering the camera position/orientation. This could also be useful for AR/VR or for accessibility. For example, people could develop plugins that allow neck or mouth controllers to move the camera. So I think there is a good chance they will take my merge.

    cybereality
    Cool
    Another potential use for control of the editor view is adding camera view shortcuts. Like place the camera looking at a part of your scene then bind that transform to a key. There's already the 6 axis view shortcuts, but no free view recording that I can see.

    For some reason I can't get release builds of 4.0 to work (comes up with an error about missing .pck), but debug builds work fine (but slower). Probably some setting got messed up when I followed some online instructions to get godot compiling from visual studio (although still routed through scons, annoyingly).

    It's working!!! @Kojack @Megalomaniak

    I gave up the idea of editing the source code. Though I did get it sort of working, that Cursor class is used for too much and it was causing all sorts of crazy bugs. Even if I did finish it, there was the problem of the z axis, so there was always going to be some jump. I decided to just use what was available as a plugin. This means the 6DOF state is not saved, but it doesn't mess up the normal function of the mouse and keyboard. I'll have to add cross-platform support for the extension, but the plugin itself is fully working.

      cybereality It might be handy to have a 5DOF mode. I find roll can get annoying at times in editors. 🙂
      Cool work though!

      I actually find roll helpful since it gives you a difference perspective. This is important in 3D because you are literally missing a dimension. It's sort of like how traditional artists back in the day were told to view their drawings in a mirror, because it shows you things you can't see normally.

      Still, a turntable mode might have it's uses too. And a fly mode. But for initial support these can probably be considered "fluff".

      Then again, if you are using the Connexion sdk, the control panel can already disable roll (at least on windows).

        I guess it is sort of a turntable. It rotates around the selected object right now. You can kind of still fly wherever, since you have full control, but it gets difficult if you move more than around 6 units from the center (of the current object). Fly mode I can look into. I have tried it before on other apps and it never worked well. But it's worth investigating.

        Kojack Then again, if you are using the Connexion sdk, the control panel can already disable roll (at least on windows).

        Yes, I can put in some options. That shouldn't be an issue. I have to do that anyway to control the speed.

          cybereality I guess linux may not have the same thing. But in windows there's already a configuration panel in the drivers for apps that use the sdk which gives axis scales, turning axes on/off, etc.

          No Linux never had that. It was just a service with no GUI, but it hasn't worked in a long time. I looked into the official SDK, but it looks overly complicated and I'm not especially fond of the license (particularly because I plan to release the plug-in open source). While I don't have to open source the DLL, it still makes me nervous. I think I did enough for today, but I'll see about either the HIDAPI or porting libspnav to Windows/macOS. Not sure which would be easier, but I feel like contributing to libspnav may be more worthwhile in the long run.

            cybereality Wait, sorry, I'd already misremembered the earlier part of the thread and thought you were using the linux sdk. Ignore my last post.
            🙂

            I like HID because I can also use it to get access to all features of things like PS5 controllers (touch pad, IMU, etc).

            Yeah, the official sdk is rather excessive. For my own simple Connexion library (not my full multi-device input library) that I use with things like Unity, the entire interface is just:

            struct ConnexionState
            {
            	float pos_x;
            	float pos_y;
            	float pos_z;
            	float rot_x;
            	float rot_y;
            	float rot_z;
            	unsigned int buttons;
            };
            int init();
            void poll();
            unsigned int getDeviceCount();
            unsigned int getDeviceType(unsigned int deviceIndex);
            ConnexionState getState(unsigned int deviceIndex);
            ConnexionState getStatePrevious(unsigned int deviceIndex);
            unsigned int buttonPressed(unsigned int deviceIndex, unsigned int button);
            unsigned int buttonReleased(unsigned int deviceIndex, unsigned int button);
            unsigned int buttonDown(unsigned int deviceIndex, unsigned int button);
            void setLED(unsigned int deviceIndex, unsigned int value);
            void setRotationPower(unsigned int deviceIndex, float p);
            void setTranslationPower(unsigned int deviceIndex, float p);

            This is where I still applaud Microsoft for the XInput API. Unlike almost every other API they've made, XInput is so incredibly simple to use. No setup/init. One function call gives you the state of every part of an xbox controller in a simple struct. One function call lets you set haptics. Shame it doesn't support other brand devices like DirectInput does.

            Side note, I don't understand these SDKs that are so over-engineered. I just want a single header file, like 5 functions, and a simple command-line sample. Not sure why they have to make things so obtuse.

              cybereality Back a long time ago I got a TrackIR (head tracker camera for flight sims).
              I wanted to add support to my college game engine.
              I had to apply to be a developer (they hand picked them). They wanted me to sign an NDA, because the C++ header of their API contained proprietary secrets or some crap. WTF? All I needed was an init function and something to return 6 floats. There's nothing else the device needs to be used.
              They also said not only could I not allow any student to see their header, but also no co-worker at the college could see it. I'd have to make my own binary only API that wrapped their API and keep the source secret.
              I never responded after I read that.

              Later I found out that they were so paranoid about their software that they made the camera (a USB IR webcam) only turn on if you sent a copyrighted haiku to it, so any third party drivers could be sued for copyright infringement. They also extorted companies like Eagle Dynamics (DCS flight sims) to remove support for competing alternative head tracking devices under threat of their games being blacklisted by the TrackIR driver.

              Urgh.

              Still not my strangest contract. I bought a Beagleboard (the spiritual ancestor of the Raspberry Pi that started the small ARM SOC board craze). But due to some encryption code in the firmware, the US treated it as a military device for export and to get it shipped to Australia I had to sign a document from the Department of Energy that said I had no dealings with a list of known terrorist collaborator companies. Also I saw the term "weapons of mass destruction" in the doc at least once. It was a tiny arm board equivalent to maybe an iphone 3.
              I've still got the Beagleboard on my shelf, never really used it for anything (it was going to be a dev kit for the Pandora handheld, but then my Pandora shipped).

              Yeah. Then these companies wonder why they go out of business.

              I got HIDAPI working. It only took a few hours, way easier than I thought. Everything is hard-coded for my device though, so it will take some time to configure for all the various models. I found this code, which has the product and vendor ids, which is a great help.

              https://github.com/johnhw/pyspacenavigator/blob/master/spacenavigator.py

              It also has some of the different mapping formats of the data. However, my device seemed to work differently (even though it is listed there with the correct VID/PID). The axes were not in the right order (meaning not x, y, z) and some were negated. I did get it all working, but I expect that not all devices conform to this format, as you can see in the link above. I have one older wired Space Navigator, so I can try that later. That should give me an idea, but there are like 8 or so devices I would need to support.

              Also, HID has WAY better performance than the libspnav. There is no more choppiness and everything is butter smooth at 144Hz. So that is a nice bonus. And the code should compile on Windows and macOS, but I will have to verify that tomorrow. So this was definitely the right plan.

                cybereality
                Yep, I believe the protocol changed when they went from Space to SpaceMouse. The event 3 (buttons and long press buttons) changed to event 28 (buttons) and 29 (long press buttons).

                Hehe, I clicked that link, firefox showed me I already had it bookmarked. 🙂

                Well, I'm not planning on supporting buttons, just axis control, so that simplifies things. If there are only two protocols for the axis that should be simple. I just don't want to deal with trying to support a bunch of devices I don't own. But I think there is enough open source examples for me to attempt it with what I have.

                Also, I noticed that the movement is not exactly the same. All I did was make a new shared lib, I left the interface the same, so I just dropped it into my plugin and everything basically worked. However it feels very different. I think libspnav was doing some sort of low pass filter or clamping or something. Because it was choppier before, but not noisy. Now it is super smooth, but things seem to move in unintended directions. So I'll have to look into that next, maybe tomorrow. But I have the basic code working, it just a matter of tweaking some numbers.