User Tools

Site Tools


Sidebar

Dan's Wiki

DokuWiki Instructions (local) DokuWiki Manual
Site Checker (Orphans Wanted)

Edit Sidebar

dan:pc:software:blender:uvdistributenetmesh

NetMesh work in progress

import bpy
import logging
from logging import FileHandler
 
 
class UVDot:
    '''Represents a UV selection in the UV Editor, which is 1 or more physical UVs'''
 
    # Important to delcare these member variables inside the __init__ block, otherwise
    # they are shared among all instances.
 
    def __init__(self):
        self.index = -1
        self.uv = None   # coordinates of this UVDot, usually matches the coordinates of all of the object UVs
        self.object_uv_indexes = set()
        self.vertex_index = None
        self.connected_to_vertex_indexes = set()
        self.selected = False
 
    def __repr__(self):
        return f"UVDot({self.index}): {('Sel' if self.selected else 'uns')}, UV{self.uv} object_uv_indexes \
{(self.object_uv_indexes)}, vertex_index {(self.vertex_index)}, \
connected_to_vertex_indexes {(self.connected_to_vertex_indexes)}"
 
 
logger = logging.getLogger(__name__ + "uvDistributeActionLogger")
 
logger.setLevel(logging.INFO)
# # uncomment the following line (and comment out the NullerHandler line) to write logging to the file
# hdlr = logging.FileHandler('/tmp/pythonlog3.log', mode='a')
hdlr = FileHandler('c:/tmp/pythonlog3.log',mode='w')
# hdlr = logging.NullHandler()
hdlr.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr) 
 
# bpy.ops.object.editmode_toggle()
bpy.ops.object.mode_set(mode = 'OBJECT')
 
ao = bpy.context.active_object 
 
# The content below here can be pasted into UVDistribute.py
 
mesh = ao.data
 
totalUVsSelectedCount = 0;       
totalUVsNotSelectedCount = 0;       
i = 0 
uv_layer = mesh.uv_layers.active.data
logger.info(f"uv_layer length {len(uv_layer)}")
 
# Creating a blank dictionary, decoding UV coordinates to list of UV indexes.
e = {}
connectedSelectedUVs = {}
vertexIndexToUVIndex = {}
 
uvDotCollection = []
vertex_indexToUVDot = {}
uv_indexToUVDot = {}
 
for poly in mesh.polygons:
    if poly.select:
        poly_vertex_indexes = []
        poly_uv_indexes = []
        for vertex_pointer_in_poly in poly.loop_indices:
            # if the uv point is selected:
            poly_vertex_indexes.append(mesh.loops[vertex_pointer_in_poly].vertex_index)
            poly_uv_indexes.append(vertex_pointer_in_poly)
        logger.info(f"Poly: {poly.index} poly_vertex_indexes {poly_vertex_indexes}, poly_uv_indexes {poly_uv_indexes}")
 
                # print("    Vertex: %d" % mesh.loops[vertex_pointer_in_poly].vertex_index)
 
# bpy.ops.uv.shortest_path_select()
# bpy.ops.uv.select_linked()
 
selectedUVIndexes = []
for i in range(len(uv_layer)):
    if uv_layer[i].select:
        selectedUVIndexes.append(i)
 
logger.info(f"Selected UV Indexes: { selectedUVIndexes}")
 
# Lets populate uvDotCollection separately
for i in range(len(uv_layer)):
    myVertexIndex = mesh.loops[i].vertex_index
    if not myVertexIndex in vertex_indexToUVDot:
        uvDot = UVDot()
        uvDot.selected = False  # Start out False
        uvDot.vertex_index = myVertexIndex
        dotIndex = len(uvDotCollection)
        uvDot.index = dotIndex
        uvDotCollection.append(uvDot)
        vertex_indexToUVDot[myVertexIndex] = dotIndex
 
    uvDot = uvDotCollection[vertex_indexToUVDot[myVertexIndex]]
    uvDot.uv = uv_layer[i].uv
    uvDot.object_uv_indexes.add(i)
    if uv_layer[i].select:
        uvDot.selected = True # set to true if ANY of the UVs are selected
    uvDotCollection[uvDot.index] = uvDot
 
for edge in mesh.edges:
    uvDot = uvDotCollection[vertex_indexToUVDot[edge.vertices[0]]]
    uvDot.connected_to_vertex_indexes.add(edge.vertices[1])
    # uvDotCollection[uvDot.index] = uvDot # maybe need this
    uvDot = uvDotCollection[vertex_indexToUVDot[edge.vertices[1]]]
    uvDot.connected_to_vertex_indexes.add(edge.vertices[0])
    # uvDotCollection[uvDot.index] = uvDot # maybe need this
 
for uvDot in uvDotCollection:
    logger.info(f"   uvDot: {uvDot}")
 
logger.info(f"Size of vertex_indexToUVDot is {len(vertex_indexToUVDot)}, content: {vertex_indexToUVDot}")
 
# Let's find the corner points, that is, points which are only attached to 1 selected uvDOT
 
numberOfCornerPoints = 0
for uvDot in uvDotCollection:
    if uvDot.selected:
        connectedSelectedVertexCount = 0
        for connectedVertex in uvDot.connected_to_vertex_indexes:
            if uvDotCollection[vertex_indexToUVDot[connectedVertex]].selected:
                connectedSelectedVertexCount += 1
        if connectedSelectedVertexCount == 1:
            numberOfCornerPoints += 1
            logger.info(f"   selected uvDot with only 1 connection: {uvDot}")
 
logger.info(f" Number of Corner Points {numberOfCornerPoints}")
 
 
 
 
logger.info("Done running.")
 
bpy.ops.object.mode_set(mode = 'EDIT')
# bpy.ops.object.editmode_toggle()
while logger.hasHandlers():
    logger.removeHandler(logger.handlers[0])
# logger.removeHandler(hdlr)
dan/pc/software/blender/uvdistributenetmesh.txt · Last modified: 2022/02/20 02:30 by dwheele