Differences between menu item execution and execution through the script editor
-
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!
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()
-
This post is deleted! -
This post is deleted! -
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,
IliaThe 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()