CSS extra coding

861 viewsCSS Questionsattributes
1

I see that CSS allows to use some coding inside action blocks. It works well when some condition, parsing code added there. But i see a problem with adding attributes. As there are not enough modifiers, and storing data outside is not comfortable, i would like to know:

Is there a way to store data inside CSS scripts?

ChatGPT is suggesting to add this code into an action:

if not hasattr(self, "_rec_blink_active"):
    self._rec_blink_active = False
if not self._rec_blink_active:
    self._rec_blink_active = True
    self.midi_cc_ch_0_val_16.send_value(57)

But this crushes ableton really hard so the reaction stop working correctly by parts or the whole reaction misses…

Only PC reboot helps.

So is there a way to add attributes and store data there? What is a correct way to do so if possible?

Hellem Answered question
0

You can add more modifiers directly in the script json file if you need to:

Is there anyway we can get more than 20 Modifiers?

Or simply write global variables in custom code: self.my_var = 1

We’re not able to support chatgpt or custom code but..
from what you have pasted there’s no indentation, which breaks python.

JohnC Posted new comment

wow!!! thats extra awesome!

I mean i used chat gpt to write code and then i implemented it into reactions actions body. No deep integration with chat gpt needed.

And about attributes: can you, please, provide an instructions how to declare them properly?
You have said that self.my_var = 1 is a global varibale, and where it can be putted? Like in a reaction action section? Show an example, please.

yes, i see that the code i inserted misses indentation – that happened due copy action to the forum here, thank you. There are indentations in the script i used. And it compiled properly.
But it act really strange… It just drops the action block with that code for attributes in action body (that was for clip_slot.is_recording) and started executing another action block (for clip_slot.is_playing)

“And about attributes: can you, please, provide an instructions how to declare them properly?”
Put them in the action section of an action block (switch the action to custom code, remove any code that’s in there then write your own)

thsnk you! Got success!!!! <3

Awesome!

How can we add a listeners to custom attributes stored?
Like self.my_var was updated?
As we actually can add a listeners for modifiers updated.
And how to do it with custom variables?

Attaching listeners to custom attributes is getting into pure Python coding not related to CSS.

Only modifiers can be used as listeners in CSS.
(Add more modifiers via the json file if you need more)

0

Does CSS allow def in actions?

Like can i insert this in action block? And what is a proper way to declare functions? To make a repetetive blink for led and not to have a time listener in reaction.

def _blink_tick(self):
    # stop if no longer active
    if not getattr(self, "_blink_active", False):
        return
     # get current sub (1..4)
    t = self.song().get_current_beats_song_time()
    parts = str(t).replace(",", ".").split(".")
    sub = int(parts[2]) if len(parts) >= 3 and parts[2].isdigit() else 1
    sub = min(4, max(1, sub))
     # toggle on sub 1 / off on sub 3 (your original pattern)
    if sub == 1:
        self.midi_cc_ch_0_val_16.send_value(57)
    elif sub == 3:
        self.midi_cc_ch_0_val_16.send_value(0)
     # schedule next tick (fastest is 1)
    self.schedule_message(1, self._blink_tick)

Hellem Posted new comment

Yes, CSS allows to define functions. They will be available only in the same reaction where they were declared.
!!! BUT NOT ALWAYS !!!

You can also declare variables in Actions.

Useful when you need duplicate mappings with only a few parameters changed; you can put those parameters in variables in the first Action; no need to search through the different Action Blocks or code to change them.

Yep, that helps. I created outgoing midi send functions for easy remapping.

0

!!! BE CAREFUL !!!

It was considered that functions declared in separate listeners would be available from a reaction where they were declared only – BUT AT SOME CASES – it goes really wrong. So:

!!! USE UNIQUE !!!

  • functions names
  • parameters names
Hellem Posted new comment

Or define them inside the user.py file, then they’re part of the User class.

What is the user class? How to work with it?

See the section “External Custom Coding with user.py” here: https://community.remotify.io/knowledge/custom-coding-in-reactions-css/

You can make methods in this class and then call them inside your Reactions; e.g. “self.user.[method_name](arguments)”. Your function naming won’t conflict with other names from CSS’s functions this way.

One important note: if you want to access some of the functions from Live’s API (like “send_midi”) inside your User class instance, then you’ll have to add something like the following inside the “__init__”:

“self.c_instance = c_instance”

This “c_instance” contains access to those Live API functions. So if you then want to call “send_midi” from inside the User class, you can use “self.c_instance.send_midi( … )”.

Great!
Thank you!

One benefit from using a user.py file is that you can make adjustments to your code, and reload Ableton without having to build a new script with the CSS app.

For example:
Let’s say you have a method in the User class called “test()”; inside this test method you can setup whatever code you like; you build a Reaction with CSS that’s triggered by a button press and that calls “self.user.test()”; you only need to build this mapping once but can then alter the code of the “test()” method whenever you like, reloading the script in Ableton will make “test()” perform the altered code.

And if you’re using Ableton Live Beta, then reloading Ableton is as simple as with a keyboard shortcut:
https://remotify.io/instant-reload-midi-remote-scripts-in-ableton-live/

Thank you!
I use Ableton 11 for vst 2 PC midi messages support.
It won’t work on newer Ableton 12 with vst 3 …(((