Create a button

Python scripts are great, they are even better when we can click a button to run them. A small GUI like the image shown (“Do it!” Button) can be very easy to set up.

Thanks to neil3d.com‘s UI Builder Tool, this was valuable in creating a setup script that I could dissect and learn from. His website is a great source of information.

Below I will go over how I learned to set up a Python script that will launch from a button click in MotionBuilder.

'''=================================
|     Written by Victor DeBaie     |
|  http://www.vicdebaie.com/blog/  |
|      vic_debaie@hotmail.com      |
================================='''

from pyfbsdk import *
from pyfbsdk_additions import *

# VAriable "DoIt" as a Button
DoIt = FBButton()

# Define what the "DoIt" button does when clicked
def BtnCallbackDoIt(control, event):
print "do it!"

# Start of the tool window lay out
def PopulateTool(t):
#populate regions here

# Layout for the Button
# the DoIt button's position on the x
x = FBAddRegionParam(15,FBAttachType.kFBAttachNone,"")
# the DoIt button's position on the y
y = FBAddRegionParam(10,FBAttachType.kFBAttachNone,"")
# the DoIt button's width
w = FBAddRegionParam(150,FBAttachType.kFBAttachNone,"")
# the DoIt button's height
h = FBAddRegionParam(65,FBAttachType.kFBAttachNone,"")

t.AddRegion("DoIt","DoIt", x, y, w, h)

t.SetControl("DoIt", DoIt)
DoIt.Visible = True
DoIt.ReadOnly = False
DoIt.Enabled = True
# hover over the button and this msg. will apear
DoIt.Hint = "launches what ever script you are testing - NOT FOR PRODUCTION"
# the text that will appear on the button
DoIt.Caption = "Do it!"
DoIt.State = 0
# the button style - read up on this, there are lots of functions to be had
DoIt.Style = FBButtonStyle.kFBPushButton
# the button's text will be centered
DoIt.Justify = FBTextJustify.kFBTextJustifyCenter
DoIt.Look = FBButtonLook.kFBLookNormal
# this tells the button "when you are clicked go to def BtnCallbackDoIt"
DoIt.OnClick.Add(BtnCallbackDoIt)

def CreateTool():
# the tool window's name
t = FBCreateUniqueTool("Tool")
# the tool window's width
t.StartSizeX = 200
# the tool window's height
t.StartSizeY = 120
PopulateTool(t)
ShowTool(t)
CreateTool()

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.