Script DAT isn't cooking?

I’m trying to use a Script DAT to receive Bluetooth UART data. I’m stuck on what’s probably a very basic thing — the DAT only updates when I click the ‘setup’ button. How do I make it update on every frame? For a minimal example:

def onCook(scriptOp):
	scriptOp.text = time.time()

The point of the script DAT is to take in some sort of DAT input (table or text) or create new DAT data output and then do something whenever that input or output is caused to cook.

If you are trying to just make some python that cooks every frame, try the Execute DAT which has an option to cook on every frame start or frame end which gives you some more control over that. (Note that “Start” for that parameter means start of the whole program - you want FRAME start or frame end)

Also, how are you getting the UART data in? If it is coming in through the Serial DAT or UDP DAT for example, there are already text callback DATs attached to those by default that will run python whenever data is received from said Serial / UDP devices…

Thanks. It looks like Execute DAT might do the job here. I’m still confused, when is onCook() supposed to run in the Script DAT callback? I thought that would be on every frame.

The UART data comes from a Bluetooth BLE device. I’ve installed the bleak module, and I’m trying to receive the BLE data in a background thread inside the Script DAT.

There is a BIG difference between a cook and a frame AKA onCook() only runs when something causes / requires the ScriptDAT to cook: Cook | Derivative Most DATs don’t (and shouldn’t) cook every frame.

The biggest thing to learn about optimizing in TD is how to have it doing as little cooking as possible and how to tie the cooking of any node chain to the most optimal reason for it to cook. If every node cooked every frame, you would not be able to do nearly as many nodes in a project as you can.

Also, Python in TD is blocking, so if you have a Python thread “running” to try to listen for BLE events, then it will most likely freeze up TD until it is finished running, so you might have to find a different approach - like run an external Python process that then sends data into TD somehow, but that is way over my head, so good luck!

Maybe try asking your favorite coding LLM to help build an external python executable that can do that…

As bleak module supports asyncio I would recommend using that in TD.

Here’s an example implementation COMP for asyncio within TD which you can use to get bleak running.

Also see this forum thread for some examples

2 Likes