Python script to Python tag hand over?
-
I'm working on a script that creates a python tag with some code.
Now I want the main script to talk to the python tag script and hand over some information.
I do it in an incredibly hacky way, and like to know if this is appropriate, or if there is a more elegant/reliable way to do it?
def add_python_tag(obj, crtl1, crtl2): if not obj: return pos_1 = get_global_position(crtl1) pos_2 = get_global_position(crtl2) distance_init = (pos_1 - pos_2).GetLength() python_tag = c4d.BaseTag(c4d.Tpython) python_code = """ import c4d def main(): #some more code ... pos_1 = get_global_position(ctrl_01) pos_2 = get_global_position(ctrl_02) distance_const = distance_init distance = (pos_1 - pos_2).GetLength() stretch_coefficient = distance_const / distance # and even more code ... # Execution if __name__ == '__main__': main() """ python_code = python_code.replace("distance_init", str(distance_init)) python_tag[c4d.TPYTHON_CODE] = python_code obj.InsertTag(python_tag) c4d.EventAdd()
-
Hi your way of doing is correct if you want to share only constant information.
However its a bit unpractical, I would recommend you to use f-string instead of the replace, which is slow and can can have side effect since you can't pin-point a specific location in your code template.
If you want something more dynamic, you will need to write data within the BaseContainer of the object (see Python tag initalization?) In this example this is the tag that write to the BaseContainer, but nothing prevent you in your script from the script manager, to write in the BaseContainer of the tag and in the tag read the value.
Cheers,
Maxime. -
Thanks for your help and nudging me into the right direction, I think I can work with that