Maxon Developers Maxon Developers
    • Documentation
      • Cinema 4D Python API
      • Cinema 4D C++ API
      • Cineware API
      • ZBrush GoZ API
      • Code Examples on Github
    • Forum
    • Downloads
    • Support
      • Support Procedures
      • Registered Developer Program
      • Plugin IDs
      • Contact Us
    • Categories
      • Overview
      • News & Information
      • Cinema 4D SDK Support
      • Cineware SDK Support
      • ZBrush 4D SDK Support
      • Bugs
      • General Talk
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Calling BaseDocument.SetMode twice in a row leads to freezes

    Bugs
    windows python 2024 2023
    2
    4
    1.0k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • D
      datamilch
      last edited by ferdinand

      hi there,

      i'm writing a modeling script which is quite complex at this point. now i'm experiencing very strange instant hangs of c4d when executing the script.
      it was super hard to pinpoint. took me some hours.
      please tell me i'm not stupid and this thing is real! ... and hopefully solvable.

      a new doc with a null and an active python tag (empty) + the execution of the following script in the scripts-manager = instant hang (at least for me)

      if i deactivate the python tag, the script will work.

      import c4d
      def main():
          print ("started")
          doc = c4d.documents.GetActiveDocument()
          doc.SetMode(c4d.Mpolygons)
          doc.SetMode(c4d.Medges)
          print("ok")
          return
      
      if __name__=='__main__':
          main()
      

      when it hangs, it doesen't

      what's even stranger:

      the original script was obviously longer, and the file had lots of objects,
      so i was incrementally reducing the script and the objects to find the bug.
      while boiling down the setup, there were stages, when the script worked randomly.
      then there was a stage, where it would run with 2 active python tags, but with 3 it would definitly hang.

      when it hangs, it doesn't even print "started". ... my working original script would run for about 3-4 seconds, but when i added the faulty line of code, the script would hang instantly after hitting the execute button. it would not run anything at all. so i imagine there could be an error compiling the script or somehing like that.

      • the script hangs with r2024 and r2023
      • it works fine with r20
      • the files are attached

      i'd be super grateful if someone could confirm this or even tell me, what is going on.

      cheers sebastian

      a crash_test_01.c4d
      a crash_test_01.py

      ferdinandF 1 Reply Last reply Reply Quote 0
      • ferdinandF
        ferdinand @datamilch
        last edited by ferdinand

        Hey @datamilch,

        Thank you for reaching out to us. I can confirm the behaviour both in 2023 and 2024. I can also confirm that the behaviour is somewhat erratic with sometimes the freeze occurring and sometimes not for the same sequence of actions.

        • The content of the Python Programming tag is irrelevant for the problem.
        • Other Python scripting objects, e.g., a Python Generator object, do not seem to trigger the problem.
        • The trigger is seemingly calling BaseDocument.SetMode twice in a row. (Mpolygons, Medges), (Medges, Mpolygons), (Manimation, Mpolygons), and (Manimation, Mtexture) did all trigger the freeze.
        • Only setting the document mode once does not seem to trigger the problem.
        • This also seems to be an initialization problem of the scene document, because running first a "non-freezing script" with only one SetMode will also let run a following "freezing script" with two SetMode without problems.
        • Also having the scene open and interacting with it (without changing the document mode manually) does suppress the problem.
        • Test more than once, I had roughly 1/10 flukes where it did not freeze but then the next time it would.

        I have field this as a bug and moved it to our bug section. For now I would recommend not to run Python Script Manager scripts which set the document mode twice.

        Cheers,
        Ferdinand

        PS: I am not 100% sure if you meant it like this, but the fact that nothing is being printed does not mean that your script did not even run before the freezes occurs. Script Manager scripts are blocking, i.e., you will only see the output of a script once it has ran. When you had script which prints every second an "A", and you would run it in a 100 sec loop, you would see for 100 seconds nothing, and then suddenly 100 "A"s in the console (once the script finished).

        MAXON SDK Specialist
        developers.maxon.net

        D 1 Reply Last reply Reply Quote 0
        • ferdinandF ferdinand moved this topic from Cinema 4D SDK on
        • D
          datamilch @ferdinand
          last edited by

          hi @ferdinand,
          thanks for the confirmation. i found a way to avoid calling setmode for now.

          concerning your PS:
          thats interesting, i have to pay attention to that. always had the feeling, that i could print feedback while it is running. and if there were errors in the script, it would only raise them when the running script was alble to reach these lines. e.g. after a few seconds.
          or sometimes i get the errors only if an if-contition is fulfilled. so i was surprised, that it would hang instantly, even though i expected it to run at least some stuff in the beginning.
          i think only syntax errors are displayed instantly.
          not sure if we are talking about the same thing though.

          all in all, i feel lucky that hangs occure rarely for me.
          thanks again!

          ferdinandF 1 Reply Last reply Reply Quote 0
          • ferdinandF
            ferdinand @datamilch
            last edited by ferdinand

            @datamilch said in Calling BaseDocument.SetMode twice in a row leads to freezes:

            always had the feeling, that i could print feedback while it is running

            That is a misconception many users have. This is because most code usually runs very fast, so you do not realize it. Note that I also wrote Script Manager script. While it technically applies to all Python Scripting scene elements, the execution of the Python VM is there always blocking, it is much more prominent for a Script Manager script. Because there the module is only executed once and it is also okay to run scripts here which take seconds to execute. But it also applies for example to a Python Programming tag, the execution of the module is there also blocking. But because the module of a Python Programming tag is called many times - on each scene update at least once, and you are anyway in a world of hurt when your Python Programming tag module takes more than ~100 ms to run, it is not that obvious there.

            Syntax errors are evaluated before the VM actually runs because then the compilation of your code into byte code fails (which is then later interpreted by the VM). But the error is here also displayed only after the VM stopped (just as a RuntimeError which is raised by the VM and not by the compiler), but syntax errors prevent the VM from running your code in the first place.

            Cheers,
            Ferdinand

            import time
            
            def main() -> None:
                """Run as a Python Script Manager script to see that the execution of a scope in the Python
                VM is blocking. This will not print five 'A's with a stride of one second, but five 'A's after 
                five seconds.
                """
                for _ in range(5):
                    print('A')
                    time. Sleep(1)
            
            if __name__ == '__main__':
                main()
            

            MAXON SDK Specialist
            developers.maxon.net

            1 Reply Last reply Reply Quote 0
            • First post
              Last post