Python Reference Guide
 
Loading...
Searching...
No Matches
ComplexTools\ActionScriptMgr.py
1# Copyright 2009 Autodesk, Inc. All rights reserved.
2# Use of this software is subject to the terms of the Autodesk license agreement
3# provided at the time of installation or download, or which otherwise accompanies
4# this software in either electronic or hard copy form.
5#
6# Script description:
7# Allow edition of MoBu ActionScript.txt that maps a Python action to a script. To change
8# the shortcut that is mapped to an action use the KeyboardMapper.py script (or edit
9# a keyboard file).
10#
11# Topic: FBConfigFile, FBSpread, FBConfigFile
12#
13
14from pyfbsdk import *
15from pyfbsdk_additions import *
16
17import os
18import os.path
19
20TOOL_NAME = "Action Script Manager"
21ACTION_SCRIPT_PATH = os.path.join(FBSystem().ConfigPath, "Scripts")
22ACTION_SCRIPT_FILE = os.path.join(ACTION_SCRIPT_PATH, "ActionScript.txt")
23
24class ActionScriptMgr(FBTool):
25 def RowClicked(self,control,event):
26 # Populate path edit box
27 if self.path_spread.GetRow(event.Row).RowSelected:
28 self.current_row = event.Row
29 else:
30 self.current_row = -1
31 path = self.path_spread.GetCellValue(event.Row, 0)
32
33 # Do not update the path edit if the selected row has no path. It makes
34 # path manipulation easier
35 if path:
36 self.path_edit.Text = path
37
38 def Browse(self,control,event):
39 filepopup = pyfbsdk.FBFilePopup()
40 filepopup.Caption = "Select a script"
41 filepopup.Style = pyfbsdk.FBFilePopupStyle.kFBFilePopupOpen
42 filepopup.Filter = "*.py"
43 result = filepopup.Execute()
44 if result:
45 self.path_edit.Text = os.path.join(filepopup.Path,filepopup.FileName)
46 del filepopup
47
48 def Add(self,control,event):
49 if self.current_row == -1:
50 return
51 self.path_spread.SetCellValue(self.current_row,0,self.path_edit.Text)
52 self.UpdateScript(self.current_row)
53
54 def Remove(self,control,event):
55 # Unbind the selected row's path
56 if self.current_row == -1:
57 return
58 self.path_spread.SetCellValue(self.current_row,0,"")
59 self.UpdateScript(self.current_row)
60
61 def UpdateScript(self,row):
62 config = FBConfigFile("ActionScript.txt",ACTION_SCRIPT_PATH)
63
64 spread_row = self.path_spread.GetRow(row)
65 key = spread_row.Caption
66 value = self.path_spread.GetCellValue(row,0)
67 if value:
68 # Convert it to relative path as it is more portable if
69 # the script is inside MotionBuilder tree
70 value = os.path.relpath(value,ACTION_SCRIPT_PATH)
71 config.Set("ScriptFiles",key,value)
72
73 def CellChanged(self,control,event):
74 # User has edited a path by modifying the cell himself: update the relevant binding
75 self.UpdateScript(event.Row)
76
77 def Populate(self):
78 # All relative paths inside ActionScript.txt are relative to ActionScrip.txt path
79 # so set the current working dir as ACTION_SCRIPT_PATH
80 old_cwd = os.getcwd()
81 os.chdir(ACTION_SCRIPT_PATH)
82
83 self.current_row = -1
84 self.nb_row = 0
85 config = FBConfigFile("ActionScript.txt",ACTION_SCRIPT_PATH)
86 for i in range(12):
87 key = "Script%d" % (i + 1)
88 value = config.Get("ScriptFiles",key)
89 self.path_spread.RowAdd(key,self.nb_row)
90 if value:
91 self.path_spread.SetCellValue(self.nb_row,0,os.path.abspath(value))
92 self.nb_row += 1
93 os.chdir(old_cwd)
94
95 def __init__(self):
96 # init base class
97 FBTool.__init__(self,TOOL_NAME)
98
99 FBAddTool(self)
100
101 x = FBAddRegionParam(0,FBAttachType.kFBAttachLeft,"")
102 y = FBAddRegionParam(0,FBAttachType.kFBAttachTop,"")
103 w = FBAddRegionParam(0,FBAttachType.kFBAttachRight,"")
104 h = FBAddRegionParam(0,FBAttachType.kFBAttachBottom,"")
105 self.AddRegion("main","main", x, y, w, h)
106
107 vbox = FBVBoxLayout()
108 self.SetControl("main",vbox)
109
110 widget_height = 30
111 button_width = 60
112
113 l = FBLabel()
114 l.Caption = "Action Scripts"
115 vbox.Add(l, widget_height)
116
117 self.path_spread = FBSpread()
118 self.path_spread.Caption = "Actions"
119 self.path_spread.ColumnAdd("Path")
120 self.path_spread.GetColumn(0).Width = 550
121 self.path_spread.OnRowClick.Add(self.RowClicked)
122 self.path_spread.OnCellChange.Add(self.CellChanged)
123 vbox.AddRelative(self.path_spread,1.0)
124
125 hbox = FBHBoxLayout()
126
127 self.path_edit = FBEdit()
128 hbox.AddRelative(self.path_edit,1.0)
129
130 b = FBButton()
131 b.Caption = "Browse..."
132 b.OnClick.Add(self.Browse)
133 hbox.Add(b, button_width)
134
135 vbox.Add(hbox,widget_height)
136
137 hbox = FBHBoxLayout()
138 b = FBButton()
139 b.Caption = "Set"
140 b.OnClick.Add(self.Add)
141 hbox.Add(b, button_width)
142
143 b = FBButton()
144 b.Caption = "Remove"
145 b.OnClick.Add(self.Remove)
146 hbox.Add(b, button_width)
147
148 # Notify user that motionbuiler reads the ActionScript.txt file on startup only.
149 l = FBLabel()
150 l.Caption = "You will need to restart MotionBuilder for changes to takes place!"
151 l.Style = FBTextStyle.kFBTextStyleBold
152 hbox.Add(l, 400)
153
154 vbox.Add(hbox,widget_height)
155
156 self.StartSizeX = 800
157 self.StartSizeY = 425
158
159 # Populate Datas
160 self.Populate()
161
162# This call is only useful in Development to ensure a recreation of the tool each time
163#FBDestroyToolByName(TOOL_NAME)
164
165# this snippet of code ensure our tool will only be created once,
166# even if this script is "imported" from other scripts.
167if TOOL_NAME in FBToolList:
168 tool = FBToolList[TOOL_NAME]
169 ShowTool(tool)
170else:
171 tool=ActionScriptMgr()
172 ShowTool(tool)
173 # This call is only useful in Development to ensure we show the tool when created
174 #ShowTool(tool)
175
176
177
Used to create and manage buttons in a user interface.
Definition: pyfbsdk_generated.h:2442
Interface to the application config files.
Definition: pyfbsdk_generated.h:4732
Text edit box.
Definition: pyfbsdk_generated.h:5998
Text label.
Definition: pyfbsdk_generated.h:9829
Base spreadsheet class.
Definition: pyfbsdk_generated.h:17486
Provides access to the underlying system, and the MotionBuilder scene.
Definition: pyfbsdk_generated.h:18771
Tool class.
Definition: pyfbsdk_generated.h:20040
File Popup (for open/save).
Definition: pyfbsdk.h:9428