Accessing UV coords from python.

Scripting in Blender with Python, and working on the API

Moderators: jesterKing, stiv

Timmmm
Posts: 9
Joined: Tue Mar 04, 2003 5:29 pm

Accessing UV coords from python.

Postby Timmmm » Tue Mar 04, 2003 5:34 pm

Hey ho, I've been trying for ages to figure out how to get the following script to export UV's properly. Currently they all come out as 0.0. Someone elses post mentioned that it doesn't work with NMesh, or something, but it does with Mesh? I dont *really* know python, and it took me a while to make this script from looking at other peoples export scripts, and the VERY inadequate phython API docs. All comments appreciated (except "Wib Wab Woo").

---------------------------------

Code: Select all

import Blender
Filename = "out.mot"
###################################
File = open(Filename, "w")
print "Opened %s\nExporting" % Filename

for Obj in Blender.Scene.getCurrent().getChildren():
  if Obj.getType() == "Mesh":
    Mesh = Obj.getData()   
    File.write("Mesh\n\n")

    for Vertex in Mesh.vertices:
      File.write( "  Vertex %s %s %s\n" % (Vertex.co[0], Vertex.co[1], Vertex.co[2]) )
      File.write( "  Normal %s %s %s\n" % (Vertex.no[0], Vertex.no[1], Vertex.no[2]) )
      File.write( "  UV %s %s\n" % (Vertex.uvco[0], Vertex.uvco[1]) )

    for Face in Mesh.faces:
      if len(Face.v) == 3: # Triangle
        File.write("  Triangle %s %s %s\n" % (Face.v[0].index, Face.v[1].index, Face.v[2].index))
      elif len(Face.v) == 4: # Quad
        File.write("  Quad %s %s %s %s\n" % (Face.v[0].index, Face.v[1].index,   Face.v[2].index, Face.v[3].index))

print "Exported"
File.close()

t_revs
Posts: 19
Joined: Tue Oct 22, 2002 4:08 pm

Postby t_revs » Wed Mar 05, 2003 6:41 pm

Hi
Here is a little prog I made to show Object data.
It may not be 100% right as I am a Python newbie, but I reckon you need the face.uv part.

# Print object data

import Blender
objects = Blender.Object.GetSelected()
object = objects[0] # this better be a mesh :)
objectname=object.name
meshname = object.data.name #or = objects[0].data.name
data = object.data #or = objects[0].data
faces = data.faces #or = objects[0].data.faces
vertices = data.verts #or = objects[0].data.verts

m = Blender.Mesh.get(objectname)
v = m.verts[0]
print v
#v.loc[0](1.0,1.0,1.0)

print"\n"
print "-----OBJECT DATA------------"
print "Object " + str(object)
print "Object Name " + str(objectname)
print "Data " + str(data)
print "Meshname " + str(meshname)
print "Faces " + str(faces)
print "No. of faces " + str(len(faces))
print "No. of verts " + str(len(vertices))


for face in faces:
facenum = 1
vertnum = 1
print "Face " + str(facenum)
print " Texture co-ords " + str(face.uv)
for vertex in face:
print " Vert " + str(vertnum)
print " Vert co-ords " + " : " + str(vertex.co[0]) + ", " + str(vertex.co[1]) + ", " + str(vertex.co[2])
print " Normal co-ords " + ": " + str(vertex.no[0]) + ", " + str(vertex.no[1]) + ", " + str(vertex.no[2])
vertnum += 1
facenum +=1

Timmmm
Posts: 9
Joined: Tue Mar 04, 2003 5:29 pm

Postby Timmmm » Wed Mar 05, 2003 8:42 pm

Yeah I think part of my problem, is that blender stores UV coords for each vertex of each face, so adjacent faces dont have to use adjacent parts of the texture. I (well, OpenGL), want it so that each vertex has its own UV coords and that they are the same for all the faces it is part of.

Oh well, I guess I can just remove the duplicates (because for my particular mapping, the vertices only have one set of UV coords....) Stupid that the thing I wrote doesn't work though...

t_revs
Posts: 19
Joined: Tue Oct 22, 2002 4:08 pm

Postby t_revs » Thu Mar 06, 2003 10:22 am

Hi
Just been reading up on texture coords and it seems .uvco is the coord for a 'sticky' texture, and so will always be 0.0, and .uv is for an 'orco' texture.
By the way in your Python prog what does the index in say (Face.v[0].index) do? Stupid newbie question I know.

Cheers

Timmmm
Posts: 9
Joined: Tue Mar 04, 2003 5:29 pm

Postby Timmmm » Thu Mar 06, 2003 3:12 pm

All the vertices of the mesh are store in an array.
Triangles are then constructed by specifying three offsets into the array to select 3 vertices.
This saves space ('cause the vertices are often duplicated at the edge of faces), and probably speeds up rendering a bit, (and is the format that OpenGL wants)...

BloB
Posts: 6
Joined: Fri Mar 07, 2003 1:38 pm

Postby BloB » Fri Mar 07, 2003 2:40 pm

Timmmm wrote:I (well, OpenGL), want it so that each vertex has its own UV coords and that they are the same for all the faces it is part of.


Where have you seen that ? Maybe some OpenGL functions are optimized to handle this case, but it's no obligation, really ! Of course, if you draw triangle strips or fans OpenGL allows you to give only once vertices that are common to adjacent faces and then you can only specify one normal and one texture coordinate. But you're not limited to that by OpenGL 'in general'.


BloB.


Return to “Python”

Who is online

Users browsing this forum: No registered users and 4 guests