我正在嘗試爲Blender 2.6x編寫一個簡單的JSON導出器,因爲我能找到的唯一一個(http://code.google.com/p/blender-machete/)不適用於2.6。我從攪拌器獲取頂點,法線和指數沒有任何問題,但儘可能地嘗試,我似乎無法弄清楚爲什麼紋理座標出錯了。紋理似乎傾斜對角地穿過一個簡單的立方體的臉,並拉伸......非常醜陋和錯誤。我一直在網上尋找一些官方出口商的來源,但我仍然無法弄清楚,所以我希望有人能給我一些提示或解決方案。Blender 2.6 JSON導出器,紋理座標問題
的一塊我使用訪問紋理座標代碼是這樣的:
# 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) > 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)
這是給我的紋理座標的名單,但不知何故,我做錯了,不正確的,因爲正如我上面解釋的那樣,紋理的外觀。
我不知道自己還能做些什麼,但顯示的代碼,因爲我想一切辦法,我能想到的周圍改變,所以這裏的地方上面的代碼片段的功能是:
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) > 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)
請問有人請告訴我我做錯了什麼?我已經在這幾天沒有進展了。