Page 1 of 1

Python / M4L Observe Element ?

Posted: Mon Aug 20, 2012 6:02 pm
by leighhunt
Hi all,

Ducking and diving in Python atm.....

If anyone with some deeper knowledge than me could take a look at this script:
Currently I can get the value of the ButtonElement, the required function is to observe changes to it also;

Code: Select all

#qManager.py 
import Live 
import time 

from _Framework.ControlSurface import ControlSurface 
from _Framework.MyObserver import MyObserver # Class the same as ButtonElement class
class qManager(ControlSurface):
    
    pplisten = {}
    typeIndex = 0
    qmButton1 = 0
    def __init__(self, c_instance):
        ControlSurface.__init__(self, c_instance)
        self.qmButton1 = MyObserver(True, 0, 0, 14)
        self.qmButton1.name = 'PP Changed Value Output'
        self.qmButton1.value = 0.
        cb = lambda :self.parent_changestate(self.qmButton1)
        self.qmButton1.add_value_listener(cb) 
        # Here I get an error in log.txt : 'NoneType' object has no attribute 'add_value_listener'
    def pp(self, pid):
        # Here I add a couple of value listeners from M4L world to the pplisten dictionary
        cb = lambda :self.param_changestate(pid)
        if self.pplisten.has_key(pid) != 1:
            pid.add_value_listener(cb)
            self.pplisten[pid] = cb
            return self.typeIndex
            # self.typeIndex is for future use
        return 1
        # These returns pass successfully to an assigned live.object
    def param_changestate(self,pid):
        # Here I pass a value from any pplistener to my ButtonElement(MyOberver)
        self.qmButton1.value = pid.value
    def parent_changestate(self,pid):
        pass # Should I do something here?
    def pp_type(self, ppType):
        self.typeIndex = ppType
 
The MyObserver is a copy of ButtonElement.py, with a 'value' function added:

Code: Select all

# emacs-mode: -*- python-*-
# -*- coding: utf-8 -*-

import Live 
from InputControlElement import * 
ON_VALUE = int(127)
OFF_VALUE = int(0)
value = 0.

class MyObserver(InputControlElement):
    ' Class representing a button a the controller '

    def __init__(self, is_momentary, msg_type, channel, identifier):
        InputControlElement.__init__(self, msg_type, channel, identifier)
        assert isinstance(is_momentary, type(False))
        self._MyObserver__is_momentary = is_momentary



    def is_momentary(self):
        ' returns true if the buttons sends a message on being released '
        return self._MyObserver__is_momentary



    def message_map_mode(self):
        assert (self.message_type() is MIDI_CC_TYPE)
        return Live.MidiMap.MapMode.absolute



    def turn_on(self):
        self.send_value(value)



    def turn_off(self):
        self.send_value(value)

    def value(self, value2):
        value = value2
        return value


# local variables:
# tab-width: 4
I've included comments in the script where things are happening - or not happening!
FYI - a live.observer object succesfully connects to id of the ButtonElement, it is only that it doesn't 'observe' the value, it can only 'get' the value.

I wondering if it is possible to 'observe' the values of Control Surface elements as they change within a script?

Thanks in advance,
Leigh

Re: Python / M4L Observe Element ?

Posted: Mon Aug 20, 2012 7:59 pm
by amounra93
Leigh, drop me some mail and I'll sort you out. I can't look at this extensively right now, but generally you have the right idea. There's some voodoo involved in order to get MxDCore to recognize the NotifyingControlElement's _value_notifications as a valid observable property. I'll have to look at it a little more closely, should be able to check it out later tonight.

Cheers,

a

Re: Python / M4L Observe Element ?

Posted: Mon Aug 20, 2012 8:16 pm
by leighhunt
amounra93 wrote:Leigh, drop me some mail and I'll sort you out. I can't look at this extensively right now, but generally you have the right idea. There's some voodoo involved in order to get MxDCore to recognize the NotifyingControlElement's _value_notifications as a valid observable property. I'll have to look at it a little more closely, should be able to check it out later tonight.

Cheers,

a
Cheers mate - sending you a message now