In most cases, UV elements (vertices, edges, faces, islands) can be freely moved anywhere in the UV Edit area. However, sometimes it is undesirable to move the UV beyond the boundaries of the base UV borders, for example, to ensure that the UV does not “move away” when baking it into atlases.
Blender provides a simple and effective method that blocks the movement of UV elements beyond the base boundaries.
To enable it, select the “UV” item in the main menu of the UV Edit area and check the “Constrain to Image Bounds” checkbox.
After that, when moving the UV elements, they will stop at the base boundary and not move beyond it.
To restore free movement of the UV, uncheck the checkbox.
This mode can also be enabled/disabled using the Blender Python API.
First, we need to get a pointer to the UV Edit area.
1 2 3 4 |
area = next((_area for _area in bpy.context.screen.areas if _area.type == 'IMAGE_EDITOR' and _area.ui_type == 'UV'), None) print(area, area.type, area.ui_type) # <bpy_struct, Area at 0x000001B07AD873E0> IMAGE_EDITOR UV |
Since the UV Edit area belongs to the general “IMAGE_EDITOR” type, to be sure to get a pointer to the UV editor, we additionally check the “ui_type” subtype, which must be equal to “UI”.
Having received a pointer to the area, we can use it to get a pointer to the required “lock_bounds” property. Check its current state:
1 2 3 |
area.spaces[0].uv_editor.lock_bounds # False |
The value is False, which means that the option is not enabled (the checkbox is not checked).
Let’s enable it (check the checkbox) by assigning a True value to the property.
1 |
area.spaces[0].uv_editor.lock_bounds = True |
To disable blocking mode (uncheck the checkbox), assign the “lock_bounds” property the False value.
1 |
area.spaces[0].uv_editor.lock_bounds = False |