How to use/import another script file in Python tag correctly?



  • Hello,

    I have 50 objects that every one of them should use a python tag. Tag Script will be same for 50 objects but that script will be changed time to time so I tried to import this script from python tags but code running infinitely. I placed my custom code (customCode.py) to "C:\Program Files\Maxon Cinema 4D R23\resource\modules\python\libs\python37" library and I added a python tag and its script is below:

    import c4d
    
    import customCode
    
    def main():
    
    print ("test")
    
    customCode.myFunction(op)
    

    my function is running correctly. But tag script runs infinitely. I printed "test" text and I could see this situation.

    Could anyone give me information what I did wrong here please?

    By the way customCode.py is like below:

    import c4d
    
    def myFunction(getObj):
    
    obj=getObj.GetObject()
    
    var1=obj[c4d.ID_USERDATA,9]
    


  • Hi:

    @delizade

    I don't think it's necessary to import external libraries, because such simple code can be used directly.I have a suggestion that you should add data in Python tag, not in the object where the Python tag are located. As for the Python tag running repeatedly, it is because your code has no conditional comparisons.Here is the Python tag code, based on the file you provided, that will only run when the frame changes.

    import c4d
    
    def myFunction(getObj):
        obj=getObj.GetObject()
        var1=obj[c4d.ID_USERDATA,1]
        print (var1)
        print ("myFunction: "+str(var1))
        #c4d.EventAdd()
    
    def main():
        frame = doc.GetTime().GetFrame(doc.GetFps())
    
        obj  = op.GetObject()
        var1 = obj[c4d.ID_USERDATA,1]
        
        if frame * 0.01 != var1:
            
            print (var1)
            print ("myFunction: "+str(var1))
            
            obj[c4d.ID_USERDATA,1] = frame * 0.01
            c4d.EventAdd()
    


  • I've solved the issue
    c4d.EventAdd() was added end of my external function script. I removed it and used it in tag script. I think after that line python tag was triggered again.



  • Hi @delizade, thanks for reaching out to us.

    With regard to your question I've a few considerations to share:

    • external libraries are supposed to be located either under %APPDATA%/MAXON/{cinemaversion}/python37/libs, if they are referred only to a specific Cinema version or in %APPDATA%/MAXON/python37/libs if they are supposed to be seen by all Cinema(s) installed on your machine (see here for further info);
    • c4d.EventAdd() adds an event to the Cinema 4D events queue, and it's not responsible for the tag to not be executed "infinitely". Consider that a Python tag is executed every time the scene is evaluated so the behaviour your mention is consistent with the code posted.

    Given said that and with regard to
    @delizade said in How to use/import another script file in Python tag correctly?:

    've solved the issue
    c4d.EventAdd() was added end of my external function script. I removed it and used it in tag script. I think after that line python tag was triggered again.

    I can't see, guessing your code I've reported below, how you stopped the PythonTag to be executed "infinitely".

    import c4d
    import test
    
    def main():
        print ("test")
        customCode.myFunction(op)
    

    Cheers, Riccardo



  • hi @r_gigante,
    I've created the scene again because I changed that workflow tons of time. You can find scene file and script. If I remove "c4d.EventAdd()" line from external code problem is gone.

    External Script file: customCode.py
    Scene file: externalPythonSample-infinity_problem.c4d

    thank you



  • Hi:

    @delizade

    I don't think it's necessary to import external libraries, because such simple code can be used directly.I have a suggestion that you should add data in Python tag, not in the object where the Python tag are located. As for the Python tag running repeatedly, it is because your code has no conditional comparisons.Here is the Python tag code, based on the file you provided, that will only run when the frame changes.

    import c4d
    
    def myFunction(getObj):
        obj=getObj.GetObject()
        var1=obj[c4d.ID_USERDATA,1]
        print (var1)
        print ("myFunction: "+str(var1))
        #c4d.EventAdd()
    
    def main():
        frame = doc.GetTime().GetFrame(doc.GetFps())
    
        obj  = op.GetObject()
        var1 = obj[c4d.ID_USERDATA,1]
        
        if frame * 0.01 != var1:
            
            print (var1)
            print ("myFunction: "+str(var1))
            
            obj[c4d.ID_USERDATA,1] = frame * 0.01
            c4d.EventAdd()
    


  • hi @x_nerve thank you for your time.
    code is not simple actually it has about 500 lines as far as I remember. To make scenario clear here I wrote very simple scripts. I needed to use external link for all python tags because that code is changing time to time (bug or new features etc) so with this way I change code in a single source.

    With this entry I had an "infinitely running script" problem and it was solved by removing "c4d.EventAdd()" line from external function. After this I had another issue with python tags. It was triggered whenever is changed in the scene. To prevent that I tried frame triggered python tags so with your answer it mixed with that issue below paragraph is my further step about the main topic)

    I've tried frame triggered tags but not by your way. I added null object and attach a python tag on it. This simple code watch current take changing and if take is changed it changes the frame so tags are triggered that way. Because I use takes. every take holds different data values. So when I changed active take, python tags has to be run.

    I will check your code as well currently I've been struggling with new issue about takes - user data - scene update :/
    thank you again.



  • Hi,

    without further feedback, we will consider this thread as solved by Monday and flag it accordingly.

    Cheers,
    Ferdinand


Log in to reply