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

    Differences between menu item execution and execution through the script editor

    Cinema 4D SDK
    2024 python windows
    2
    4
    616
    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
      Hongzz
      last edited by i_mazlov

      Script layout can normally execute the command, but the icon under the menu can not correctly execute the command, why, I am a beginner, please answer the master doubts!d65a5bc6-8f35-4cc7-804a-d55b64f18d35-image.png 7c904fc5-0822-46a2-8763-ed2d4a03f06d-image.png

      Updated by Ilia on 22/05/2024:
      Consolidated consequent messages in a single posting:

      import os
      import c4d
      
      class ImportDialog(c4d.gui.GeDialog):
          BUTTON_ID_BASE = 1000
      
          def __init__(self):
              self.files = []  # 初始化为空列表
      
          def LoadFiles(self):
              startup_path = c4d.storage.GeGetStartupWritePath()
              project_dir = os.path.join(startup_path, 'library', 'scripts', 'spline load', 'projects')
              if not os.path.exists(project_dir):
                  c4d.gui.MessageDialog('项目目录不存在。')
                  return False
      
              self.files = [os.path.join(project_dir, f) for f in os.listdir(project_dir) if f.endswith('.c4d')]
              return True
      
          def CreateLayout(self):
              self.SetTitle("Import C4D Projects")
              if not self.LoadFiles():
                  return False  # 如果加载文件失败,不创建布局
              for index, file in enumerate(self.files, start=self.BUTTON_ID_BASE):
                  filename = os.path.splitext(os.path.basename(file))[0]
                  self.AddButton(index, c4d.BFH_CENTER, name=filename)
              return True
      
      global_dialog_instance = None
      
      def main():
          global global_dialog_instance
          if global_dialog_instance is None:
              global_dialog_instance = ImportDialog()
          global_dialog_instance.Open(c4d.DLG_TYPE_ASYNC, defaultw=400, defaulth=50)
      
      if __name__=="__main__":
          main()
      
      H 1 Reply Last reply Reply Quote 0
      • H
        Hongzz
        last edited by

        This post is deleted!
        1 Reply Last reply Reply Quote 0
        • H
          Hongzz @Hongzz
          last edited by

          This post is deleted!
          i_mazlovI 1 Reply Last reply Reply Quote 0
          • i_mazlovI
            i_mazlov @Hongzz
            last edited by i_mazlov

            Hello @Hongzz,

            Welcome to the Maxon developers forum and its community, it is great to have you with us!

            Getting Started

            Before creating your next postings, we would recommend making yourself accustomed with our forum and support procedures. You did not do anything wrong, we point all new users to these rules.

            • Forum Overview: Provides a broad overview of the fundamental structure and rules of this forum, such as the purpose of the different sub-forums or the fact that we will ban users who engage in hate speech or harassment.
            • Support Procedures: Provides a more in detail overview of how we provide technical support for APIs here. This topic will tell you how to ask good questions and limits of our technical support.
            • Forum Features: Provides an overview of the technical features of this forum, such as Markdown markup or file uploads.

            It is strongly recommended to read the first two topics carefully, especially the section Support Procedures: Asking Questions.

            About your First Question

            When removing all the path handling fluff from your code, everything works as expected (the layout is created properly, when runs from both "Execute" button and the menu item). Please check the attached script below.

            This actually means that there's something fishy going on with the files on your filesystem or the way you're dealing with them. I'd suggest improve handling edge cases, when the path doesn't exist or when there're no files in the specified path.

            There're a couple more comments on your posting.

            First of all, storing your dialog in the global scope of the script is a "hack" that is a) highly not recommended in general b) exceptionally not recommended for being shipped to the end user. You should consider developing a plugin instead, if you need your dialog to be persistent over multiple openings of your dialog. You can have a look into our github repository for some references, e.g. the CommandDataDialog example.

            Secondly, please consolidate your consequent messages in a single posting as it improves the readabily of the forum (please check the "Support Topic Rules" part of our Support Procedures). For your previous message I've already done this for you.

            Cheers,
            Ilia

            The code snippet without unnecessary path handling parts:

            import os
            import c4d
            
            class ImportDialog(c4d.gui.GeDialog):
                BUTTON_ID_BASE = 1000
            
                def __init__(self):
                    self.files = ['myFile1.c4d', 'myFile2.c4d', 'myFile3.c4d']
            
                def CreateLayout(self):
                    self.SetTitle("Import C4D Projects")
                    for index, file in enumerate(self.files, start=self.BUTTON_ID_BASE):
                        filename = os.path.splitext(os.path.basename(file))[0]
                        self.AddButton(index, c4d.BFH_CENTER, name=filename)
                    return True
            
            global_dialog_instance = None
            
            def main():
                global global_dialog_instance
                if global_dialog_instance is None:
                    global_dialog_instance = ImportDialog()
                global_dialog_instance.Open(c4d.DLG_TYPE_ASYNC, defaultw=400, defaulth=50)
            
            if __name__=="__main__":
                main()
            

            MAXON SDK Specialist
            developers.maxon.net

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