2
$\begingroup$

I'd like to add a poll function to my BoolProperty, but I don't understand how it works.

Is it similar to the poll function of an operator? What represents the "object" argument of this function?

EDIT after Teck-freak answer:

enter image description here

$\endgroup$
3
  • $\begingroup$ There is no poll function for Properties. You can hide the property in the layout or (even better) block the layout (layout.enable=False) so it can be seen but not used. You can also fake the behaviour within the update-funktion, but you'll need a second prop $\endgroup$
    – Teck-freak
    Dec 6, 2017 at 23:12
  • $\begingroup$ With respect to your image, that was likely an old version of the API...you should post the link that you used. If you look at the current version (link in my answer), there is no such function. $\endgroup$
    – JakeD
    Dec 7, 2017 at 23:29
  • $\begingroup$ The image come from the 2.79 API documentation docs.blender.org/api/2.79/… $\endgroup$
    – pistiwique
    Dec 8, 2017 at 13:40

2 Answers 2

4
$\begingroup$

There is no poll function for Properties.

HOWEVER: You can skip the property in the layout or (even better) block the layout (layout.enable=False) so it can be seen but not used. You can also fake the behaviour within the update-funktion, but you'll need a second property. Finally you can define getter, and setter functions for your property.

BLOCK THE LAYOUT:

row = layout.row()
row.enable = False
row.prop(self, 'MyBool') #this will appear grayed out

GETTER & SETTER:

def get_MyBool(self, context):
    return self.MyBool_internal
def set_MyBool(self, context, value):
    if not self.dont_touch_MyBool:
        self.MyBool_internal = value
MyBool_internal = bpy.props.BoolProperty() # This is where its actually stored
MyBool = bpy.props.BoolProperty( # This is for your user-interface
         name='MyBool', get=get_MyBool, set=set_MyBool)

ABUSING THE UPDATE-FUNCTION: (Documentary only. Avoid if possible.)

def update_MyBool(self, context):
    if self.dont_touch_MyBool: # condition to check if changing mybool is allowed
        self.MyBool = self.MyBool_Backup
    else:
        self.MyBool_Backup = self.MyBool
MyBool = bpy.props.BoolProperty(name='MyBool', update=update_MyBool)

You can read more about bpy.props here

BLOCKING CERTAIN INPUT:

Let's say you'd like to define an integer that may NOT accept multiples of 2 or 3.

# this will only be called if a valid entry is made
def upd_MyInt(self, context):
    print('Entry accepted')
# This is where its actually stored
MyInt_Internal = bpy.props.IntProperty()

# This is for your user-interface
def get_MyInt(self, context):
    return self.MyInt_internal
def set_MyInt(self, context, value):
    if value%2 and value%3:
        self.MyInt_internal = value
MyInt = bpy.props.IntProperty(get=get_MyInt, set=set_MyInt)
$\endgroup$
7
  • $\begingroup$ Sorry, I misspoke. Speaking of the poll function, I wanted to talk about the function that the poll parameter of the property expects. I edited my post to add an image $\endgroup$
    – pistiwique
    Dec 7, 2017 at 6:14
  • $\begingroup$ I have litterally NO IDEA where you got that part. Properties have no poll function . However you can achieve what you want by defining your property with getters, setters, and a second property in the background. If you state more about your goal, I might be able to help you. $\endgroup$
    – Teck-freak
    Dec 7, 2017 at 15:05
  • $\begingroup$ It's weird because it's from the Blender API. But it doesn't matter, I sould have to get what I want by "blocking" the layout. Thanks for your help :) $\endgroup$
    – pistiwique
    Dec 7, 2017 at 17:53
  • $\begingroup$ @pistiwique Ah I see. 2.79 is fairly new and we tend to program with the second or third-newest version instead, to avoid bugs and crashes. It seems the poll-function would take the object the property binds to as first, and your value as second input. $\endgroup$
    – Teck-freak
    Dec 8, 2017 at 23:33
  • $\begingroup$ Just as a side note, the Accepted answer above (Teck freak) gives this error in Blender 2.79: TypeError: set keyword: expected a function taking 2 arguments, not 3 $\endgroup$
    – N. Wells
    Jul 3, 2018 at 15:10
1
$\begingroup$

From: https://blender.stackexchange.com/a/19431/28015

[P]oll, checked before running the operator, which will never run when poll fails, used to check if an operator can run, menu items will be greyed out and if key bindings should be ignored.

A poll operator method would not make sense for a property. You may be looking for the update method.

update (function) – Function to be called when this value is modified, This function must take 2 values (self, context) and return None. Warning there are no safety checks to avoid infinite recursion.

$\endgroup$
1
  • $\begingroup$ Using the poll for pointer properties. $\endgroup$
    – batFINGER
    Jul 3, 2018 at 15:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .