Listen Specific UserData field [SOLVED]

On 19/03/2016 at 23:15, xxxxxxxx wrote:

Hi, i need to execut some functions from my script everytime when user change something in one user data field. Im just a beginer in python sdk and i dont understand very well how it works but i thought a little and i made for my userdata field that i want to listen a clone  and i hide it from script(just to store the old value) and i compare always with main field. If something changed i do something. In may case i just change the value of chekbutton.

here my script:

import c4d

def main() :
    obj = op.GetObject() #get an object with python tag
    ud = obj.GetUserDataContainer() #get an userdata container
    ud[2][1][c4d.DESC_HIDE] = True #hide my field copy 
    obj.SetUserDataContainer(ud[2][0], ud[2][1])
    new = obj[c4d.ID_USERDATA,1] #value of my filed that i want to listen
    old = obj[c4d.ID_USERDATA,3]  #value of clone 
    
    if old != new:
        obj[c4d.ID_USERDATA,2] = 1 - obj[c4d.ID_USERDATA,2] #change value of radiobutton to oposite
        obj[c4d.ID_USERDATA,3] = new #store value of my field to clone

So it works well but it looks like a trick. Is there other method to listen a change in userdata filed?

On 21/03/2016 at 03:07, xxxxxxxx wrote:

Hello,

what exactly do you mean with "script"? Do you mean a script that is stored and executed from the Script Manager? A script in the Script Manager can only be executed from that Script Manager, it cannot be triggered by an event.

Or is your script stored in a Python Tag? Then the best way would be indeed to store the old value in another user data field and compare it with the current value.

Best wishes,
Sebastian

On 21/03/2016 at 05:09, xxxxxxxx wrote:

From python tag. Thank you.

On 21/03/2016 at 05:26, xxxxxxxx wrote:

Originally posted by xxxxxxxx

Or is your script stored in a Python Tag? Then the best way would be indeed to store the old value in another user data field and compare it with the current value.

You can store the old value without an additional user data field.

import c4d
G = {}
  
def main() :  
    value = op[c4d.ID_USERDATA, 1]
    if value != G.get('old_value') :
        # Value changed or first time the script is run (eg. if 
        # the scene was just opened).
        G['old_value'] = value

Best,
Niklas

On 21/03/2016 at 07:30, xxxxxxxx wrote:

good method NiklasR. Thank you