Note that there are some explanatory texts on larger screens.

plurals
  1. POBlender 2.6 JSON exporter, texture coordinates trouble
    primarykey
    data
    text
    <p>I'm trying to write a simple JSON exporter for Blender 2.6x because the only one I could find ( <a href="http://code.google.com/p/blender-machete/" rel="nofollow">http://code.google.com/p/blender-machete/</a> ) doesn't work with 2.6. I didn't have any trouble getting the vertices, normals, and indices from blender, but try as I might, I just can't seem to figure out why the texture coordinates are coming out wrong. The textures seem tilted diagonally across the face of a simple cube, and stretched... really ugly and wrong. I've been looking online and through the source for some of the official exporters, but I still can't figure it out, so I was hoping someone could give me some hints or solutions.</p> <p>The piece of code I'm using to access the texture coordinates is this:</p> <pre><code> # add texture coordinates to scene_data structure m = bpy.context.active_object.to_mesh(bpy.context.scene, True, 'PREVIEW') for j in range(len(m.tessfaces)): if len(m.tessface_uv_textures) &gt; 0: scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv1.x ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv1.y ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv2.x ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv2.y ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv3.x ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv3.y ) </code></pre> <p>This is giving me a list of texture coordinates, but somehow I'm doing it wrong, because of the incorrect appearance of the texture as I explained above.</p> <p>I don't know what else to do but show the code, since I've tried changing it around in every way I can think of, so here's the function where the above snippet of code is:</p> <pre><code>def get_json(objects, scene): """ Currently only supports one scene. Exports with -Z forward, Y up. """ scene_data = [] mesh_number = -1 # iterate over each mesh for i in range(len(bpy.data.objects)): if bpy.data.objects[i].type == 'MESH': mesh_number += 1 bpy.ops.object.mode_set(mode='OBJECT') # convert all the mesh's faces to triangles bpy.data.objects[i].select = True bpy.context.scene.objects.active = bpy.data.objects[i] bpy.ops.object.mode_set(mode='EDIT') bpy.ops.mesh.select_all(action='SELECT') bpy.ops.mesh.quads_convert_to_tris() bpy.context.scene.update() bpy.ops.object.mode_set(mode='OBJECT') bpy.data.objects[i].select = False # add data to scene_data structure scene_data.append({ "name" : bpy.data.objects[i].name, "vertices" : [], "indices" : [], "normals" : [], "tex_coords" : [] }) # iterate over all the vertices in the mesh for j in range(len(bpy.data.objects[i].data.vertices)): # add vertex to scene_data structure scene_data[mesh_number]["vertices"].append( bpy.data.objects[i].data.vertices[j].co.x + bpy.data.objects[i].location.x ) scene_data[mesh_number]["vertices"].append( bpy.data.objects[i].data.vertices[j].co.z + bpy.data.objects[i].location.z ) scene_data[mesh_number]["vertices"].append( -(bpy.data.objects[i].data.vertices[j].co.y + bpy.data.objects[i].location.y) ) # add vertex normal to scene_data structure scene_data[mesh_number]["normals"].append( bpy.data.objects[i].data.vertices[j].normal.x ) scene_data[mesh_number]["normals"].append( bpy.data.objects[i].data.vertices[j].normal.z ) scene_data[mesh_number]["normals"].append( -(bpy.data.objects[i].data.vertices[j].normal.y) ) # iterate over each face in the mesh for j in range(len(bpy.data.objects[i].data.polygons)): verts_in_face = bpy.data.objects[i].data.polygons[j].vertices[:] # iterate over each vertex in the face for k in range(len(verts_in_face)): # twiddle index for -Z forward, Y up index = k if index == 1: index = 2 elif index == 2: index = 1 # twiddle index so we draw triangles counter-clockwise if index == 0: index = 2 elif index == 2: index = 0 # add index to scene_data structure scene_data[mesh_number]["indices"].append( verts_in_face[index] ) # add texture coordinates to scene_data structure m = bpy.context.active_object.to_mesh(bpy.context.scene, True, 'PREVIEW') for j in range(len(m.tessfaces)): if len(m.tessface_uv_textures) &gt; 0: scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv1.x ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv1.y ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv2.x ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv2.y ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv3.x ) scene_data[mesh_number]["tex_coords"].append( m.tessface_uv_textures.active.data[j].uv3.y ) return json.dumps(scene_data, indent=4) </code></pre> <p>Will someone please tell me what I'm doing wrong? I've been at this for a few days now with no progress.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload