2023-08-15 14:20:26 +00:00
|
|
|
# SPDX-FileCopyrightText: 2009-2023 Blender Authors
|
2023-06-15 03:09:04 +00:00
|
|
|
#
|
2022-02-10 22:07:11 +00:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
2009-12-16 21:27:07 +00:00
|
|
|
|
2011-08-12 06:57:00 +00:00
|
|
|
from bpy.types import Operator
|
2009-12-16 21:27:07 +00:00
|
|
|
|
2019-10-31 07:00:18 +00:00
|
|
|
from bpy.props import (
|
|
|
|
EnumProperty,
|
|
|
|
)
|
2009-12-17 01:21:55 +00:00
|
|
|
|
2018-09-26 23:41:18 +00:00
|
|
|
STATUS_OK = (1 << 0)
|
|
|
|
STATUS_ERR_ACTIVE_FACE = (1 << 1)
|
2018-09-05 22:25:56 +00:00
|
|
|
STATUS_ERR_NOT_SELECTED = (1 << 2)
|
2018-09-26 23:41:18 +00:00
|
|
|
STATUS_ERR_NOT_QUAD = (1 << 3)
|
2023-06-14 07:28:45 +00:00
|
|
|
STATUS_ERR_MISSING_UV_LAYER = (1 << 4)
|
|
|
|
STATUS_ERR_NO_FACES_SELECTED = (1 << 5)
|
2018-09-26 23:41:18 +00:00
|
|
|
|
2013-01-15 23:15:32 +00:00
|
|
|
|
2023-07-06 02:33:47 +00:00
|
|
|
def extend(obj, EXTEND_MODE, use_uv_selection):
|
2012-11-29 14:02:28 +00:00
|
|
|
import bmesh
|
2023-06-14 07:28:45 +00:00
|
|
|
from .uvcalc_transform import is_face_uv_selected
|
|
|
|
|
2009-12-17 01:21:55 +00:00
|
|
|
me = obj.data
|
2013-01-15 23:15:32 +00:00
|
|
|
|
2012-11-29 14:02:28 +00:00
|
|
|
bm = bmesh.from_edit_mesh(me)
|
2013-01-15 23:15:32 +00:00
|
|
|
|
2012-11-29 14:02:28 +00:00
|
|
|
f_act = bm.faces.active
|
2013-01-15 23:15:32 +00:00
|
|
|
|
2012-11-29 14:02:28 +00:00
|
|
|
if f_act is None:
|
2023-06-14 07:28:45 +00:00
|
|
|
return STATUS_ERR_ACTIVE_FACE # Active face cannot be none.
|
2013-12-12 18:05:21 +00:00
|
|
|
if not f_act.select:
|
2023-06-14 07:28:45 +00:00
|
|
|
return STATUS_ERR_NOT_SELECTED # Active face is not selected.
|
|
|
|
if len(f_act.verts) != 4:
|
|
|
|
return STATUS_ERR_NOT_QUAD # Active face is not a quad
|
|
|
|
uv_act = bm.loops.layers.uv.active # Always use the active UV layer.
|
2024-01-09 05:41:46 +00:00
|
|
|
if uv_act is None:
|
|
|
|
return STATUS_ERR_MISSING_UV_LAYER # Object's mesh doesn't have any UV layers.
|
2023-06-14 07:28:45 +00:00
|
|
|
|
2023-07-06 02:33:47 +00:00
|
|
|
if use_uv_selection:
|
2024-01-09 05:41:46 +00:00
|
|
|
faces = [
|
|
|
|
f for f in bm.faces
|
|
|
|
if f.select and len(f.verts) == 4 and is_face_uv_selected(f, uv_act, False)
|
|
|
|
]
|
|
|
|
else:
|
|
|
|
faces = [
|
|
|
|
f for f in bm.faces
|
|
|
|
if f.select and len(f.verts) == 4
|
|
|
|
]
|
2023-07-06 02:33:47 +00:00
|
|
|
|
2023-06-14 07:28:45 +00:00
|
|
|
if not faces:
|
|
|
|
return STATUS_ERR_NO_FACES_SELECTED
|
|
|
|
|
2024-01-09 05:41:46 +00:00
|
|
|
# Our own local walker.
|
2012-11-29 14:02:28 +00:00
|
|
|
|
2024-01-09 05:41:46 +00:00
|
|
|
def walk_face_init(faces, f_act):
|
|
|
|
# First tag all faces True (so we don't UV-map them).
|
2013-01-08 12:10:53 +00:00
|
|
|
for f in bm.faces:
|
2024-01-09 05:41:46 +00:00
|
|
|
f.tag = True
|
|
|
|
# Then tag faces argument False.
|
|
|
|
for f in faces:
|
|
|
|
f.tag = False
|
|
|
|
# Tag the active face True since we begin there.
|
|
|
|
f_act.tag = True
|
|
|
|
|
|
|
|
def walk_face(f):
|
|
|
|
# All faces in this list must be tagged.
|
|
|
|
f.tag = True
|
|
|
|
faces_a = [f]
|
|
|
|
faces_b = []
|
|
|
|
|
|
|
|
while faces_a:
|
|
|
|
for f in faces_a:
|
|
|
|
for l in f.loops:
|
|
|
|
l_edge = l.edge
|
|
|
|
if (l_edge.is_manifold is True) and (l_edge.seam is False):
|
|
|
|
l_other = l.link_loop_radial_next
|
|
|
|
f_other = l_other.face
|
|
|
|
if not f_other.tag:
|
|
|
|
yield (f, l, f_other)
|
|
|
|
f_other.tag = True
|
|
|
|
faces_b.append(f_other)
|
|
|
|
# Swap.
|
|
|
|
faces_a, faces_b = faces_b, faces_a
|
|
|
|
faces_b.clear()
|
2012-11-29 14:02:28 +00:00
|
|
|
|
2013-01-04 07:57:33 +00:00
|
|
|
def walk_edgeloop(l):
|
|
|
|
"""
|
|
|
|
Could make this a generic function
|
|
|
|
"""
|
|
|
|
e_first = l.edge
|
|
|
|
e = None
|
|
|
|
while True:
|
|
|
|
e = l.edge
|
|
|
|
yield e
|
|
|
|
|
2023-06-14 07:28:45 +00:00
|
|
|
# Don't step past non-manifold edges.
|
2013-01-04 07:57:33 +00:00
|
|
|
if e.is_manifold:
|
2023-06-14 07:28:45 +00:00
|
|
|
# Walk around the quad and then onto the next face.
|
2013-01-04 07:57:33 +00:00
|
|
|
l = l.link_loop_radial_next
|
|
|
|
if len(l.face.verts) == 4:
|
|
|
|
l = l.link_loop_next.link_loop_next
|
|
|
|
if l.edge is e_first:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2024-01-09 05:41:46 +00:00
|
|
|
def extrapolate_uv(
|
|
|
|
fac,
|
|
|
|
l_a_outer, l_a_inner,
|
|
|
|
l_b_outer, l_b_inner,
|
|
|
|
):
|
|
|
|
l_b_inner[:] = l_a_inner
|
|
|
|
l_b_outer[:] = l_a_inner + ((l_a_inner - l_a_outer) * fac)
|
2012-11-29 14:02:28 +00:00
|
|
|
|
2019-05-08 23:15:01 +00:00
|
|
|
def apply_uv(_f_prev, l_prev, _f_next):
|
2012-11-29 14:02:28 +00:00
|
|
|
l_a = [None, None, None, None]
|
|
|
|
l_b = [None, None, None, None]
|
2013-01-15 23:15:32 +00:00
|
|
|
|
2012-11-29 14:02:28 +00:00
|
|
|
l_a[0] = l_prev
|
|
|
|
l_a[1] = l_a[0].link_loop_next
|
|
|
|
l_a[2] = l_a[1].link_loop_next
|
|
|
|
l_a[3] = l_a[2].link_loop_next
|
|
|
|
|
|
|
|
# l_b
|
|
|
|
# +-----------+
|
|
|
|
# |(3) |(2)
|
|
|
|
# | |
|
|
|
|
# |l_next(0) |(1)
|
|
|
|
# +-----------+
|
|
|
|
# ^
|
|
|
|
# l_a |
|
|
|
|
# +-----------+
|
|
|
|
# |l_prev(0) |(1)
|
|
|
|
# | (f) |
|
|
|
|
# |(3) |(2)
|
|
|
|
# +-----------+
|
2023-06-14 07:28:45 +00:00
|
|
|
# Copy from this face to the one above.
|
2012-11-29 14:02:28 +00:00
|
|
|
|
2023-06-14 07:28:45 +00:00
|
|
|
# Get the other loops.
|
2012-11-29 14:02:28 +00:00
|
|
|
l_next = l_prev.link_loop_radial_next
|
|
|
|
if l_next.vert != l_prev.vert:
|
|
|
|
l_b[1] = l_next
|
|
|
|
l_b[0] = l_b[1].link_loop_next
|
|
|
|
l_b[3] = l_b[0].link_loop_next
|
|
|
|
l_b[2] = l_b[3].link_loop_next
|
|
|
|
else:
|
|
|
|
l_b[0] = l_next
|
|
|
|
l_b[1] = l_b[0].link_loop_next
|
|
|
|
l_b[2] = l_b[1].link_loop_next
|
|
|
|
l_b[3] = l_b[2].link_loop_next
|
2009-12-17 01:21:55 +00:00
|
|
|
|
2024-01-09 05:41:46 +00:00
|
|
|
l_a_uv = [l[uv_act].uv for l in l_a]
|
|
|
|
l_b_uv = [l[uv_act].uv for l in l_b]
|
|
|
|
|
2013-01-04 07:57:33 +00:00
|
|
|
if EXTEND_MODE == 'LENGTH_AVERAGE':
|
2020-07-15 11:48:28 +00:00
|
|
|
d1 = edge_lengths[l_a[1].edge.index][0]
|
|
|
|
d2 = edge_lengths[l_b[2].edge.index][0]
|
|
|
|
try:
|
|
|
|
fac = d2 / d1
|
|
|
|
except ZeroDivisionError:
|
|
|
|
fac = 1.0
|
2013-01-04 07:57:33 +00:00
|
|
|
elif EXTEND_MODE == 'LENGTH':
|
2012-11-29 14:02:28 +00:00
|
|
|
a0, b0, c0 = l_a[3].vert.co, l_a[0].vert.co, l_b[3].vert.co
|
|
|
|
a1, b1, c1 = l_a[2].vert.co, l_a[1].vert.co, l_b[2].vert.co
|
2009-12-17 01:21:55 +00:00
|
|
|
|
2012-11-29 14:02:28 +00:00
|
|
|
d1 = (a0 - b0).length + (a1 - b1).length
|
|
|
|
d2 = (b0 - c0).length + (b1 - c1).length
|
|
|
|
try:
|
|
|
|
fac = d2 / d1
|
|
|
|
except ZeroDivisionError:
|
|
|
|
fac = 1.0
|
2009-12-17 01:21:55 +00:00
|
|
|
else:
|
2012-11-29 14:02:28 +00:00
|
|
|
fac = 1.0
|
2009-12-17 01:21:55 +00:00
|
|
|
|
2024-04-30 02:46:05 +00:00
|
|
|
extrapolate_uv(
|
|
|
|
fac,
|
|
|
|
l_a_uv[3], l_a_uv[0],
|
|
|
|
l_b_uv[3], l_b_uv[0],
|
|
|
|
)
|
2024-01-09 05:41:46 +00:00
|
|
|
|
2024-04-30 02:46:05 +00:00
|
|
|
extrapolate_uv(
|
|
|
|
fac,
|
|
|
|
l_a_uv[2], l_a_uv[1],
|
|
|
|
l_b_uv[2], l_b_uv[1],
|
|
|
|
)
|
2009-12-17 01:21:55 +00:00
|
|
|
|
2013-01-04 07:57:33 +00:00
|
|
|
# -------------------------------------------
|
2023-06-14 07:28:45 +00:00
|
|
|
# Calculate average length per loop if needed.
|
2013-01-04 07:57:33 +00:00
|
|
|
|
|
|
|
if EXTEND_MODE == 'LENGTH_AVERAGE':
|
|
|
|
bm.edges.index_update()
|
|
|
|
edge_lengths = [None] * len(bm.edges)
|
|
|
|
|
|
|
|
for f in faces:
|
2023-06-14 07:28:45 +00:00
|
|
|
# We know it's a quad.
|
2013-01-04 07:57:33 +00:00
|
|
|
l_quad = f.loops[:]
|
|
|
|
l_pair_a = (l_quad[0], l_quad[2])
|
|
|
|
l_pair_b = (l_quad[1], l_quad[3])
|
|
|
|
|
|
|
|
for l_pair in (l_pair_a, l_pair_b):
|
|
|
|
if edge_lengths[l_pair[0].edge.index] is None:
|
|
|
|
|
|
|
|
edge_length_store = [-1.0]
|
|
|
|
edge_length_accum = 0.0
|
|
|
|
edge_length_total = 0
|
|
|
|
|
|
|
|
for l in l_pair:
|
|
|
|
if edge_lengths[l.edge.index] is None:
|
|
|
|
for e in walk_edgeloop(l):
|
|
|
|
if edge_lengths[e.index] is None:
|
|
|
|
edge_lengths[e.index] = edge_length_store
|
|
|
|
edge_length_accum += e.calc_length()
|
|
|
|
edge_length_total += 1
|
|
|
|
|
|
|
|
edge_length_store[0] = edge_length_accum / edge_length_total
|
|
|
|
|
2024-01-09 05:41:46 +00:00
|
|
|
# done with average length
|
|
|
|
# ------------------------
|
2009-12-17 01:21:55 +00:00
|
|
|
|
2024-01-09 05:41:46 +00:00
|
|
|
walk_face_init(faces, f_act)
|
|
|
|
for f_triple in walk_face(f_act):
|
|
|
|
apply_uv(*f_triple)
|
2023-06-14 07:28:45 +00:00
|
|
|
|
PyAPI: use keyword only arguments
Use keyword only arguments for the following functions.
- addon_utils.module_bl_info 2nd arg `info_basis`.
- addon_utils.modules 1st `module_cache`, 2nd arg `refresh`.
- addon_utils.modules_refresh 1st arg `module_cache`.
- bl_app_template_utils.activate 1nd arg `template_id`.
- bl_app_template_utils.import_from_id 2nd arg `ignore_not_found`.
- bl_app_template_utils.import_from_path 2nd arg `ignore_not_found`.
- bl_keymap_utils.keymap_from_toolbar.generate 2nd & 3rd args `use_fallback_keys` & `use_reset`.
- bl_keymap_utils.platform_helpers.keyconfig_data_oskey_from_ctrl 2nd arg `filter_fn`.
- bl_ui_utils.bug_report_url.url_prefill_from_blender 1st arg `addon_info`.
- bmesh.types.BMFace.copy 1st & 2nd args `verts`, `edges`.
- bmesh.types.BMesh.calc_volume 1st arg `signed`.
- bmesh.types.BMesh.from_mesh 2nd..4th args `face_normals`, `use_shape_key`, `shape_key_index`.
- bmesh.types.BMesh.from_object 3rd & 4th args `cage`, `face_normals`.
- bmesh.types.BMesh.transform 2nd arg `filter`.
- bmesh.types.BMesh.update_edit_mesh 2nd & 3rd args `loop_triangles`, `destructive`.
- bmesh.types.{BMVertSeq,BMEdgeSeq,BMFaceSeq}.sort 1st & 2nd arg `key`, `reverse`.
- bmesh.utils.face_split 4th..6th args `coords`, `use_exist`, `example`.
- bpy.data.libraries.load 2nd..4th args `link`, `relative`, `assets_only`.
- bpy.data.user_map 1st..3rd args `subset`, `key_types, `value_types`.
- bpy.msgbus.subscribe_rna 5th arg `options`.
- bpy.path.abspath 2nd & 3rd args `start` & `library`.
- bpy.path.clean_name 2nd arg `replace`.
- bpy.path.ensure_ext 3rd arg `case_sensitive`.
- bpy.path.module_names 2nd arg `recursive`.
- bpy.path.relpath 2nd arg `start`.
- bpy.types.EditBone.transform 2nd & 3rd arg `scale`, `roll`.
- bpy.types.Operator.as_keywords 1st arg `ignore`.
- bpy.types.Struct.{keyframe_insert,keyframe_delete} 2nd..5th args `index`, `frame`, `group`, `options`.
- bpy.types.WindowManager.popup_menu 2nd & 3rd arg `title`, `icon`.
- bpy.types.WindowManager.popup_menu_pie 3rd & 4th arg `title`, `icon`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.app_template_paths 1st arg `subdir`.
- bpy.utils.blend_paths 1st..3rd args `absolute`, `packed`, `local`.
- bpy.utils.execfile 2nd arg `mod`.
- bpy.utils.keyconfig_set 2nd arg `report`.
- bpy.utils.load_scripts 1st & 2nd `reload_scripts` & `refresh_scripts`.
- bpy.utils.preset_find 3rd & 4th args `display_name`, `ext`.
- bpy.utils.resource_path 2nd & 3rd arg `major`, `minor`.
- bpy.utils.script_paths 1st..4th args `subdir`, `user_pref`, `check_all`, `use_user`.
- bpy.utils.smpte_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.smpte_from_seconds 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.system_resource 2nd arg `subdir`.
- bpy.utils.time_from_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.time_to_frame 2nd & 3rd args `fps`, `fps_base`.
- bpy.utils.units.to_string 4th..6th `precision`, `split_unit`, `compatible_unit`.
- bpy.utils.units.to_value 4th arg `str_ref_unit`.
- bpy.utils.user_resource 2nd & 3rd args `subdir`, `create`
- bpy_extras.view3d_utils.location_3d_to_region_2d 4th arg `default`.
- bpy_extras.view3d_utils.region_2d_to_origin_3d 4th arg `clamp`.
- gpu.offscreen.unbind 1st arg `restore`.
- gpu_extras.batch.batch_for_shader 4th arg `indices`.
- gpu_extras.batch.presets.draw_circle_2d 4th arg `segments`.
- gpu_extras.presets.draw_circle_2d 4th arg `segments`.
- imbuf.types.ImBuf.resize 2nd arg `resize`.
- imbuf.write 2nd arg `filepath`.
- mathutils.kdtree.KDTree.find 2nd arg `filter`.
- nodeitems_utils.NodeCategory 3rd & 4th arg `descriptions`, `items`.
- nodeitems_utils.NodeItem 2nd..4th args `label`, `settings`, `poll`.
- nodeitems_utils.NodeItemCustom 1st & 2nd arg `poll`, `draw`.
- rna_prop_ui.draw 5th arg `use_edit`.
- rna_prop_ui.rna_idprop_ui_get 2nd arg `create`.
- rna_prop_ui.rna_idprop_ui_prop_clear 3rd arg `remove`.
- rna_prop_ui.rna_idprop_ui_prop_get 3rd arg `create`.
- rna_xml.xml2rna 2nd arg `root_rna`.
- rna_xml.xml_file_write 4th arg `skip_typemap`.
2021-06-08 08:03:14 +00:00
|
|
|
bmesh.update_edit_mesh(me, loop_triangles=False)
|
2018-09-05 22:25:56 +00:00
|
|
|
return STATUS_OK
|
2009-12-16 21:27:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main(context, operator):
|
2023-07-06 02:33:47 +00:00
|
|
|
use_uv_selection = True
|
|
|
|
if context.space_data and context.space_data.type == 'VIEW_3D':
|
|
|
|
use_uv_selection = False # When called from the 3D editor, UV selection is ignored.
|
|
|
|
|
2018-09-05 22:25:56 +00:00
|
|
|
num_meshes = 0
|
|
|
|
num_errors = 0
|
|
|
|
status = 0
|
|
|
|
|
2018-12-18 02:35:43 +00:00
|
|
|
ob_list = context.objects_in_mode_unique_data
|
2018-09-05 22:25:56 +00:00
|
|
|
for ob in ob_list:
|
|
|
|
num_meshes += 1
|
|
|
|
|
2023-07-06 02:33:47 +00:00
|
|
|
ret = extend(ob, operator.properties.mode, use_uv_selection)
|
2018-09-05 22:25:56 +00:00
|
|
|
if ret != STATUS_OK:
|
|
|
|
num_errors += 1
|
|
|
|
status |= ret
|
|
|
|
|
|
|
|
if num_errors == num_meshes:
|
|
|
|
if status & STATUS_ERR_NOT_QUAD:
|
|
|
|
operator.report({'ERROR'}, "Active face must be a quad")
|
|
|
|
elif status & STATUS_ERR_NOT_SELECTED:
|
|
|
|
operator.report({'ERROR'}, "Active face not selected")
|
2023-12-06 09:12:25 +00:00
|
|
|
elif status & STATUS_ERR_NO_FACES_SELECTED:
|
|
|
|
operator.report({'ERROR'}, "No selected faces")
|
|
|
|
elif status & STATUS_ERR_MISSING_UV_LAYER:
|
|
|
|
operator.report({'ERROR'}, "No UV layers")
|
2018-09-05 22:25:56 +00:00
|
|
|
else:
|
2022-09-14 06:18:59 +00:00
|
|
|
assert status & STATUS_ERR_ACTIVE_FACE != 0
|
2018-09-05 22:25:56 +00:00
|
|
|
operator.report({'ERROR'}, "No active face")
|
2009-12-16 21:27:07 +00:00
|
|
|
|
|
|
|
|
2011-08-12 06:57:00 +00:00
|
|
|
class FollowActiveQuads(Operator):
|
2012-07-03 09:02:41 +00:00
|
|
|
"""Follow UVs from active quads along continuous face loops"""
|
2009-12-17 01:21:55 +00:00
|
|
|
bl_idname = "uv.follow_active_quads"
|
|
|
|
bl_label = "Follow Active Quads"
|
2010-03-01 00:03:51 +00:00
|
|
|
bl_options = {'REGISTER', 'UNDO'}
|
2009-12-16 21:27:07 +00:00
|
|
|
|
2019-10-31 07:00:18 +00:00
|
|
|
mode: EnumProperty(
|
2018-06-26 17:41:37 +00:00
|
|
|
name="Edge Length Mode",
|
|
|
|
description="Method to space UV edge loops",
|
2019-03-14 00:08:48 +00:00
|
|
|
items=(
|
|
|
|
('EVEN', "Even", "Space all UVs evenly"),
|
|
|
|
('LENGTH', "Length", "Average space UVs edge length of each loop"),
|
|
|
|
('LENGTH_AVERAGE', "Length Average", "Average space UVs edge length of each loop"),
|
|
|
|
),
|
2018-06-26 17:41:37 +00:00
|
|
|
default='LENGTH_AVERAGE',
|
|
|
|
)
|
2009-12-16 21:27:07 +00:00
|
|
|
|
2010-08-09 01:37:09 +00:00
|
|
|
@classmethod
|
|
|
|
def poll(cls, context):
|
2019-03-13 23:56:25 +00:00
|
|
|
return context.mode == 'EDIT_MESH'
|
2009-12-16 21:27:07 +00:00
|
|
|
|
2009-12-17 01:21:55 +00:00
|
|
|
def execute(self, context):
|
|
|
|
main(context, self)
|
2009-12-24 19:50:43 +00:00
|
|
|
return {'FINISHED'}
|
2009-12-16 21:27:07 +00:00
|
|
|
|
2019-05-08 23:15:01 +00:00
|
|
|
def invoke(self, context, _event):
|
2011-03-09 11:01:44 +00:00
|
|
|
wm = context.window_manager
|
|
|
|
return wm.invoke_props_dialog(self)
|
2017-03-18 09:03:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
classes = (
|
|
|
|
FollowActiveQuads,
|
2017-05-25 05:11:00 +00:00
|
|
|
)
|