Problem Exporting Texture Coordinates

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

melbow
Posts: 6
Joined: Fri Nov 02, 2012 11:04 pm

Problem Exporting Texture Coordinates

Postby melbow » Wed Dec 05, 2012 9:59 pm

I am writing an exporter and hit a brick wall attempting to export UV coordinates. My UV export code is:

Code: Select all

def WriteMeshUVCoordinates(Config, Mesh):
   if Config.Verbose:
      print('  Writing UV Coordinates')
   
   # start UV list
   Config.File.write('{}"uvList" : [ '.format(' ' * Config.WhiteSpace))
   
   if len(Mesh.tessface_uv_textures) > 0:
      Faces = Mesh.tessface_uv_textures.active.data
      for index, f in enumerate(Faces):
         if index != 0:
            Config.File.write(', ')
         
         if len(f.uv) == 4:
            # quads
            Config.File.write('{}, {}, {}, {}, {}, {}, '.format(f.uv1.x, 1.0 - f.uv1.y, f.uv2.x, 1.0 - f.uv2.y, f.uv3.x, 1.0 - f.uv3.y))
            Config.File.write('{}, {}, {}, {}, {}, {}'.format(f.uv1.x, 1.0 - f.uv1.y, f.uv3.x, 1.0 - f.uv3.y, f.uv4.x, 1.0 - f.uv4.y))
         
         elif len(f.uv) == 3:
            # triangles
            Config.File.write('{}, {}, {}, {}, {}, {}, '.format(f.uv1.x, 1.0 - f.uv1.y, f.uv2.x, 1.0 - f.uv2.y, f.uv3.x, 1.0 - f.uv3.y))
      
   # end UV list
   Config.File.write(']')
   
   if Config.Verbose:
      print('  Done')


I am unsure if it is related to how I export my indices or not, so here is the index export code too:

Code: Select all

def WriteMeshTriList(Config, Mesh):
   if Config.Verbose:
      print('  Writing Mesh Triangle List.')

   # start triangle list
   Config.File.write('{}"triList" : [ '.format(' ' * Config.WhiteSpace))
   
   for index, face in enumerate(Mesh.tessfaces):
      # if not the first element in the list
      if index != 0:
         Config.File.write(', ')
      
      if len(face.vertices) == 4:
         # quad
         # should sort by face.center
         Config.File.write('{}, {}, {}, '.format(face.vertices[0], face.vertices[1], face.vertices[2]))
         Config.File.write('{}, {}, {}'.format(face.vertices[0], face.vertices[2], face.vertices[3]))
      
      if len(face.vertices) == 3:
         # triangles :D!
         # should sort by face.center
         Config.File.write('{}, {}, {}'.format(face.vertices[0], face.vertices[1], face.vertices[2]))
      
   # end triangle list
   Config.File.write(' ]')
   
   if Config.Verbose:
      print('  Done')


An image showing the problem is available here:
https://www.dropbox.com/s/6qtjjrvvf8qyjm5/zomb_uvmishap.png
Obviously the right is what it should look like.
It's pretty clear my UV are messed up :(

I'd really appreciate any help, as this project has already taken much longer than anticipated. Thanks!
<3

melbow
Posts: 6
Joined: Fri Nov 02, 2012 11:04 pm

Postby melbow » Thu Dec 06, 2012 5:36 am

I think I'm on the right track... I think it has to do with the coordinate spaces. I'm not entirely sure how to deal with different coordinate spaces. I'll do some more googling but any input so I know I'm on the right track would be super helpful.
<3

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Thu Dec 06, 2012 2:02 pm

you flip the Y coordinate, which is usually required, but could it be that your target format expects this like it is originally in blender? or maybe X flipped or something like this?

note that 3d view may differ from the actual texture mapping, e.g. you need to change coordinates from Generated to UV if you use GLSL shading to see the actual uv mapping, but in multitexture mode it doesn't seem to be required...
I'm sitting, waiting, wishing, building Blender in superstition...

melbow
Posts: 6
Joined: Fri Nov 02, 2012 11:04 pm

Postby melbow » Thu Dec 06, 2012 5:53 pm

I'm pretty confident I need to flip the V coordinate, because it wasn't showing anything before I did that. And I'm using GLSL in Blender shading so that shouldn't be the problem. That is good to know though, so thank you. If everything looks correct maybe I messed up my rendering code, but I haven't been able to find any problems yet... I'm super confused.
<3

melbow
Posts: 6
Joined: Fri Nov 02, 2012 11:04 pm

Postby melbow » Thu Dec 06, 2012 10:07 pm

Just found the problem I think. I'm exporting the UV coordinates for each index, but it should be per vertex right?? I just removed my index array and changed my vertex array to not use the index buffer and it worked. It's been a very long time since I did any rendering code. Hope my ignorance helped someone out there. And thanks again CoDEmanX for your input.
<3

CoDEmanX
Posts: 894
Joined: Sun Apr 05, 2009 7:42 pm
Location: Germany

Postby CoDEmanX » Thu Dec 06, 2012 10:28 pm

UVs are actually stored per loop (not vertex), but if you use tessface_uv_texture, then it's the old style (uv1-uv4) and a little different.

good to hear you got it sorted!
I'm sitting, waiting, wishing, building Blender in superstition...


Return to “Python”

Who is online

Users browsing this forum: No registered users and 0 guests