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

    Executing Python code in C++: crash when importing c4d package

    Cinema 4D SDK
    c++ macos r23 python
    2
    4
    455
    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.
    • fwilleke80F
      fwilleke80
      last edited by fwilleke80

      Hi,

      I'm trying to do some things using Python, so I wrote a simple function to execute Python code, based on the example in the SDK docs. I've been using this function before (see https://developers.maxon.net/forum/topic/12973/opening-a-maxon-url-in-the-browser-anchors-and-query-parameters/3) with no problems.

      Here is the code to execute Python code from within a C++ plugin:

      maxon::Result<void> ExecutePythonCodeSimple(const maxon::String &code)
      {
      	iferr_scope;
      
      	// Check code
      	if (code.IsEmpty())
      		return maxon::IllegalArgumentError(MAXON_SOURCE_LOCATION);
      
      	// Create VM ref and scope
      #if API_VERSION >= 23000
      	const maxon::VirtualMachineRef& vm = MAXON_CPYTHON37VM();
      #else
      	const maxon::VirtualMachineRef& vm = MAXON_CPYTHON27VM();
      #endif
      	const maxon::VirtualMachineScopeRef scope = vm.CreateScope() iferr_return;
      
      	// Init script
      	iferr (scope.Init("Python Script"_s, code, maxon::ERRORHANDLING::PRINT, nullptr))
      	{
      		const String errorMessage = "Error on Init()"_s;
      		return maxon::UnknownError(MAXON_SOURCE_LOCATION, errorMessage);
      	}
      
      	// set __name__ = __main__
      	scope.Add("__name__"_s, maxon::Data("__main__"_s)) iferr_return;
      
      	// executes the script and returns when it got executed.
      	// info: if the script causes an unexpected infinite loop, Execute() does not return
      	// and there is no way to stop from the outside.
      	iferr (scope.Execute())
      	{
      		const String errorMessage = "Error on Execute()"_s;
      		return maxon::UnknownError(MAXON_SOURCE_LOCATION, errorMessage);
      	}
      	return maxon::OK;
      }
      

      The Python code I want to execute relies on functionality from the c4d package. Here's the - very simplified - code:

      maxon::Result<void> MyFunction()
      {
      	iferr_scope;
      
      	maxon::String theCode;
      	theCode = "import c4d\n"_s;
      	theCode += "print('Hello World!')\n"_s;
      
      	// Execute code
      	ExecutePythonCodeSimple(theCode) iferr_return;
      	return maxon::OK;
      }
      

      So, the actual Python code comes down to this:

      import c4d
      print('Hello World!')
      

      It crashes reproducibly:
      Screenshot 2021-01-06 at 15.14.15.jpg

      If I remove the line import c4d, it does not crash.

      Why, and how can I solve this?

      Thanks in advance & best greetings,
      Frank

      www.frankwilleke.de
      Only asking personal code questions here.

      1 Reply Last reply Reply Quote 0
      • kbarK
        kbar
        last edited by

        You are calling the Python module before it is loaded and initiated. You could try setting the priority of your plugin to ensure it is the last plugin loaded.

        Also you shouldn’t be calling that in PluginStart. You should be calling it once all the plugins have been started, ie the Python module.

        Instead try calling it on StartActivity or possibly Program_Started.

        https://developers.maxon.net/docs/cpp/2023_2/page_manual_module_functions.html#page_manual_module_functions_functions_message_start

        https://www.gamelogicdesign.com
        https://www.plugins4d.com

        1 Reply Last reply Reply Quote 0
        • fwilleke80F
          fwilleke80
          last edited by

          Hi Kent,

          thanks! I'll try that! I was under the impression, that all "built-in" modules are registered before the plugins.
          Will reply here and report if that solved it 🙂

          Cheers,
          Frank

          www.frankwilleke.de
          Only asking personal code questions here.

          1 Reply Last reply Reply Quote 0
          • fwilleke80F
            fwilleke80
            last edited by

            I was in deed calling the code from within PluginStart(). Calling it from PluginMessage(C4DPL_STARTACTIVITY) fixed it. Thank you, Kent!

            Cheers,
            Frank

            www.frankwilleke.de
            Only asking personal code questions here.

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