MenuTest.py
1    """ 
2    The interesting part starts at class MainButtons below :) 
3    """
4    from __future__ import print_function
5    
6    try:
7        import poser
8    except ImportError:
9        raise RuntimeError("Must run in Poser.")
10   import os
11   import sys
12   import wx
13   
14   THISPATH = os.path.dirname(os.path.abspath(sys.argv[0]))
15   if not os.path.exists(os.path.join(THISPATH, "__init__.py")):
16       # this file must exist to be able to make includes
17       with open(os.path.join(THISPATH, "__init__.py"), "w"):
18           pass
19   
20   if THISPATH not in sys.path:
21       sys.path.append(THISPATH)
22   
23   import PoserLibs.PoserMenu
24   # this import is used to set some variables.
25   PoserLibs.PoserMenu.BUTTON_KEYWORD = "BUTTON"
26   PoserLibs.PoserMenu.TEMPDIR = poser.TempLocation()
27   
28   # Standard import.
29   from PoserLibs.PoserMenu import \
30       TEMPDIR, \
31       TITLE, \
32       BUTTON_KEYWORD, \
33       restore_buttons, \
34       MenuClass
35   
36   print("-" * 50 + "\nTemporary script are stored in '%s'." % TEMPDIR)
37   print("Button Keyword is: '%s'" % BUTTON_KEYWORD)
38   print("Script name is: '%s'" % TITLE)
39   print("(Feel free to change anything above to your needs.)\n" + "_" * 50)
40   print("\nMake sure the 'Python Buttons' are displayed in Poser: Main Menu->Window->Python Scripts")
41   
42   # A global variable to show transfer of data
43   # (see below where buttons are defined).
44   test1 = "This is global variable 'test1'"
45   
46   # Required to find this script for the back button.
47   THISFILE = os.path.abspath(__file__)
48   print("Script Base location:", THISFILE)
49   
50   
51   class MainButtons(MenuClass):
52   
53       def setup(self):
54           """ 
55           setup() - will be called before buttons are displayed. 
56           Use it to set up what ever you need. 
57           """
58           return
59   
60       # Remember: You may call the button-methods via script in
61       # addition to click it.
62       # Don't forget to define '*args', because the buttons are
63       # maybe called with parameters.
64   
65       def BUTTON_Number_1(self, *args):
66           """ 
67           Method name behind keyword ('BUTTON') doesn't matter. Except 
68           for 'next' and 'back' (see below). 
69    
70           Three args are declared below. 
71           'test1' is previously declared in the script as a global variable. 
72    
73           All other args results in whatever they contain if this button is 
74           clicked, or, in case they are not defined, just their variable-name. 
75           '$name' is a special variable and results in the name of the 
76           actual method ("BUTTON_Number_1" in this case). 
77    
78           You may leave comments in the description part. Anything not enclosed 
79           in ':' is ignored. Also anything after '#' in lines with keywords. 
80    
81           :<keyword>: <content> # <Description> 
82    
83           The content-part may have keywords also. These keywords are 
84           prefixed with '$'. 
85           $name = Name of method. 
86           $index = Button indexnumber. 
87    
88    
89           ===================== the interesting part ======================== 
90           :nr: 1 # Button number. 
91           :title: First Button from Main # Button-Title. 
92           :args: test1, test2, $name 
93           """
94           print("%s pressed. Button index=1" % args[-1])
95           print("'test1' is a global variable.")
96           print("This args I got:", args)
97   
98       def BUTTONnext(self, *args):
99           """ 
100          'next' and 'back' in method-name are keywords too. 
101          def BUTTONnext() and def BUTTONback() are automatically set to the last 
102          two buttons if no button indexnumber is given. 
103          ATTENTION: This may overwrite previously defined buttons (9 or 10)! 
104   
105          ===================== the interesting part ======================== 
106          :title: Second Menu >>> 
107          """
108          self.add(ButtonMenu_1)
109  
110      def BUTTONback(self, *args):
111          """ 
112          Here we switch back to original script menu, but we leave 
113          an entry to call this script again (assumed a free slot at index 9). 
114   
115          We can not use 'self.back()', because this menu is the first one 
116          in the list with no previous menu defined. So we must call the original 
117          ScriptButton-script via restore_buttons() from imported 'PoserMenu.py'. 
118   
119          ===================== the interesting part ======================== 
120          :title: <<< Back to Original 
121          (Button index is automatically set) 
122          """
123          restore_buttons()
124          poser.DefineScriptButton(9, THISFILE, TITLE)
125  
126  
127  # Now the second button menu, but without comments.
128  
129  class ButtonMenu_1(MenuClass):
130      def BUTTON_second_test(self, *args):
131          """ 
132          :Nr: 1 
133          :Title: 1. Button from Second Menu 
134          """
135          print("This is ButtonMenu_1 button nr. 1")
136  
137      def BUTTON_second_anotherone(self, *args):
138          """ 
139          :Nr: 2 
140          :Title: 2. Button from Second Menu 
141          :args: $name 
142          """
143          print("This is method '%s' from class '%s'" %
144                (args[0], self.__class__.__name__))
145  
146      def BUTTONnext(self, *args):
147          """ 
148          :title: Third Menu >>> 
149          """
150          self.add(ButtonMenu_2)
151  
152      def BUTTONback(self, *args):
153          """ 
154          :title: <<< Back to First Menu 
155          """
156          self.back()
157  
158  
159  class ButtonMenu_2(MenuClass):
160      def BUTTON_third_test(self, *args):
161          """ 
162          :Nr: 1 
163          :Title: Third menu #1 
164          :args: $index 
165          """
166          print("This is ButtonMenu_2 button nr.", args[0])
167  
168      def BUTTON_third_without_index(self, *args):
169          """ 
170          The first free slot will be assigned to this button. 
171          :Title: Without index 
172          :args: $index 
173          """
174          print("This is ButtonMenu_2 button nr.", args)
175  
176      def BUTTON_Next(self, *args):
177          """ 
178          No title, no number. 
179          """
180          self.add(ButtonMenu_3)
181  
182      def BUTTONback(self, *args):
183          """ 
184          :title: <<< Back to Second Menu 
185          :nr: 5 # Here we overwrite the automatic index generation. 
186          """
187          self.back()
188  
189  
190  class ButtonMenu_3(MenuClass):
191      def setup(self):
192          self.clearButtons()
193          self.displayButtons()
194  
195      def BUTTON_1_fromMethodName(self, *args):
196          """ 
197          Button-nr is 1, defined in method-name (part after first "_"). 
198          Title of this button is "fromMethodName", defined in method-name (part after last "_") 
199          :args: $index 
200          """
201          print("Button nr. is %s. Button-nr is defined in method-name." % args[0])
202  
203      def BUTTON_withoutNumber(self, *args):
204          """ 
205          :args: $index 
206          """
207          print("Button-nr (%s) automatically generated.Button-name defined in method-name." % args[0])
208  
209      def BUTTON_10_GoBack(self, *args):
210          """ 
211          Index nr and title defined in method-name 
212          """
213          self.back()
214  
215  
216  # Call the first Button Menu to start the show.
217  MainButtons()
218