# Counting rows and columns for Smart UV Project for voxel style meshes

I want to have one pixel on the UV map per square for a voxel style model. I set the texture interpolation to "closest" (rather than linear).

I used "Smart UV project":

By hand counting the UV map I got 49 rows and 49 columns. Then I changed the texture to be 49 x 49 pixels. I want to use much higher detail meshes so I'd rather not have to hand count the rows and columns in the UV map.

Any ideas about doing this? Maybe there is a plugin or script?

BTW someone asked a similar question 3 years and 8 months ago and no one responded....

Dimensions of Smart Project UV Unwrap

Edit: It would be good if it can also handle when Decimate - Planar is used with voxel like meshes and Smart UV Project - it could detect the minimum size of a square then divide it by the width and height of the UV map... (which gives 49 in this example)

An idea - a script/plugin could look at all of the U coordinates and all of the V coordinates and see the minimum gap between them. In my example it should be 1/49 or 0.020408 in normalized units (0 to 1 for U and V).

You can parse the uvs of your mesh and count the coordinates that are different from each other.

A naive approach using the set data structure would be :

import bpy

x_pos = set()
y_pos = set()

for loop in bpy.context.active_object.data.uv_layers.active.data:
# We round because of floating point precision error

rows, columns = len(x_pos) - 1, len(y_pos) - 1
print(rows)  # u
print(columns)  # v


This may be slow to execute if you have a very dense mesh. You can use numpy utilities to speed things up.

Inspired by this Q&A

uv_layers docs

MeshUVLoopLayer docs

• Hi the torus is 25 x 25 x 5. What I'm after involves the UV map.... for the torus it is 49 x 49. Also sometimes Smart UV Project can offset some islands by half a square so it can be things like 10.5 x 10.5. The mesh isn't necessarily the same size in X and Y though the UV map is. Mar 3, 2023 at 9:55
• Also if I delete the bottom faces of the torus (which I'd usually do) the UV map becomes 43 x 43. Mar 3, 2023 at 10:01
• As far as the density of the mesh goes - it has nothing internally - and it has 1252 faces - and 450 when the decimate planar modifier is used. Mar 3, 2023 at 10:08
• Oh sorry I read it as project from view. I'll see about updating the answer Mar 3, 2023 at 10:31
• Rounding to 3 sometimes doesn't give the right results but 2 does. Sometimes Smart UV Project offsets squares by fractions of a row (e.g. 5 or 10%) which is a shame. Mar 3, 2023 at 23:47