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
    • Unread
    • Recent
    • Tags
    • Users
    • Login

    Executing a script within a script

    PYTHON Development
    0
    3
    804
    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.
    • H
      Helper
      last edited by

      On 30/07/2018 at 12:31, xxxxxxxx wrote:

      Hi everyone,

      I'm trying to figure out how I can make this bit of code work to open an existing script within a different script?

      import c4d
      from c4d import gui
      import os
        
        
        
          
          
      def main('C:\Users\Benjamin\AppData\Roaming\MAXON\CINEMA 4D R18_62A5E681\library\scripts	est1.py') :
         
          fl = open('C:\Users\Benjamin\AppData\Roaming\MAXON\CINEMA 4D R18_62A5E681\library\scripts	est1.py', 'rb')
          
          code = compile(fl.read(), 'C:\Users\Benjamin\AppData\Roaming\MAXON\CINEMA 4D R18_62A5E681\library\scripts	est1.py', 'exec')
          
          scope = {'__file__': 'C:\Users\Benjamin\AppData\Roaming\MAXON\CINEMA 4D R18_62A5E681\library\scripts	est1.py', '__name__': '__main__'}
        
        
          exec code in scope
        
        
        
      if __name__=='__main__':
          main()
        
        
      
      
      1 Reply Last reply Reply Quote 0
      • H
        Helper
        last edited by

        On 31/07/2018 at 02:40, xxxxxxxx wrote:

        Hi,

        To execute a script from a file one can use execfile().

        Note the user library directory can be retrieved calling c4d.storage.GeGetC4DPath(c4d.C4D_PATH_LIBRARY_USER).

        1 Reply Last reply Reply Quote 0
        • H
          Helper
          last edited by

          On 31/07/2018 at 19:55, xxxxxxxx wrote:

          Thanks Yannick

          That worked well. I ended up using this

              execfile(c4d.storage.GeGetC4DPath(c4d.C4D_PATH_LIBRARY_USER)+os.sep+"scripts"+os.sep+"VolumetricSwitch.py")
            
          
          
          1 Reply Last reply Reply Quote 0
          • First post
            Last post