'NoneType' Error getting button to call a function

'NoneType' Error getting button to call a function

Anonymous
Not applicable
1,332 Views
4 Replies
Message 1 of 5

'NoneType' Error getting button to call a function

Anonymous
Not applicable

Hey guys, very new scripting/python and was hoping to get some help.

Ive been trying to make a simple window that holds a few buttons with commonly used assets. 

First asset Im trying to tackle is having a button add a parent contraint.

 

I can get the window to pop up with my button and just cant seen to figure out how to call the "CreateCons" function using the button. Pasting my code below. 

whenI run it I currently get a "TypeError: 'NoneType' object is not callable"

 

If anyone can steer me in the right direction, I would appreciate it.

Thanks in adavance.

 

from pyfbsdk import *
from pyfbsdk_additions import *

#------------------Add Constraint---------------------------------

def CreateCons (Name, Type, Active):
    myManager = FBConstraintManager()
    myCons = myManager.TypeCreateConstraint(Type)
    myCons.Name = Name
    myCons.Active = Active
   
#--------------------------------------------------------------------------------

def PopulateLayout(mainLyt):
    x = FBAddRegionParam(0,FBAttachType.kFBAttachLeft,"")
    y = FBAddRegionParam(5,FBAttachType.kFBAttachTop,"")
    w = FBAddRegionParam(0,FBAttachType.kFBAttachRight,"")
    h = FBAddRegionParam(25,FBAttachType.kFBAttachNone,"")
    mainLyt.AddRegion("main","main", x, y, w, h)
    lyt = FBHBoxLayout()
    mainLyt.SetControl("main",lyt)
    
    b = FBButton()
    b.Caption = "add constraint"
    b.Style = FBButtonStyle.kFB2States
    b.Look = FBButtonLook.kFBLookColorChange
    b.Justify = FBTextJustify.kFBTextJustifyCenter
    b.SetStateColor(FBButtonState.kFBButtonState0,FBColor(0.20, 0.0, 0.45))
    b.SetStateColor(FBButtonState.kFBButtonState1,FBColor(0.20, 0.0, 0.45))
    lyt.Add(b,90)

    b.OnClick.Add(CreateCons("gunConstraint", 5, True))

def CreateTool():
    # Tool creation will serve as the hub for all other controls
    t = FBCreateUniqueTool("Tools")
    t.StartSizeX = 305
    t.StartSizeY = 70
    PopulateLayout(t)
    ShowTool(t)

CreateTool()

 

 

 

0 Likes
Accepted solutions (1)
1,333 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable

Hello,

 

You cannot actually pass your own parameter via "CreateCons", so the correct call is like this:

 

b.OnClick.Add(CreateCons)

 

MotionBuilder will automatically pass CreateCons(theButton, theEvent)

 

So you can define your function like this (using the mobu naming convention, but you can call the parameters whatever you want):

 

def CreateCons (pControl, pEvent):

 

 

So now you're thinking that's all great but how do I still pass my three parameters, so you can either do one of two things:

 

1) Save that data in a global variable. You can search the web for "Python Global Variables" for more information on this topic.

 

2) But a better choice is to use the Python language to do this by doing this:

 

    b = FBButton()
    b.Caption = "add constraint"
    b.Style = FBButtonStyle.kFB2States
    b.Look = FBButtonLook.kFBLookColorChange
    b.Justify = FBTextJustify.kFBTextJustifyCenter
    b.SetStateColor(FBButtonState.kFBButtonState0,FBColor(0.20, 0.0, 0.45))
    b.SetStateColor(FBButtonState.kFBButtonState1,FBColor(0.20, 0.0, 0.45))
    b.ConstraintName = "gunConstraint" # My Added property
    b.Constraint = 5 # My Added property
    b.ContraintActive = True # My Added property
    lyt.Add(b,90)

 

So now in my CreateCons function, I just need to get this date off the control like this:

def CreateCons (pControl, pEvent):
    myManager = FBConstraintManager()
    myCons = myManager.TypeCreateConstraint(pControl.Constraint)
    myCons.Name = pControl.ContraintName
myCons.Active = pControl.ConstraintActive

 

Remember in Python everything is kind of like a virtual in other languages, so you can just add functions, properties on the fly, and they stay with that instance of the FBButton object, this is greate for passing data around when you can actually use the parameter mechanism like you normall would.

 

Hopefully this makes sense,

 

Kristine

 

 

 

CreateCons

0 Likes
Message 3 of 5

Anonymous
Not applicable
Accepted solution

I should mention, you need to pick a unique name for your property to add on to your class instance, because if you don't then you would over write something that maybe important on that class. You can just look in the class FBButton documentation for what could be unique 🙂

 

~Kristine

Message 4 of 5

Anonymous
Not applicable
Amazing Kristine, this worked. Been trying to figure this out for some time now. There were a few typos that were bugging out (constraint was misspelled) but I got it to work. still need to wrap my around some of these concepts.
thanks for helping out.
0 Likes
Message 5 of 5

Anonymous
Not applicable

Opps, sorry about the grammar, yes this line:

 

    b.ContraintActive = True # My Added property

 

should be:

 

    b.ConstraintActive = True # My Added property

 

and this line:

 

myCons.Name = pControl.ContraintName

 

Should be:

 

myCons.Name = pControl.ConstraintName

 

Or really whatever you call it as long as you are consistant (unlike me) 😉 😉

 

~Kristine

 

 

0 Likes