forked from franMarz/TexTools-Blender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathop_uv_crop.py
94 lines (69 loc) · 3 KB
/
op_uv_crop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import bpy
import bmesh
from mathutils import Vector
from . import utilities_uv
from . import utilities_ui
class op(bpy.types.Operator):
bl_idname = "uv.textools_uv_crop"
bl_label = "Crop"
bl_description = "Crop UV area to selected UV faces"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
if bpy.context.area.ui_type != 'UV':
return False
if not bpy.context.active_object:
return False
if bpy.context.active_object.type != 'MESH':
return False
if bpy.context.active_object.mode != 'EDIT':
return False
if not bpy.context.object.data.uv_layers:
return False
if bpy.context.scene.tool_settings.use_uv_select_sync:
return False
return True
def execute(self, context):
crop(self, context)
return {'FINISHED'}
def crop(self, context, distort=False, selection=None):
selection_mode = bpy.context.scene.tool_settings.uv_select_mode
selected_obs = [ob for ob in bpy.context.selected_objects if ob.type == 'MESH']
# Clean selection so that only entirely selected UV faces remain selected
bpy.ops.uv.select_split()
if len(selected_obs) <= 1:
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()
if selection is None:
selection = utilities_uv.get_selected_uv_faces(bm, uv_layers)
if not selection:
return {'CANCELLED'}
boundsAll = utilities_uv.get_BBOX(selection, bm, uv_layers)
elif len(selected_obs) > 1:
all_ob_bounds = utilities_uv.multi_object_loop(utilities_uv.getSelectionBBox, need_results=True)
if not any(all_ob_bounds):
return {'CANCELLED'}
boundsAll = utilities_uv.get_BBOX_multi(all_ob_bounds)
prepivot = bpy.context.space_data.pivot_point
precursor = bpy.context.space_data.cursor_location.copy()
bpy.context.space_data.pivot_point = 'CURSOR'
bpy.context.space_data.cursor_location = (0.0, 0.0)
padding = utilities_ui.get_padding()
# Scale to fit bounds
scale_u = (1.0-padding) / boundsAll['width']
scale_v = (1.0-padding) / boundsAll['height']
if not distort:
scale_u = scale_v = min(scale_u, scale_v)
bpy.ops.transform.resize(value=(scale_u, scale_v, 1), constraint_axis=(False, False, False), mirror=False, use_proportional_edit=False)
# Reposition
delta_position = Vector((padding/2 - scale_u*boundsAll['min'].x, padding/2 - scale_v*boundsAll['min'].y, 0))
udim_tile, column, row = utilities_uv.get_UDIM_tile_coords(bpy.context.active_object)
if udim_tile != 1001:
delta_position += Vector((column, row, 0))
bpy.ops.transform.translate(value=delta_position, mirror=False, use_proportional_edit=False)
bpy.context.space_data.pivot_point = prepivot
bpy.context.space_data.cursor_location = precursor
# Workaround for selection not flushing properly from loops to EDGE Selection Mode, apparently since UV edge selection support was added to the UV space
bpy.ops.uv.select_mode(type='VERTEX')
bpy.context.scene.tool_settings.uv_select_mode = selection_mode
bpy.utils.register_class(op)