0
$\begingroup$

I have been googling this and can't find an answer. I defined a float property that creates a number spinner in my UI that allows me to move an object back and forth. I would like to clamp this value dynamically so I can't use the built-in FloatProperty clamp values. The code below should work, yet it seems to reset the value to the default value when it should get clamped.

def SetPos(self, context):
    self['left'] = Clamp(self['left'],0,1)

def register():
    bpy.types.Object.left = FloatProperty(name="left", default=0.34, unit="LENGTH", update=SetPos)

Another issue is that I don't see how to change an enumproperty through script. I can see in the python console it just assigns the string value to the prop like
bpy.context.object.myEnum = 'BLA' but when I do an assignment like that myself, the UI does not update.

$\endgroup$
3
  • 1
    $\begingroup$ Hello, could you post your Clamp method ? $\endgroup$
    – Gorgious
    Feb 1, 2021 at 7:58
  • $\begingroup$ Also, why don't you use the min and max fields of the properties ? $\endgroup$
    – Gorgious
    Feb 1, 2021 at 9:06
  • $\begingroup$ Setter / getter example. blender.stackexchange.com/a/134310/15543 see also the _RNA_UI dictionary of a custom prop, which can be dynamically altered if not in use when not currently defined as a bpy.props property. $\endgroup$
    – batFINGER
    Feb 1, 2021 at 15:07

1 Answer 1

0
$\begingroup$

The update method will be invoked every time you try to modify the value of this property, like a callback function. The manual do warn about infinite loop when your update method constantly do something to props.

As your code, each time you update a new value, you set the value instead of check the value then set. So each time you invoke update method, blender will trigger another update function since you "set" value that is supervised by Blender system.

Not sure how to avoid this behavior but you might check the condition first then change the value:

def SetPos(self, context):
    if need_clamp(self['left']):
        self['left'] = Clamp(self['left'],0,1)
    # so next time this method will do nothing

Or just use setter and getter function: Property Definitions (bpy.props) — Blender Python API

$\endgroup$
3

You must log in to answer this question.

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