1
0
mirror of https://github.com/drewcassidy/TexTools-Blender synced 2024-06-11 00:26:53 +00:00
Blender-TexTools/op_uv_crop.py

69 lines
1.8 KiB
Python
Raw Normal View History

2019-06-08 23:42:50 +00:00
import bpy
import bmesh
import operator
from mathutils import Vector
from collections import defaultdict
from math import pi
from . import utilities_uv
from . import utilities_ui
2019-12-24 19:59:46 +00:00
2019-06-08 23:42:50 +00:00
class op(bpy.types.Operator):
2019-12-18 20:53:16 +00:00
bl_idname = "uv.textools_uv_crop"
bl_label = "Crop"
bl_description = "Crop UV area to selected UV faces"
bl_options = {'REGISTER', 'UNDO'}
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
@classmethod
def poll(cls, context):
if not bpy.context.active_object:
return False
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
if bpy.context.active_object.type != 'MESH':
return False
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Only in Edit mode
2019-12-18 20:53:16 +00:00
if bpy.context.active_object.mode != 'EDIT':
return False
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Only in UV editor mode
2019-12-18 20:53:16 +00:00
if bpy.context.area.type != 'IMAGE_EDITOR':
return False
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
# Requires UV map
2019-12-18 20:53:16 +00:00
if not bpy.context.object.data.uv_layers:
return False
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
return True
2019-12-24 19:59:46 +00:00
2019-12-18 20:53:16 +00:00
def execute(self, context):
crop(self, context)
return {'FINISHED'}
2019-06-08 23:42:50 +00:00
def crop(self, context):
2019-12-24 19:59:46 +00:00
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
padding = utilities_ui.get_padding()
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Scale to fit bounds
bbox = utilities_uv.getSelectionBBox()
scale_u = (1.0-padding) / bbox['width']
scale_v = (1.0-padding) / bbox['height']
scale = min(scale_u, scale_v)
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
bpy.ops.transform.resize(value=(scale, scale, scale), constraint_axis=(
False, False, False), mirror=False, use_proportional_edit=False)
2019-06-08 23:42:50 +00:00
2019-12-18 20:53:16 +00:00
# Reposition
bbox = utilities_uv.getSelectionBBox()
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
delta_position = Vector((padding/2, 1-padding/2)) - \
Vector((bbox['min'].x, bbox['min'].y + bbox['height']))
2019-12-18 20:53:16 +00:00
bpy.ops.transform.translate(value=(delta_position.x, delta_position.y, 0))
2019-06-08 23:42:50 +00:00
2019-12-24 19:59:46 +00:00
bpy.utils.register_class(op)