Threading Event Loop Poor Performance (Midi Controller to Nuke API)

47 views Asked by At

I want to connect my MIDI Controller to Foundry´s Nuke. In the past, I did a similar thing with Blender and it works fine. Unfortunately, unlike Blender, Nuke does not provide its event loop. (Or i didn´t find it in the Python API Doc...) So I wrote one myself, using threading for the first time. And it works! Sadly, it´s a little slow, more on that later. I want to make it faster, maybe you can spot the problem? Here is my current code:

import mido
import threading
import time

avaliableDevices = mido.get_input_names()


#create some knobs in nuke, just for testing purposes
sNode = nuke.nodes.NoOp(name="MIDIMIX")

for x in range(8):
    xS = str(x+1)
    k = nuke.Double_Knob("F"+xS, "FADER, Channel "+xS)  #Name it F1, F2, ... and Label it
    sNode.addKnob(k)


#Connecting to my MIDI Controller
inport = mido.open_input(avaliableDevices[1])

#Mapping MidiCC messages to my created knobs
knobMap = {19: 'F1', 23: 'F2', 27: 'F3', 31: 'F4', 49: 'F5', 53: 'F6', 57: 'F7', 61: 'F8'}


def setNewValue(*args):
    sNode[knobMap[args[0]]].setValue(args[1]*0.01)


#My Loop
def loopRead():
    while True:
        msg = inport.receive()       #Wait for the next received MIDI message
        #filtering for CC messages
        if msg.is_cc():
            nuke.executeInMainThreadWithResult(setNewValue, (msg.control, msg.value))
            time.sleep(.0001)
        time.sleep(.0001)

t1 = threading.Thread(target=loopRead, daemon=True)

t1.start()

Everything is working at this point, but as stated it´s too slow, especially when moving multiple sliders at the same time (which kind of is the whole point of using a controller...).

Setting time.sleep() to a lower value causes really high CPU usage or crashes, setting it to a higher value increases the latency even more (obviously).

One thing I notice is, that my knobs seam to never skip a value on change. So if I move a (7 bit) slider on my MIDI Controller very fast it doesn´t go 0-35-79-127, but 0-1-2-3-4-...-127. Maybe this is my Problem? How would I fix this?

Or is it something else..

Every hint in the right direction very appreciated! Greetings, Julius

Nuke API Doc MIDO API Doc

0

There are 0 answers