How to map a fac to a UVmap?

I’m trying to map each individual faces of a cube to desired rectangles on a UV map.
I’m using one of the latest builds with Bmesh.

At this point I have :
Which gives me my UV map I guess.

But how would I now take a face and map it’s vertices where I want on the map?
I’m a bit lost :confused:

Maybe try to understand the UV-part of the latest(!!) import wavefront script in the addons !
I think there is solved how the UV map has to be created if all the info is given in an *.obj file and should give you a hint…

Oh yes good idea! And obj is a good format to learn since it’s so basic, I won’t have to cope with 10000 lines to find what I need :).
I’ll update this thread with the solution when I have it (you can still help me if you have the answer :p)

Hi, make a small example (export obj, obj with uv map say the cube) and do the import ‘by hand’ :wink: ?

Oh simpler than that, I’ll just read the code where it create the uv map and apply that to make my “custom unwrap”, I don’t need to create geometry.

In fact I’m making a minecraft importer, and I want to support texture packs, so I have to unwrap specific faces to some part of a atlas.
I’ll post the code here when I find (will work on this tonight)

I’m struggling with UVs and 2.63, too.
I have to convert this 20 lines of code for my addon.
But no idea what I have to change.

If I change “uvtex =” to “uvtex =”, than I have the problem that there is no uv1 to uv4.
And is readonly?

We’ll get through this :slight_smile:
Send me a pm if you want to discuss that!
I’ll start working on this in one hour

searching … a Cube (6 faces) gives len(cd.uv_loop_layers[0].data) 24 6 * 4 corners and there are uv to be found 2D vectors.
cd =

IMHO, the problem here is, that we have two ways to set UVs now.
With BMesh polys and the “uv_loop_layers”, and old meshes (tesselated meshes) with “mesh.tessface_uv_textures.”.
For my problem I need a logic for tesselated meshes, I want to build my mesh the same way as in 2.62. If I remove the UV part, than my Add-On is working with the old mesh code in 2.63.
Maybe I’m wrong here.

Interesting part of the new obj importer is from line 600 to 625 and yes it uses tesselated mesh.
It’s confusing to have the current way being changed and bmesh way coexisting! (mostly because we have to guess by reading)
Now I have to understand what’s happening in there

When I try to create a new uv map like this : on a cube, I have this error : Can’t add tessface uv’s when MPoly’s exist
So I’m not sure but it seems like it’s already a bmesh mesh so I have to use uv_loop_layers but I don’t know how to manipulate them at all for now…

yes, for a cube you have to use the uv_loop_layers.
Than you have a different problem than I have. I need it for a tesselated Mesh (old logic for meshes).
What kind of mesh do you want to uv-map?

first succes !
try this in object mode (after adding a UV map with ):
bpy.context.selected_objects[0].data.uv_loop_layers[0].data[1].uv.x = 0.5

Now I’m looking on how to get a correspondence face / 4 vertices, could you help me out?

For your problem, you could convert your tesselated mesh to a bmesh can’t you?
like that ?
#get a bmesh
bm =
#fill the bmesh with mesh data
#blablabla go crazy with geometry

no, I can not convert the object at this point. I’m generating s lot of objects and join them at the end. After that I could convert it. But than I have no clue which are the faces I want to uv map.
I have to uv map one of the objects at generation time. And at this moment it’s a tesselated mesh.

So your problem with uvs is that you can access the raw collection of vertices but you want to access the uv per face?

If I understand it right, than yes.

At generation time of the mesh I know every vertice and it’s uv. At this point I store the uv verts index and uv coords in an array.
After the mesh creation I call my function to set the stored uv coords.
That is the posted Code: which I have to change.
IMHO every conversion to bmesh would change my structure of the mesh and my stored UVs are worthless.
That’s the reason why I have to use tesselated mesh.
Other solution would be to change the generation of the mesh to BMesh, that I can store the UV in the order of the new structure at generation time.
But that would be a huge task, I have a very complex object. That’s a ToDo for a later version.

The object, where only the studs have a UV for the Logo. At the generation time of the studs I know the UV coords for the logo. But after joining the brick, I can not figgure out which faces are the studs.

Okay I now understand your problem.
What kind of scene are you generating (if it’s okay to ask)?
But even with a Bmesh object, I don’t know how to obtain the MeshUvLoop that correspond to a given vertex.
I wonder if the API isn’t just… very incomplete?
But maybe I’m just doing it wrong.

Also I don’t understand why I got this on a cube :
>>> len(cubeMesh.faces)

>>> len(cubeMesh.vertices)

I mean, it should be 6 faces!

Added a shot of the object

EDIT: the bottom of the brick (if manifold is activated):

EDIT2: A Rendering with cycles to see the UV mapping for the logo:

The len(cubeMesh.faces) gives me 6 in 2.62, I have no clue what’s going on here.

Wow a lego generator, that is so cool :D.
I don’t know who we could ask about our problems?

The Cube has no faces, only polygons. Thats why len(cubeMesh.faces) is 0. Maybe try len(cubeMesh.polygons) (only guessing)