• Projects
  • 3Dconnexion Space Mouse Support in Godot

I've been looking into this project for @Kojack , to support the 3Dconnexion 3D mice, like the SpaceMouse and SpaceNavigator in Godot. For those that don't know, this is a mouse that supports 6DOF (rotation and translation) and makes it very easy to move the camera and position objects in 3D space.

I think I figured it out, but it's a little harder than I initially thought. It might take me about a week to get working. The basic idea is that you make an EditorPlugin. See here:

https://docs.godotengine.org/en/3.4/tutorials/plugins/editor/making_main_screen_plugins.html

You'll also need a GDNative plugin to access the native shared libraries for the mouse:

https://docs.godotengine.org/en/stable/tutorials/scripting/gdnative/index.html

Then you can get access to the editor interface windows with EditorInterface:

https://docs.godotengine.org/en/stable/classes/class_editorinterface.html

From there you would use get_editor_viewport() to get the viewport, and then search through that to find the camera.

I still need at least a couple days to get a prototype together, but I have some of the pieces working on their own with command line apps. So maybe like a week to get it working. Will be fun to try this with my stereo 3D plug-in.

Here is the code that finds the camera given a viewport:

func find_camera(node) :
	var children = node.get_children()
	for child in children:
		if child is Camera:
			return child
	for child in children:
		return find_camera(child)

Nice.

I just went to find the camera code I was testing, and the gd script file is missing from my project. I think it was that damn fast boot thing that screwed things up recently after I reinstalled windows. Of course it didn't help that I was testing the camera stuff in Godot 4, before the alpha came out (I got the repo and built it myself). The docs were a bit out of date. :) The camera code I was using was from here: https://github.com/me2beats/godot-tips-and-tricks/issues/15 Your one is a lot simpler. :)

I really need to buy a new Connexion. Not just because the buttons are a bit jammed on my Space Pilot Pro, but also because their newer devices have a different protocol. I use HID to access the devices, but I can't test my reverse engineering without one of the newer ones. Maybe a Space Mouse Enterprise, although I never used the LCD screen of my Space Pilot Pro.

I've got the Space Navigator (wired) and Space Pilot Pro. Started with the Navigator, now I keep it at work and use the bigger one at home.

After using one with the palm rest, I don't like going back to the smaller one. Not because I actually need a palm rest, but because that holds it down so hard upwards movement doesn't lift the thing off the desk.

Yeah, that makes sense. I have to touch it lightly because it moves too much. But usually it's not too bad.

4 months later

So I resumed this project. It's sort of working now, @Kojack . I used the libspnav, so it only works on Linux, but I kind of don't feel like supporting the proprietary SDK. There may be a way to compile for Windows/OSX but I'll have to look into that later. I'm getting all the values for position and rotation in Godot. Right now I am just moving a cube, but I will hook it up to the camera tomorrow. The results are kind of wonky since I'm using Euler, I think I have to convert to quaternion, but that shouldn't be hard.

cybereality Hey, yep notifications work. I was asleep until 30min ago. 🙂

Nice.
Yeah, I don't use their proprietary sdk either. I use direct HID access using HIDAPI from http://github.com/signal11/hidapi
It's windows, linux, bsd and osx. The connexion protocol is really simple. The great thing about HID level is it doesn't need drivers installed, so I can plug my space navigator or pilot pro into any PC and use it (in my own apps) without admin permissions to install the official drivers. That's handy at work where classroom computers don't let me admin.

The annoying thing is they've changed the protocol for newer devices and I can't test they work. I want to buy a new one, but they don't sell to australia directly and resellers add over $100. Plus their educational discount system is "Page not found" for me. Oh well.

Since you generously gifted me one, should you need someone to help with testing, just let me know.

I see right. I'm using this SDK, but now that I look at it, I'm not sure if it can compile for anything but Linux.

https://github.com/FreeSpacenav/libspnav

However, I will finish what I have because it's like 75% there and it if seems good I can look into how to support other platforms. I have the older and newer space nav devices, so figuring out the HID protocol shouldn't be too difficult. I'll look into that later.

The old protocol is basically:

#pragma pack(push,1)
struct Packet
{
	unsigned char type;
	union
	{
		struct
		{
			short x;
			short z;
			short y;
		};
		unsigned char bytes[6];
	};
};
#pragma pack(pop)

If type is 1, the shorts are position offsets. If type is 2, they are euler rotations. If type is 3, the x and y are bitfields for buttons (32 bit).

Thanks @Megalomaniak . I should be able to figure it out, I have a bunch of computers for testing, though that will probably be a good idea once I am ready to release something.

Oh yeah, that is dirt simple. Okay. I will probably go with that, because I don't think I could release it into the wild only supporting Linux. Though let me work with what I have for now and finish the rest of it and see if it is even worthwhile.

Hmm, the Connexion forums say that their wireless devices are harder to use through HID. That's annoying. I have no use for a wireless connexion, I wasn't planning on getting one (the Enterprise model has no wireless option anyway).

I also see that the official Linux sdk hasn't been updated in 17 years, so it may be a little out of date. Probably a good idea to go with libspnav instead. 🙂

    So I made some progress. I can now move the viewport camera around. The axis are all messed up, so I'll need to do some math, but both translation and rotation are getting correct values from the device.

    My issue now is that after moving with the spacenav, if you use the mouse/keyboard to move it snaps back to the previous position (before you moved with the spacenav). I guess the editor has it's own copy of the transform it uses, so I may have to hook into that rather than modifying the camera directly. Hopefully that is possible with a plugin and won't need source access.

    I also had to rewrite the function to find the camera. That code works for a game, but not in the editor. In the editor you have to find the Viewports and then get the Camera from that. Here is the new function.

    func find_camera(node, list) :
    	if node is Viewport:
    		var camera = node.get_camera()
    		list.append(camera)
    		return list
    	for child in node.get_children():
    		find_camera(child, list)
    	return list

    And you can call it like this:

    var editor = get_editor_interface()
    var viewport = editor.get_editor_viewport()
    var results = find_camera(viewport, [])
    var camera = results[1]

    The number 1 is needed because I have 1 user camera in my scene (results[0], the first editor camera is results[1]). If I had more, that would be a different number. It would also be a higher number if you used the split view for multiple view angles in the editor. I'll have to figure out a way to determine where you are looking rather than hard-coding the index.

    Kojack I also see that the official Linux sdk hasn't been updated in 17 years, so it may be a little out of date. Probably a good idea to go with libspnav instead. 🙂

    Yeah, the fact that they haven't updated the Linux SDK in 2 decades doesn't make me eager to support them. Like it's not even a lot of work, the drivers are fairly simple and they basically abandoned Linux.

    So I kept looking into it, and no, it is not possible without a source code change. The editor camera transform is controlled by a class called Cursor and is updated in the file spatial_editor_plugin.cpp. However, it holds it's own state, it does not read it from the editor viewport camera. Meaning even though I can move the camera, the state is not maintained (as soon as you start altering the camera with the mouse or keyboard it will revert to the previous state). So it's not going to work. Sorry, @Kojack

    Oh well. Thanks for looking into it.

    I guess I'll just hack the Godot source. 🙂

    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.