Skip to content

Commit

Permalink
Extended support for multi-object UV editing
Browse files Browse the repository at this point in the history
- Align, Sort, Crop, Fill and Select Similar now work with multiple objects while editing the UVs.
- Now, Select Overlap behaves like in previous versions: all but one of the overlapping islands are selected, making it posible to manually fix the overlapping by using this operator in conjunction with manually moving the selected islands.
- Further optimization of the 90º Rotate Island operator.
- Some cleanup and minor corrections in op_smoothing_uv_islands, op_select_islands_flipped, op_select_islands_outline, op_rectify, settings, __init__ and LICENSE.txt
  • Loading branch information
franMarz authored Dec 15, 2020
1 parent 767de25 commit ff036d6
Show file tree
Hide file tree
Showing 15 changed files with 297 additions and 304 deletions.
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
# Copyright (C) <2014> <Reslav Hollos>
# https://github.com/JoseConseco/UvSquares/blob/master/uv_squares.py
#
# Current maintainers Sav Martin, FranMarz.
# Current maintainers Sav Martin, franMarz.
#
# Icons art. DavidRivera (activemotionpictures)
2 changes: 1 addition & 1 deletion __init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
bl_info = {
"name": "TexTools",
"description": "Professional UV and Texture tools for Blender.",
"author": "renderhjs, (Port to 2.80 by Sav Martin), FranMarz",
"author": "renderhjs, (Port to 2.80 by Sav Martin), franMarz",
"version": (1, 4, 00),
"blender": (2, 80, 0),
"category": "UV",
Expand Down
50 changes: 25 additions & 25 deletions op_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from mathutils import Vector
from collections import defaultdict
from math import pi, sqrt
from numpy import median


from . import utilities_uv

Expand Down Expand Up @@ -34,7 +32,7 @@ def poll(cls, context):

#Requires UV map
if not bpy.context.object.data.uv_layers:
return False #self.report({'WARNING'}, "Object must have more than one UV map")
return False

# Not in Synced mode
if bpy.context.scene.tool_settings.use_uv_select_sync:
Expand All @@ -44,29 +42,32 @@ def poll(cls, context):


def execute(self, context):
all_ob_bounds = utilities_uv.multi_object_loop(utilities_uv.getSelectionBBox, need_results=True)

select = False
for ob_bounds in all_ob_bounds:
if len(ob_bounds) > 0 :
select = True
break
if not select:
return {'CANCELLED'}

boundsAll = utilities_uv.getMultiObjectSelectionBBox(all_ob_bounds)
utilities_uv.multi_object_loop(align, context, self.direction, boundsAll)

align(context, self.direction)
return {'FINISHED'}


def align(context, direction):
#Store selection
utilities_uv.selection_store()
def align(context, direction, boundsAll):

if bpy.context.tool_settings.transform_pivot_point != 'CURSOR':
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
prepivot = bpy.context.space_data.pivot_point
bpy.context.space_data.pivot_point = 'CURSOR'

#B-Mesh
obj = bpy.context.active_object
bm = bmesh.from_edit_mesh(obj.data);
uv_layers = bm.loops.layers.uv.verify();

if len(obj.data.uv_layers) == 0:
print("There is no UV channel or UV data set")
return
bm = bmesh.from_edit_mesh(obj.data)
uv_layers = bm.loops.layers.uv.verify()

# Collect BBox sizes
boundsAll = utilities_uv.getSelectionBBox()
center_all = boundsAll['center']

mode = bpy.context.scene.tool_settings.uv_select_mode
Expand All @@ -80,19 +81,19 @@ def align(context, direction):

if direction == "bottom":
delta = boundsAll['min'] - bounds['min']
utilities_uv.move_island(island, 0,delta.y)
utilities_uv.move_island(island, 0, delta.y)

elif direction == "top":
delta = boundsAll['max'] - bounds['max']
utilities_uv.move_island(island, 0,delta.y)
utilities_uv.move_island(island, 0, delta.y)

elif direction == "left":
delta = boundsAll['min'] - bounds['min']
utilities_uv.move_island(island, delta.x,0)
utilities_uv.move_island(island, delta.x, 0)

elif direction == "right":
delta = boundsAll['max'] - bounds['max']
utilities_uv.move_island(island, delta.x,0)
utilities_uv.move_island(island, delta.x, 0)

elif direction == "center":
delta = Vector((center_all - center))
Expand All @@ -106,9 +107,8 @@ def align(context, direction):
delta = Vector((center_all - center))
utilities_uv.move_island(island, delta.x, 0)


else:
print("Unkown direction: "+str(direction))
print("Unknown direction: "+str(direction))


elif mode == 'EDGE' or mode == 'VERTEX':
Expand All @@ -132,8 +132,8 @@ def align(context, direction):

bmesh.update_edit_mesh(obj.data)

#Restore selection
# utilities_uv.selection_restore()
bpy.context.space_data.pivot_point = prepivot


bpy.utils.register_class(op)

Expand Down
102 changes: 33 additions & 69 deletions op_island_align_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,55 +51,46 @@ def poll(cls, context):


def execute(self, context):
main(context, self.is_vertical, self.padding)
utilities_uv.multi_object_loop(main, context, self.is_vertical, self.padding)

all_ob_bounds = utilities_uv.multi_object_loop(utilities_uv.getSelectionBBox, need_results=True)
utilities_uv.multi_object_loop(relocate, context, self.is_vertical, self.padding, all_ob_bounds, ob_num=0)

return {'FINISHED'}


def main(context, isVertical, padding):
print("Executing IslandsAlignSort main {}".format(padding))

#Store selection
utilities_uv.selection_store()

if bpy.context.tool_settings.transform_pivot_point != 'CURSOR':
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'

#Only in Face or Island mode
if bpy.context.scene.tool_settings.uv_select_mode is not 'FACE' or 'ISLAND':
bpy.context.scene.tool_settings.uv_select_mode = 'FACE'
bpy.context.tool_settings.transform_pivot_point = 'CURSOR'
bpy.context.scene.tool_settings.uv_select_mode = 'FACE'

bm = bmesh.from_edit_mesh(bpy.context.active_object.data);
uv_layers = bm.loops.layers.uv.verify();
bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()


boundsAll = utilities_uv.getSelectionBBox()


islands = utilities_uv.getSelectionIslands()
allSizes = {} #https://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value
allBounds = {}

print("Islands: "+str(len(islands))+"x")

bpy.context.window_manager.progress_begin(0, len(islands))

#Rotate to minimal bounds
for i in range(0, len(islands)):
alignIslandMinimalBounds(uv_layers, islands[i])
# Select Island
bpy.ops.uv.select_all(action='DESELECT')
utilities_uv.set_selected_faces(islands[i])

utilities_uv.alignMinimalBounds(uv_layers=uv_layers)

# Collect BBox sizes
bounds = utilities_uv.getSelectionBBox()
allSizes[i] = max(bounds['width'], bounds['height']) + i*0.000001;#Make each size unique
allBounds[i] = bounds;
print("Rotate compact: "+str(allSizes[i]))

bpy.context.window_manager.progress_update(i)

bpy.context.window_manager.progress_end()

allSizes[i] = max(bounds['width'], bounds['height']) + i*0.000001 #Make each size unique
allBounds[i] = bounds

#Position by sorted size in row
sortedSizes = sorted(allSizes.items(), key=operator.itemgetter(1))#Sort by values, store tuples
sortedSizes = sorted(allSizes.items(), key=operator.itemgetter(1)) #Sort by values, store tuples
sortedSizes.reverse()
offset = 0.0
for sortedSize in sortedSizes:
Expand All @@ -113,59 +104,32 @@ def main(context, isVertical, padding):

#Offset Island
if(isVertical):
delta = Vector((boundsAll['min'].x - bounds['min'].x, boundsAll['max'].y - bounds['max'].y));
delta = Vector((boundsAll['min'].x - bounds['min'].x, boundsAll['max'].y - bounds['max'].y))
bpy.ops.transform.translate(value=(delta.x, delta.y-offset, 0))
offset += bounds['height']+padding
else:
print("Horizontal")
delta = Vector((boundsAll['min'].x - bounds['min'].x, boundsAll['max'].y - bounds['max'].y));
delta = Vector((boundsAll['min'].x - bounds['min'].x, boundsAll['max'].y - bounds['max'].y))
bpy.ops.transform.translate(value=(delta.x+offset, delta.y, 0))
offset += bounds['width']+padding


#Restore selection
utilities_uv.selection_restore()


def alignIslandMinimalBounds(uv_layers, faces):
# Select Island
bpy.ops.uv.select_all(action='DESELECT')
utilities_uv.set_selected_faces(faces)

steps = 8
angle = 45; # Starting Angle, half each step

bboxPrevious = utilities_uv.getSelectionBBox()

for i in range(0, steps):
# Rotate right
bpy.ops.transform.rotate(value=(angle * math.pi / 180), orient_axis='Z')
bbox = utilities_uv.getSelectionBBox()

if i == 0:
sizeA = bboxPrevious['width'] * bboxPrevious['height']
sizeB = bbox['width'] * bbox['height']
if abs(bbox['width'] - bbox['height']) <= 0.0001 and sizeA < sizeB:
# print("Already squared")
bpy.ops.transform.rotate(value=(-angle * math.pi / 180), orient_axis='Z')
break;


if bbox['minLength'] < bboxPrevious['minLength']:
bboxPrevious = bbox; # Success
else:
# Rotate Left
bpy.ops.transform.rotate(value=(-angle*2 * math.pi / 180), orient_axis='Z')
bbox = utilities_uv.getSelectionBBox()
if bbox['minLength'] < bboxPrevious['minLength']:
bboxPrevious = bbox; # Success
def relocate(context, isVertical, padding, all_ob_bounds, ob_num=0):
if ob_num > 0 :
offset = 0.0
for i in range(0, ob_num):
if isVertical:
offset += all_ob_bounds[i]['height']+padding
else:
# Restore angle of this iteration
bpy.ops.transform.rotate(value=(angle * math.pi / 180), orient_axis='Z')

angle = angle / 2
offset += all_ob_bounds[i]['width']+padding
if isVertical:
delta = Vector((all_ob_bounds[0]['min'].x - all_ob_bounds[ob_num]['min'].x, all_ob_bounds[0]['max'].y - all_ob_bounds[ob_num]['max'].y))
bpy.ops.transform.translate(value=(delta.x, delta.y-offset, 0))
else:
delta = Vector((all_ob_bounds[0]['min'].x - all_ob_bounds[ob_num]['min'].x, all_ob_bounds[0]['max'].y - all_ob_bounds[ob_num]['max'].y))
bpy.ops.transform.translate(value=(delta.x+offset, delta.y, 0))

if bboxPrevious['width'] < bboxPrevious['height']:
bpy.ops.transform.rotate(value=(90 * math.pi / 180), orient_axis='Z')

bpy.utils.register_class(op)
40 changes: 7 additions & 33 deletions op_island_rotate_90.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,15 @@ def poll(cls, context):


def execute(self, context):
utilities_uv.multi_object_loop(main, context, self.angle)
return {'FINISHED'}


def main(context, angle):
bpy.ops.uv.select_linked()

#Store selection
utilities_uv.selection_store()

bm = bmesh.from_edit_mesh(bpy.context.active_object.data)
uv_layers = bm.loops.layers.uv.verify()

bpy.ops.uv.select_linked()
angle = self.angle
bversion = float(bpy.app.version_string[0:4])
if bversion == 2.80 or bversion == 2.81 or bversion == 2.82 or bversion == 2.90:
angle = -angle
bpy.ops.transform.rotate(value=-angle, orient_axis='Z', constraint_axis=(False, False, False), use_proportional_edit=False)

#Bounds
#bounds_initial = utilities_uv.getSelectionBBox()

# bpy.ops.transform.rotate behaves differently based on the version of Blender on the UV Editor. Not expected to be fixed for every version of master
bversion = float(bpy.app.version_string[0:4])
if bversion == 2.80 or bversion == 2.81 or bversion == 2.82 or bversion == 2.90:
angle = -angle
bpy.ops.transform.rotate(value=-angle, orient_axis='Z', constraint_axis=(False, False, False), use_proportional_edit=False)

#Align rotation to top left|right
# bounds_post = utilities_uv.getSelectionBBox()
# dy = bounds_post['max'].y - bounds_initial['max'].y
# dx = 0
# if angle > 0:
# dx = bounds_post['max'].x - bounds_initial['max'].x
# else:
# dx = bounds_post['min'].x - bounds_initial['min'].x
# bpy.ops.transform.translate(value=(-dx, -dy, 0), constraint_axis=(False, False, False), use_proportional_edit=False)

#Restore selection
utilities_uv.selection_restore()
return {'FINISHED'}


bpy.utils.register_class(op)
12 changes: 6 additions & 6 deletions op_rectify.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def main(square = False, snapToClosest = False):

face_act = bm.faces.active
targetFace = face_act

#if len(bm.faces) > allowedFaces:
# operator.report({'ERROR'}, "selected more than " +str(allowedFaces) +" allowed faces.")
# return
# operator.report({'ERROR'}, "selected more than " +str(allowedFaces) +" allowed faces.")
# return

edgeVerts, filteredVerts, selFaces, nonQuadFaces, vertsDict, noEdge = ListsOfVerts(uv_layers, bm)

Expand All @@ -79,8 +79,8 @@ def main(square = False, snapToClosest = False):
return

cursorClosestTo = CursorClosestTo(filteredVerts)

#line is selected

if len(selFaces) is 0:
if snapToClosest is True:
SnapCursorToClosestSelected(filteredVerts)
Expand Down Expand Up @@ -154,7 +154,7 @@ def ListsOfVerts(uv_layers, bm):
else: isFaceSel = False

allEdgeVerts.extend(facesEdgeVerts)
if isFaceSel:
if isFaceSel:
if len(f.verts) is not 4:
nonQuadFaces.append(f)
edgeVerts.extend(facesEdgeVerts)
Expand All @@ -168,7 +168,7 @@ def ListsOfVerts(uv_layers, bm):
vertsDict[(x, y)].append(luv)

else: edgeVerts.extend(facesEdgeVerts)

noEdge = False
if len(edgeVerts) is 0:
noEdge = True
Expand Down
Loading

0 comments on commit ff036d6

Please sign in to comment.