Razer Hydra to vjoy (dinput) script. To convert it to xbox controller (xinput) afterwards

Official forum for open source FreePIE discussion and development.
Post Reply
VerdeMsk
One Eyed Hopeful
Posts: 2
Joined: Wed Mar 08, 2023 5:11 am

Razer Hydra to vjoy (dinput) script. To convert it to xbox controller (xinput) afterwards

Post by VerdeMsk »

Hello, everyone. This script converts Razer Hydra to VJoy for further conversion to XInput. It's possible someone else has done this before, but for some reason, I haven't found anything. This is the second time I'm posting a script on this forum. This time, the script was entirely created by ChatGPT :shock: . I just provided correct input and output names, and when the initial versions didn’t work, I found an example script for other devices on YouTube. ChatGPT even managed to create several switches I asked for. It's an amazing neural network. It took just over a day to work on it, considering I can hardly code myself. I'll call this version 0.1 since there's much more I'd like to add. For instance, currently, there's only support for motion from one of the selected controllers. Plus, I assigned the vJoy axes randomly, which I don't think is right. For everything to work properly in the 'Configure vJoy' program, enable all axes in vJoy device 1. Set 14 for the number of buttons. Afterward, you can map the Razer Hydra, working through Freepie and vJoy, in Steam or, for example, in x360ce. This way, it will work in almost all modern games that support XInput. It's much more convenient than mapping it to the keyboard in Motion Creator 2, as the developers initially intended. By the way, you can still use mouse aiming in Motion Creator 2. My script doesn't interfere with that, but all sticks and triggers operate in analog mode. Plus you can switch motion inputs to left Hydra.

Code: Select all

# Script created by VerdeMsk using ChatGPT. Version 0.1

if starting:
    # Settings
    # Define the switch: 1 for hydra[0] - left, 2 for hydra[1] - right
    hydra_switch = 2  # Change this value to 1 or 2 to switch between Hydra - left and Hydra - right

   # Define the switch: 1 for hydra[0].yaw, 2 for hydra[0].roll
    translation_switch = 1  # Change this value to 1 or 2 to switch between yaw and roll

    v = vJoy[0]
    v.x, v.y, v.z, v.rx, v.ry, v.rz, v.slider, v.dial = (0,) * 8  # Reset vJoy axes to 0 initially
    
    global yaw_sensitivity, roll_sensitivity, pitch_sensitivity, left_stick_sensitivity
    global left_trigger_sensitivity, right_trigger_sensitivity

    yaw_sensitivity = 1  # Adjust the sensitivity for yaw as needed
    roll_sensitivity = 1  # Adjust the sensitivity for the roll axis as needed
    pitch_sensitivity = 1  # Adjust the sensitivity for pitch as needed
    left_stick_sensitivity = 1  # Adjust the sensitivity for the left stick as needed
    left_trigger_sensitivity = 1  # Adjust the sensitivity for left trigger as needed
    right_trigger_sensitivity = 1  # Adjust the sensitivity for right trigger as needed

    # Define vJoy X, Y, Z, Rx, Ry, Rz, Slider, and Dial axis maximum and minimum range
    vjoy_x_max = 16383
    vjoy_x_min = -16383
    vjoy_y_max = 16383
    vjoy_y_min = -16383
    vjoy_z_max = 16383
    vjoy_z_min = -16383
    vjoy_rx_max = 16383
    vjoy_rx_min = -16383
    vjoy_ry_max = 16383
    vjoy_ry_min = -16383
    vjoy_rz_max = 16383
    vjoy_rz_min = -16383
    vjoy_slider_max = 16383
    vjoy_slider_min = -16383
    vjoy_dial_max = 16383
    vjoy_dial_min = -16383

    # Calculate the ratio to map Hydra yaw, pitch, left stick, triggers, and second stick to vJoy axes range
    yaw_to_vjoy_x_ratio = vjoy_x_max / 1.0  # Maximum Hydra yaw (adjust this value if needed)
    roll_to_vjoy_x_ratio = vjoy_x_max / 1.0  # Maximum Hydra roll (adjust this value if needed)
    pitch_to_vjoy_y_ratio = vjoy_y_max / 1.0  # Maximum Hydra pitch (adjust this value if needed)
    left_stick_to_vjoy_rx_ratio = vjoy_rx_max / 1.0  # Maximum Hydra left stick axis (adjust this value if needed)
    left_stick_to_vjoy_ry_ratio = vjoy_ry_max / 1.0  # Maximum Hydra left stick axis (adjust this value if needed)
    trigger_to_vjoy_z_ratio = vjoy_z_max / 1.0  # Maximum Hydra trigger (adjust this value if needed)
    trigger_to_vjoy_rz_ratio = vjoy_rz_max / 1.0  # Maximum Hydra trigger (adjust this value if needed)
    joy_to_vjoy_slider_ratio = vjoy_slider_max / 1.0  # Maximum Hydra joyx (adjust this value if needed)
    joy_to_vjoy_dial_ratio = vjoy_dial_max / 1.0  # Maximum Hydra joyy (adjust this value if needed)

if hydra[0].yaw or hydra[0].roll or hydra[0].pitch or hydra[0].joyx or hydra[0].joyy or hydra[0].trigger or hydra[1].trigger or hydra[1].joyx or hydra[1].joyy:
    # Update Hydra values using Razer Hydra controller data
    if hydra_switch == 1:
        yaw = hydra[0].yaw * yaw_sensitivity
        roll = hydra[0].roll * roll_sensitivity
        pitch = hydra[0].pitch * pitch_sensitivity
        
    elif hydra_switch == 2:
        yaw = hydra[1].yaw * yaw_sensitivity
        roll = hydra[1].roll * roll_sensitivity
        pitch = hydra[1].pitch * pitch_sensitivity
        
    # Use the switch to determine what to map to vJoy x-axis
    if translation_switch == 1:
        v.x = max(vjoy_x_min, min(vjoy_x_max, int(round(yaw * yaw_to_vjoy_x_ratio))))
    elif translation_switch == 2:
        v.x = max(vjoy_x_min, min(vjoy_x_max, int(round(roll * roll_to_vjoy_x_ratio))))

    left_stick_x = hydra[0].joyx * left_stick_sensitivity
    left_stick_y = hydra[0].joyy * left_stick_sensitivity
    left_trigger = hydra[0].trigger * left_trigger_sensitivity
    right_trigger = hydra[1].trigger * right_trigger_sensitivity
    joy_x = hydra[1].joyx * left_stick_sensitivity
    joy_y = hydra[1].joyy * left_stick_sensitivity

    # Map Hydra pitch to vJoy Y-axis range centered around 0
    v.y = max(vjoy_y_min, min(vjoy_y_max, int(round(-pitch * pitch_to_vjoy_y_ratio))))

    # Map Hydra left stick x-axis to vJoy Rx-axis
    v.rx = max(vjoy_rx_min, min(vjoy_rx_max, int(round(left_stick_x * left_stick_to_vjoy_rx_ratio))))

    # Map Hydra left stick y-axis to vJoy Ry-axis
    v.ry = max(vjoy_ry_min, min(vjoy_ry_max, int(round(-left_stick_y * left_stick_to_vjoy_ry_ratio))))

    # Map Hydra left trigger to vJoy Slider axis
    v.z = max(vjoy_z_min, min(vjoy_z_max, int(round(joy_x * joy_to_vjoy_slider_ratio))))

    # Map Hydra right trigger to vJoy Rz-axis
    v.rz = max(vjoy_rz_min, min(vjoy_rz_max, int(round(-joy_y * joy_to_vjoy_dial_ratio))))

    # Map Hydra joyx to vJoy Z-axis
    v.slider = max(vjoy_slider_min, min(vjoy_slider_max, int(round(left_trigger * trigger_to_vjoy_z_ratio))))

    # Map Hydra joyy to vJoy Dial axis
    v.dial = max(vjoy_dial_min, min(vjoy_dial_max, int(round(right_trigger * trigger_to_vjoy_rz_ratio))))

#Buttons
vJoy[0].setButton(0,int(hydra[0].one))
vJoy[0].setButton(1,int(hydra[0].two))
vJoy[0].setButton(2,int(hydra[0].three))
vJoy[0].setButton(3,int(hydra[0].four))
vJoy[0].setButton(4,int(hydra[1].one))
vJoy[0].setButton(5,int(hydra[1].two))
vJoy[0].setButton(6,int(hydra[1].three))
vJoy[0].setButton(7,int(hydra[1].four))
vJoy[0].setButton(8,int(hydra[0].start))
vJoy[0].setButton(9,int(hydra[1].start))
vJoy[0].setButton(10,int(hydra[0].bumper))
vJoy[0].setButton(11,int(hydra[1].bumper))
vJoy[0].setButton(12,int(hydra[0].joybutton))
vJoy[0].setButton(13,int(hydra[1].joybutton))
	
diagnostics.watch(hydra[0].yaw)
#diagnostics.watch(hydra[0].roll)
#diagnostics.watch(hydra[0].pitch)
diagnostics.watch(hydra[1].yaw)
#diagnostics.watch(hydra[1].roll)
#diagnostics.watch(hydra[1].pitch)
#diagnostics.watch(vJoy[0].x)
#diagnostics.watch(v.x)
#diagnostics.watch(hydra[0].trigger)
#diagnostics.watch(hydra[1].trigger)
#diagnostics.watch(vJoy[0].z)
#diagnostics.watch(v.z)
Post Reply

Return to “FreePIE”