日々適当

hibitekitou

UV空間のUVアイランドの中心にポイントを集める

cg |2022-03-12

前書いたの、ポリゴンごとの中心にまとめる、だった。
で、欠点はありつつも、前書いたのを拡張する形でUV空間でのポリゴンアイランドとかUVシェルとか呼ばれる塊ごとにその中心にポイントを集約するのを描きました。

import bpy
import bpy_extras.mesh_utils
import mathutils

me = bpy.context.object.data
islands = bpy_extras.mesh_utils.mesh_linked_uv_islands(me)

uv_layer = me.uv_layers.active.data

for island in islands:
    v = mathutils.Vector([0.,0.])
    pIndex = []
    for i in island:
        for loop_index in range( me.polygons[i].loop_start , me.polygons[i].loop_start + me.polygons[i].loop_total ):
            pIndex.append( loop_index )
    pIndex = list(set(pIndex))
    for i in pIndex:
        v += uv_layer[i].uv
    v = v / len(pIndex)
    for i in pIndex:
        uv_layer[i].uv = v

bpy_extras.mesh_utils.mesh_linked_uv_islands ってのがbpy_extras.mesh_utilsに用意されているのでそれを利用した形。
欠点というのはUV空間内で別のアイランドに属するポイント位置が完全に重なっていると、そのポイントたちが所属するアイランドが一つと見做されちゃうっぽいことです(そのポイント同士は接続されていないのだけどね)。そんな状況を作らなければいいのだけど、使いたい場面はそんな状況だったのでやっぱり困ってる。

ネットを見ると、これより遥かに複雑な書き方で実現しているものを見るので、そっちの書き方を試すかなぁ

コメント ( 0 )|Trackback ( )
  ・