Setting up a Dictionary in a Modifier

142 viewsGeneralcustom_code modifiers utility
0

Hello everyone,

Yesterday, with the help of this tutorial, I was able to add custom methods to a CSS script.

Today I wanted to use this knowledge for a custom modifier setup.

Code

from types import MethodType
if type(self.get_modifier_value("m20")) != dict:
  self.set_modifier_value("m20", {})

def setMod(self, key, value):
  self.get_modifier_value("m20")[key] = value

def getMod(self, key=None):
  if key == None:
    return self.get_modifier_value("m20")
  return self.get_modifier_value("m20")[key]

self.setMod = MethodType(setMod, self)

self.getMod = MethodType(getMod, self)

Explanation

The code first creates a dictionary at Modifier m20 (if no dictionary is already present).

Then the code creates 2 new methods:
– self.setMod(key, value)
– self.getMod(key)

With the method self.setMod you can add a key/value pair to the dictionary.
With the method self.getMod you can get the value from a given key inside the dictionary.

This way, you’re able to store as many named modifiers as you’d like.

Setting up a Reaction with this code

  1. Create a new Reaction, at the top of your list of Reactions.
  2. Use “Script –> script is initialised” as your Listener
  3. Add the custom code to the Action of your Action Block
  4. Save the Reaction

After that, you can use the self.setMod and self.getMod methods inside custom code in other Reactions.

Files

I’ve added 2 json files to this post, both are singular Reactions:

– The file “Modifier Dictionary Setup” contains the code in this post all set up and ready to go.
– The file “Modifier Dictionary Tests” contains a Reaction called “TESTS” that demonstrates the methods.

JohnC Answered question July 6, 2024
0

Have you had a look at ‘lists’ in CSS? https://youtu.be/ZYzkH1_ZL6U?si=Xh7KJB3yYBn_VKtW

JohnC Posted new comment July 8, 2024

No, I hadn’t really looked into Lists yet. But to be honest, I hadn’t really needed to iterate over multiple objects in a custom list. Up to this point I just used Modifiers to store single data pieces.

I thought a dictionary might be useful because then I don’t need to remember what Modifier I used for what piece of data, I can use a name that’s easy to remember for me. Plus, it allows me to store more than 20 mods (if that’s ever needed).

Very cool Glenn, you’re digging deep with your work

You are viewing 1 out of 1 answers, click here to view all answers.