How to transfer variable to xpresso from Python tag


#1

Hello everyone. I have python tag and xpresso. How can I get the variable from Python tag in xpresso?
I get an error in console “SyntaxError: can’t assign to function call”


#2

Every Python tag and node has its own execution context, so you need to handle them like separate programs, which are executed under different triggers. So you cannot just pass a variable from one Python “something” to another Python “something” like this.

In this case, it is even more difficult as the variable seems to be set in a message function - which is triggered by the program-wide messaging system - but needs to be read by an XPresso - which is triggered during the object tree evaluation. So, the XPresso is not even part of the same execution sequence.

You need to treat both parts as separate and asynchronous sub-programs, not functions that must be called in sequence. Store the value in a User Data for example, and do not rely on the XPresso to be evaluated between a press and a release of your button at all (it may be, or it may not be).

(Provided I read your screenshot right…)


#3

Okay, can you say me how can I change user data by id? Google haven’t helped.


#4

User data is accessed in Python as:
obj[c4d.ID_USERDATA,index]
where obj is the item where the user data is added, and index is the index number of the GUI control.

The following code (based on your own code) can be applied as Python tag to an object which contains one integer user data field with index 1 and a button with index 2. It will count the integer field up by 1 every time you press the button.

import c4d

def message(msg_type,data):
    if msg_type == c4d.MSG_NOTIFY_EVENT:
        event_data = data['event_data']
        if event_data['msg_id'] == c4d.MSG_DESCRIPTION_COMMAND:
            desc_id = event_data['msg_data']['id']
            if desc_id[1].id == 2:
                print "Increase"
                obj = op.GetObject()
                obj[c4d.ID_USERDATA,1] += 1 # 1 is index of an integer input field
                c4d.EventAdd()
            else:
                print "whatever"
            
def main():
    obj = op.GetObject()
    bc = c4d.BaseContainer()
    obj.AddEventNotification(op, c4d.NOTIFY_EVENT_MESSAGE, 0, bc)