我正在尝试为Blender 2.6x编写一个简单的JSON导出器,因为我能找到的唯一一个(http://code.google.com/p/blender-machete/)不适用于2.6。我从blender获取顶点,法线和索引没有任何问题,但是尽可能地尝试,我似乎无法弄清楚为什么纹理坐标出错了。纹理似乎在一个简单的立方体的对角线斜向倾斜,并且拉伸......真的很丑陋和错误。我一直在网上寻找一些官方出口商的消息来源,但我仍然无法理解,所以我希望有人可以给我一些提示或解决方案。
我用来访问纹理坐标的代码是:
# 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)
有人请告诉我我做错了什么吗?我已经在这几天了,没有任何进展。
答案 0 :(得分:0)
经过进一步研究,我想我可能知道这个问题,但我不确定这个......
这是一个不能使用共享顶点的经典示例。一旦引入顶点属性而不是位置,立方体就需要有24个顶点,而不是8.立方体的角不是共享顶点,因为它们没有相同的纹理坐标。
例如,前两个三角形由索引[0,1, 2,0,2,3],如果引用顶点和纹理坐标 阵列,面对很好。后两个三角形由 indices [0,4,5,0,5,1],并且引用顶点数组 正确制作立方体的第二面,产生纹理 坐标完全被打破。
我是对的,这是我的问题,还是我离开了?
修改强> 几乎通过使用更多顶点位置来获得纹理。现在唯一的问题是,一个简单立方体的一个面将被扭曲并对角线到其正确的位置。所有其他面孔看起来都很好。
这是几乎起作用的功能:
def get_json(objects, scene):
""" Currently only supports one scene.
Exports with -Z forward, Y up. """
object_number = -1
scene_data = []
# for every object in the scene
for object in bpy.context.scene.objects:
# if the object is a mesh
if object.type == 'MESH':
object_number += 1
# convert all the mesh's faces to triangles
bpy.ops.object.mode_set(mode='OBJECT')
object.select = True
bpy.context.scene.objects.active = object
# triangulate using new Blender 2.65 Triangulate modifier
bpy.ops.object.modifier_add(type='TRIANGULATE')
object.modifiers["Triangulate"].use_beauty = False
bpy.ops.object.modifier_apply(apply_as="DATA", modifier="Triangulate")
bpy.ops.object.mode_set(mode='OBJECT')
object.select = False
# add data to scene_data structure
scene_data.append({
"name" : object.name,
"vertices" : [],
"indices" : [],
"normals" : [],
"tex_coords" : []
})
vertex_number = -1
# for each face in the object
for face in object.data.polygons:
vertices_in_face = face.vertices[:]
# for each vertex in the face
for vertex in vertices_in_face:
vertex_number += 1
# store vertices in scene_data structure
scene_data[object_number]["vertices"].append( object.data.vertices[vertex].co.x + object.location.x )
scene_data[object_number]["vertices"].append( object.data.vertices[vertex].co.z + object.location.z )
scene_data[object_number]["vertices"].append( -(object.data.vertices[vertex].co.y + object.location.y) )
# store normals in scene_data structure
scene_data[object_number]["normals"].append( object.data.vertices[vertex].normal.x )
scene_data[object_number]["normals"].append( object.data.vertices[vertex].normal.z )
scene_data[object_number]["normals"].append( -(object.data.vertices[vertex].normal.y) )
# store indices in scene_data structure
scene_data[object_number]["indices"].append(vertex_number)
# texture coordinates
# bug: for a simple cube, one face's texture is warped
mesh = object.to_mesh(bpy.context.scene, True, 'PREVIEW')
if len(mesh.tessface_uv_textures) > 0:
for data in mesh.tessface_uv_textures.active.data:
scene_data[object_number]["tex_coords"].append( data.uv1.x )
scene_data[object_number]["tex_coords"].append( data.uv1.y )
scene_data[object_number]["tex_coords"].append( data.uv2.x )
scene_data[object_number]["tex_coords"].append( data.uv2.y )
scene_data[object_number]["tex_coords"].append( data.uv3.x )
scene_data[object_number]["tex_coords"].append( data.uv3.y )
return json.dumps(scene_data, indent=4)
我想知道只有一张脸的纹理被扭曲的原因是什么?我差不多了,但似乎无法想出这个。