Come impilare l'isola UV usando Python in Blender?
Sto cercando un aiuto / soluzione alla mia domanda "Come impilare l'isola UV usando Python in Blender.
Non sono andato lontano dall'inizio ma questo è tutto ciò che ho ottenuto fino a questo punto:
import bpy
me = bpy.context.object.data
uv_layer = me.uv_layers.active.data
for poly in me.polygons:
# Here I want to move the selected poly in the location of my 2D cursor
# So eventually all my polygons are stacked into the same pile
Questa è una domanda per principianti e tutto l'aiuto e i suggerimenti sono molto apprezzati.
------------------MODIFICARE---------------------
Ecco l'illustrazione di cosa voglio ottenere con lo script:
Seleziona isola UV
Aggancia l'isola UV al cursore 2D
Ripeti le fasi 1 e 2 per ogni faccia nella mesh, quindi alla fine tutte le facce vengono impilate l'una sull'altra
Risposte
Qui abbiamo bisogno di tre cose:
- Trova la posizione del cursore 2d
- Controlla la modalità in cui ci troviamo prima di elaborare le coordinate UV
- Usa lo strato UV per trovare le coordinate UV
Per ogni faccia, una volta trovate le coordinate UV, calcola il loro centro e scosta le coordinate dalla differenza dal cursore.
Nota che se nessun editor UV è aperto, non troverà il cursore e non farà nulla.
Codice commentato:
import bpy
from mathutils import Vector
def find_cursor_location():
# Look through area and find the first image editor
for area in bpy.context.screen.areas:
if area.type == 'IMAGE_EDITOR':
return area.spaces.active.cursor_location
return None
obj = bpy.context.object
cursor = find_cursor_location()
if cursor:
#Check the mode as we cant do it in edit mode
mode = obj.mode
if mode != 'OBJECT':
bpy.ops.object.mode_set(mode='OBJECT')
me = obj.data #Need to get it here in case mode is changed
uv_layer = me.uv_layers.active
if uv_layer:
for poly in me.polygons:
# Get all Uv coordinates of the face
uvs = [uv_layer.data[loop_index].uv for loop_index in poly.loop_indices]
# Its center
center = sum(uvs, Vector((0,0))) / len(uvs)
# The needed offset
delta = center - cursor
# Shift UV coords
for uv_data in [uv_layer.data[loop_index] for loop_index in poly.loop_indices]:
uv_data.uv -= delta
#Back to the mode we were in
if mode != 'OBJECT':
bpy.ops.object.mode_set(mode=mode)