To clip the value along the boundaries – check if the value goes beyond the specified limits, return the value itself, and if it goes out – the maximum possible boundary value, we can use a very simple function.
Let’s define the clipping function:
1 2 3 4 |
def clip(value, _min=0.0, _max=1.0): value = min(value, _max) value = max(value, _min) return value |
In this function, we have set the default bounds from 0 to 1.
For example, if we need to move an object, but not more than within the boundaries of the coordinate area from 0 to 1, we can use this function with default parameters:
1 2 3 4 5 6 7 8 9 |
move_to_x = 1 move_to_y = 2 move_to_z = 0 bpy.context.object.location = ( clip(move_to_x), clip(move_to_y), clip(move_to_z) ) |
Even though we set the Y coordinate to 2, with our clipping function the active object will move to the point with only coordinates (1.0, 1.0, 0.0).
Clipping boundaries can be set in the parameters, if they differ from the default:
1 2 3 4 5 |
bpy.context.object.location = ( clip(move_to_x, _min=0.0, _max=1.0), clip(move_to_y, _min=0.0, _max=1.5), clip(move_to_z, _min=0.0, _max=1.0) ) |