Python Reference Guide
 
Loading...
Searching...
No Matches
UI\SafeToolCreationExample.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# Shows how to create a tool that will be only instantiated once even if dragged multiple
8# times in the Viewer.
9#
10# Topic: FBTool
11#
12
13from pyfbsdk import *
14import pyfbsdk_additions
15
16# Button creation
17def BtnCallback(control, event):
18 print(control.Caption, " has been clicked!")
19
20def PopulateLayout(mainLyt):
21 pass
22
23toolname = "Bullet Proof"
24
25def CreateTool():
26 """
27 Tool creation function. Everything about the tool is created here.
28 So if this script is executed multiple times and we only call this function once
29 there won't be multiple instantiation of this tool.
30 """
32 t.StartSizeX = 400
33 t.StartSizeY = 200
34 PopulateLayout(t)
35 return t
36
37# Tool Developement
38# Ideally, when a tool has been created users should put it in PythonStartup folder and add this tool
39# to a custom layout. This way a tool would be integrated seemlessly in MotionBuilder.
40#
41# some users might prefer to "drag and drop" a script in the viewer to create a tool. What is wrong
42# with this approach is that if the script gets executed multiple times, the tool gets destroy and recreated.
43# This has the following problems:
44# 1- This means, the tool state gets wiped out.
45# 2- If some particular code must be called once in a MotionBuilder run and at tool initialization it won't work
46# 3- If you register some callbacks and they are not removed you might end up with callbacks to your deleted tool
47# which will bring a "UnboundWrapper Exception".
48#
49# If a user prefer to to use a Drag and Drop mecanism to pop a tool here is my suggestion:
50# 1- Ensure the tool is created once at the first script execution.
51# 2- When the script is executed again, "Show" the already existing tool
52
53# The following code implement that behavior:
54if toolname in pyfbsdk_additions.FBToolList:
55 # Each time you rexecute this script (by dragging in viewer or execute from python console)
56 # You will "show" the tool instead of recreating this tool.
57 print("show me")
58 ShowToolByName(toolname)
59else:
60 # The first time this script is executed, it gets created.
61 print("create me")
62 t = CreateTool()
63
64 # Comment this line if you want to put this script in PythonStartup folder.
65 ShowTool(t)
66
FBCreateUniqueTool(name)
Create a Tool with a unique name.