Xpresso Python Node . "Code" field
I need to add an "Object" port to an object node, but I guess since I have R20 with that particular python bug preventing it, I need to find a workaround. All I need to do is get the currently instanced objects name, fed back into the display name (and appending with another string in here). Ive seen that "UserData" workaround to get a makeshift Object port but that feels heavy handed for my case. Thought a python node with simple script can replace my Object node and Name port like so
And this works, but cant seem to figure out how to "set" this Python Code in this Node via my Python Script. Have made this snippet of code a function and was trying everything from OperatorSetData, and even the InsertObjects through In/Exclude method I was just shown for my Link List question. But nothing ive done yet seems to populate the field. Final Stretch! Thanks for all the help thus far, this script has already been an amazing learning experience.
def main(): global Output2 global Input3 Output2 = c4d.BaseList2D.GetName(Input3)
Cairyn last edited by
@esan Your Python node and code do not match up. You have renamed the ports to Link and String but in your code the globals that are supposed to connect to the ports are named Output2 and Input3. This must be consistent.
You can write the call to GetName easier as well by invoking it on the object (the input port).
import c4d def main(): global TheLink, ObjName # ports ObjName = TheLink.GetName()
Sorry, i have a lot of permutations of this and the code snippet isn't from that exact node pictured, just grabbed examples at random since the point of the question was asking how to populate the Python Node Code field with my Python Script. Once that works then yes ill make all the proper ports/names to accommodate, that part i have figured out and got working, just inserting code into the field is what im missing. Thanks
m_adam last edited by
So you can drag and drop parameter as explained in the Python Console Manual
esan last edited by
ahh, palm securely in face. Ya I had the GV_PYTHON_CODE but since I had spent so much time wrapping my head around this OperatorSetData in the node world prior I overcomplicated it from the start. Hadnt come across that console manual either, thanks a lot!
So final question. When i execute node[c4d.GV_PYTHON_CODE] = 'test string', all is well. When i try to exectute this as a function, i get errors. This setup gives me error " setitem expected str, not None"
i wanted to do a multi-line string incapsulation but the triple quotes seem to be reserved for title/headers in this environment. How would i go about conveying that this function is a Str if making it a bucket of strings doesnt do it?
def pythonNodeCode(): 'import c4d' 'def main():' ' global Output2' ' global Input3' ' Output2 = c4d.BaseList2D.GetName(Input3)' def main(): #skip a lot of other code node[c4d.GV_PYTHON_CODE] = pythonNodeCode()
(i tried node[c4d.GV_PYTHON_CODE] = str(pythonNodeCode()) too just to make sure the obvious wasnt sitting in my face)
edit: I achieved the intent with this, but still interested in the non-atrocious way to write it lol
node[c4d.GV_PYTHON_CODE] = 'import c4d \n\ndef main():\n\n global Output2\n global Input3\n\n Output2 = c4d.BaseList2D.GetName(Input3)'
Cairyn last edited by
@esan Your first version does not assign the string(s) to anything - a return is missing. What pythonNodeCode() is actually doing is to define a number of strings that are then discarded, and to return None as this is the default.
You can write it line by line if you add the \n manually to each line, and force Python to concatenate the strings by using line continuation \
def pythonNodeCode(): return \ 'import c4d\n'\ 'def main():\n'\ ' global Output2\n'\ ' global Input3\n'\ ' Output2 = c4d.BaseList2D.GetName(Input3)\n'\
I don't quite see why triple quotation should not work, maybe same issue?
def pythonNodeCode(): return ''' import c4d def main(): global Output2 global Input3 Output2 = c4d.BaseList2D.GetName(Input3) '''
esan last edited by
Ah youre right, the Return was the cause of the errors, not the triple quotes. Got it all working now! (after taking a few tries to get the "indents" with the strings right)
Thanks a ton!