1

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).

enter image description here

I used "Smart UV project":

enter image description here

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)

enter image description here

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).

Luke Wenke
  • 175
  • 6

1 Answers1

2

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 x_pos.add(round(loop.uv.x, 3)) y_pos.add(round(loop.uv.y, 3))

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

Gorgious
  • 30,723
  • 2
  • 44
  • 101
  • 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. – Luke Wenke Mar 03 '23 at 09:55
  • Also if I delete the bottom faces of the torus (which I'd usually do) the UV map becomes 43 x 43. – Luke Wenke Mar 03 '23 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. – Luke Wenke Mar 03 '23 at 10:08
  • Oh sorry I read it as project from view. I'll see about updating the answer – Gorgious Mar 03 '23 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. – Luke Wenke Mar 03 '23 at 23:47
  • you could use some kind of a more lenient rounding process, eg testing if a value is within a range of all the other ones – Gorgious Mar 09 '23 at 16:00
  • If the squares are slightly out in the UV map I can use snap to the other squares. I need to do that so that each square fits exactly one pixel. – Luke Wenke Mar 10 '23 at 22:12