Skip to content

Commit

Permalink
Code clean-up and consistency.
Browse files Browse the repository at this point in the history
  • Loading branch information
majikdev committed Oct 25, 2022
1 parent 6740426 commit b9be29f
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions precise_uv_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from mathutils.geometry import tessellate_polygon
from math import ceil, sqrt, isclose

# Precise UV layout export operator.
# Precise UV export operator.

class ExportLayout(bpy.types.Operator):
"""Export pixel-perfect UV layouts as images"""
Expand Down Expand Up @@ -54,15 +54,15 @@ def execute(self, context):
edit_mode = mesh.mode == "EDIT"

if edit_mode:
bpy.ops.object.mode_set(mode="OBJECT", toggle=False)
bpy.ops.object.mode_set(mode="OBJECT")

path = bpy.path.ensure_ext(self.filepath, ".png")
meshes = list(self.get_meshes_to_export(context))
triangles = list(self.get_mesh_triangles(meshes))
self.export_uv_layout(path, triangles)

if edit_mode:
bpy.ops.object.mode_set(mode="EDIT", toggle=False)
bpy.ops.object.mode_set(mode="EDIT")

return {"FINISHED"}

Expand Down Expand Up @@ -92,7 +92,7 @@ def draw_line(ax, ay, bx, by):
while dist < length:
if x_min <= x < x_max and y_min <= y < y_max:
offset = (y * width + x) * 4
pixels[offset:offset + 4] = get_colour(index)
pixels[offset:offset + 4] = get_colour()

if x_dist < y_dist:
x_dist += x_delta
Expand All @@ -115,21 +115,21 @@ def fill_poly(ax, ay, bx, by, cx, cy):

if not (negative and positive):
offset = (y * width + x) * 4
pixels[offset:offset + 4] = get_colour(index)
pixels[offset:offset + 4] = get_colour()

def get_colour(index):
if not self.shade_islands:
return 1, 1, 1, 1
def get_colour():
if self.shade_islands:
value = 1 - (island_index % 5) * 0.125

value = 1 - (index % 5) * 0.125
return value, value, value, 1

return value, value, value, 1
return 1, 1, 1, 1

width, height = self.size
pixels = [0, 0, 0, 0] * width * height

for triangle in triangles:
index = triangle.pop()
island_index = triangle.pop()
v1, v2, v3 = [(x * width, y * height) for x, y in triangle]

x_min, x_max = max(min(v1[0], v2[0], v3[0]), 0), min(ceil(max(v1[0], v2[0], v3[0])), width)
Expand All @@ -146,9 +146,9 @@ def get_colour(index):

try:
image = bpy.data.images.new("temp", width, height, alpha=True)
image.filepath = path
image.pixels = pixels
image.filepath, image.pixels = path, pixels
image.save()

bpy.data.images.remove(image)

except:
Expand All @@ -162,7 +162,7 @@ def get_image_size(context, default):
image = context.space_data.image

if image is not None:
image_w, image_h = context.space_data.image.size
image_w, image_h = image.size

if image_w and image_h:
width, height = image_w, image_h
Expand Down Expand Up @@ -190,7 +190,7 @@ def get_mesh_triangles(meshes):

# TODO: Sometimes there are more islands than there should be?
# TODO: Sometimes the island_index is calculated incorrectly.
# Pperhaps a vertex resides in more than one island?
# Perhaps a vertex resides in more than one island?

for vertex in layer:
if vertex not in [uv for island in islands for uv in island]:
Expand All @@ -204,7 +204,9 @@ def get_mesh_triangles(meshes):
bpy.ops.uv.select_linked()
bpy.ops.object.mode_set(mode="OBJECT")

if island := [uv for uv in layer if uv.select]:
island = [uv for uv in layer if uv.select]

if island:
islands.append(island)

bpy.ops.object.mode_set(mode="EDIT")
Expand All @@ -224,9 +226,9 @@ def get_mesh_triangles(meshes):
island_index = i

for triangle in tessellate_polygon([uvs]):
yield [tuple(uvs[index]) for index in triangle] + [island_index]
yield [tuple(uvs[i]) for i in triangle] + [island_index]

# Register and unregister the addon.
# Register and unregister.

def menu_entry(self, context):
self.layout.operator(ExportLayout.bl_idname)
Expand Down

0 comments on commit b9be29f

Please sign in to comment.