首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搅拌机2.6 JSON出口商,纹理坐标故障

搅拌机2.6 JSON出口商,纹理坐标故障
EN

Stack Overflow用户
提问于 2012-12-05 21:05:04
回答 1查看 1.3K关注 0票数 0

我试图为Blender 2.6x编写一个简单的JSON导出程序,因为我能找到的唯一一个JSON导出程序( http://code.google.com/p/blender-machete/ )在2.6中无法工作。我没有任何困难从搅拌器得到顶点,法线和索引,但我试着,我只是不知道为什么纹理坐标是错误的。在一个简单的立方体表面上,纹理看起来是斜向倾斜的,并且拉伸.又丑又错。我一直在网上查找一些官方出口商的来源,但我仍然搞不清楚,所以我希望有人能给我一些提示或解决方案。

用于访问纹理坐标的代码如下:

代码语言:javascript
复制
    # 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 )

这给了我一个纹理坐标的列表,但不知何故,我做错了,因为纹理的不正确外观,正如我前面解释的。

除了显示代码之外,我不知道还能做什么,因为我已经尝试过用我所能想到的每一种方式来改变它,下面是下面的函数,上面的代码片段是:

代码语言:javascript
复制
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)

有人能告诉我我做错了什么吗?我已经干了几天了,没有任何进展。

EN

回答 1

Stack Overflow用户

发布于 2012-12-06 19:38:06

经过进一步的研究,我想我可能知道这个问题,但我不确定.

出发地:4811454#entry4811454

这是一个典型的例子,说明您何时不能使用共享顶点。一旦引入位置以外的顶点属性,立方体就需要有24个顶点,而不是8个。立方体的角不是共享顶点,因为它们没有相同的纹理坐标。 例如,前两个三角形由索引0、1、2、0、2、3组成,如果引用顶点和纹理坐标数组,则该面很好。后两个三角形由索引0、4、5、0、5、1组成,当顶点数组被引用以形成立方体的第二面时,所产生的纹理坐标被完全破坏。

我说得对吗,这是我的问题,还是我离得太远了?

编辑:几乎通过使用更多的顶点位置来获得纹理。现在唯一的问题是一个简单的立方体的一个面会被扭曲和对角到它正确的位置。其他的脸看起来都很好。

这是一个几乎可以工作的功能:

代码语言:javascript
复制
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)

我想知道只有一张脸的纹理被扭曲的原因是什么?我快到了,但似乎搞不清楚这件事。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13732429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档