-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUniqueParameters.py
50 lines (46 loc) · 1.71 KB
/
UniqueParameters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# UniqueParameters
# - class to make Live's device parameter names unique
#
# Part of ElectraOne.
#
# Ableton Live MIDI Remote Script for the Electra One
#
# Author: Jaap-henk Hoepman ([email protected])
#
# Distributed under the MIT License, see LICENSE
import Live
class UniqueParameter(Live.DeviceParameter.DeviceParameter):
"""Class extending Live's original parameter to redefine original_name
and name. These are 'Monkeypatched' using get_unique_parameters_for_device
See: https://stackoverflow.com/questions/31590152/monkey-patching-a-property
"""
# Define class variables with the same name as the properties we want
# to override; this shields the property defintions in DeviceParameter.
# Now an instance of this class can create its own
# instance variables with the same name without complaints about a missing
# setter for the instance
original_name = ""
name = ""
def make_device_parameters_unique(device):
"""Return the list device.parameters, in the same order, but making
original_name and name unique
- device: Live.Device
result: list of parameters; [UniqueParameter]
"""
original_names = {}
parameters = []
for p in device.parameters:
name = p.name
original_name = p.original_name
if original_name in original_names:
original_names[original_name] += 1
suffix = f'.{original_names[original_name]}'
else:
original_names[original_name] = 0
suffix = ''
p.__class__ = UniqueParameter
# This creates instance variables!
p.original_name = original_name + suffix
p.name = name + suffix
parameters.append(p)
return parameters