OBJFileLoader中的错误 [英] Error in OBJFileLoader

查看:93
本文介绍了OBJFileLoader中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

链接中的OBJFILELOADER.py文件存在错误- https://www.pygame.org/wiki/OBJFileLoader . 我认为存在python2到python3版本错误,但是我尝试更改 raise ValueError 并使用obj作为 obj =(filename)

There's error in OBJFILELOADER.py file from the link - https://www.pygame.org/wiki/OBJFileLoader . I think there's python2 to python3 version error, however I tried changing the raise ValueError thing and used obj as obj=(filename)

但是仍然有很多错误.

我从Blender中获取了.obj文件,我认为这不是问题. 谁能告诉我 Python3

I took the .obj file from blender, I don't think that's the issue. Can anyone tell me the exact objfileloader for Python3

错误-

 Traceback (most recent call last):
  File "/home/warted/anaconda3/lib/python3.6/site-packages/OpenGL/latebind.py", line 41, in __call__
    return self._finalCall( *args, **named )
TypeError: 'NoneType' object is not callable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "run.py", line 28, in <module>
    obj = OBJ('robotbase.obj')
  File "/home/warted/COMMAND CENTRE/SEDS/GUI/Simulate/objloader.py", line 97, in __init__
    glNormal3fv(self.normals[normals[i] - 1])
  File "/home/warted/anaconda3/lib/python3.6/site-packages/OpenGL/latebind.py", line 45, in __call__
    return self._finalCall( *args, **named )
  File "/home/warted/anaconda3/lib/python3.6/site-packages/OpenGL/wrapper.py", line 677, in wrapperCall
    pyArgs = tuple( calculate_pyArgs( args ))
  File "/home/warted/anaconda3/lib/python3.6/site-packages/OpenGL/wrapper.py", line 438, in calculate_pyArgs
    yield converter(args[index], self, args)
  File "/home/warted/anaconda3/lib/python3.6/site-packages/OpenGL/arrays/arrayhelpers.py", line 112, in asArraySize
    handler = typ.getHandler( incoming )
  File "/home/warted/anaconda3/lib/python3.6/site-packages/OpenGL/arrays/arraydatatype.py", line 53, in __call__
    typ.__module__, type.__name__, repr(value)[:50]
TypeError: ('No array-type handler for type builtins.type (value: <map object at 0x7f6bf0f8f4a8>) registered', <function asArrayTypeSize.<locals>.asArraySize at 0x7f6c01d6a620>)

推荐答案

您似乎只是将文件名分配给变量obj.

You just seem to assign the filename to the variable obj.

相反,您必须创建OBJ类的实例并传递文件/路径名称:

Instead you have to create an instance of the OBJ class and pass the file/path name:

obj = OBJ(filename)

要将脚本移植到Python 3,请查看链接的答案.

To port the script to Python 3 check out the linked answer.

尝试此更新版本:

import pygame
from OpenGL.GL import *

def MTL(filename):
    contents = {}
    mtl = None
    for line in open(filename, "r"):
        if line.startswith('#'): continue
        values = line.split()
        if not values: continue
        if values[0] == 'newmtl':
            mtl = contents[values[1]] = {}
        elif mtl is None:
            raise ValueError("mtl file doesn't start with newmtl stmt")
        elif values[0] == 'map_Kd':
            # load the texture referred to by this declaration
            mtl[values[0]] = values[1]
            surf = pygame.image.load(mtl['map_Kd'])
            image = pygame.image.tostring(surf, 'RGBA', 1)
            ix, iy = surf.get_rect().size
            texid = mtl['texture_Kd'] = glGenTextures(1)
            glBindTexture(GL_TEXTURE_2D, texid)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                GL_LINEAR)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
                GL_LINEAR)
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA,
                GL_UNSIGNED_BYTE, image)
        else:
            mtl[values[0]] = list(map(float, values[1:]))
    return contents

class OBJ:
    def __init__(self, filename, swapyz=False):
        """Loads a Wavefront OBJ file. """
        self.vertices = []
        self.normals = []
        self.texcoords = []
        self.faces = []

        material = None
        for line in open(filename, "r"):
            if line.startswith('#'): continue
            values = line.split()
            if not values: continue
            if values[0] == 'v':
                v = list(map(float, values[1:4]))
                if swapyz:
                    v = v[0], v[2], v[1]
                self.vertices.append(v)
            elif values[0] == 'vn':
                v = list(map(float, values[1:4]))
                if swapyz:
                    v = v[0], v[2], v[1]
                self.normals.append(v)
            elif values[0] == 'vt':
                self.texcoords.append(list(map(float, values[1:3])))
            elif values[0] in ('usemtl', 'usemat'):
                material = values[1]
            elif values[0] == 'mtllib':
                self.mtl = MTL(values[1])
            elif values[0] == 'f':
                face = []
                texcoords = []
                norms = []
                for v in values[1:]:
                    w = v.split('/')
                    face.append(int(w[0]))
                    if len(w) >= 2 and len(w[1]) > 0:
                        texcoords.append(int(w[1]))
                    else:
                        texcoords.append(0)
                    if len(w) >= 3 and len(w[2]) > 0:
                        norms.append(int(w[2]))
                    else:
                        norms.append(0)
                self.faces.append((face, norms, texcoords, material))

        self.gl_list = glGenLists(1)
        glNewList(self.gl_list, GL_COMPILE)
        glEnable(GL_TEXTURE_2D)
        glFrontFace(GL_CCW)
        for face in self.faces:
            vertices, normals, texture_coords, material = face

            mtl = self.mtl[material]
            if 'texture_Kd' in mtl:
                # use diffuse texmap
                glBindTexture(GL_TEXTURE_2D, mtl['texture_Kd'])
            else:
                # just use diffuse colour
                glColor(*mtl['Kd'])

            glBegin(GL_POLYGON)
            for i in range(len(vertices)):
                if normals[i] > 0:
                    glNormal3fv(self.normals[normals[i] - 1])
                if texture_coords[i] > 0:
                    glTexCoord2fv(self.texcoords[texture_coords[i] - 1])
                glVertex3fv(self.vertices[vertices[i] - 1])
            glEnd()
        glDisable(GL_TEXTURE_2D)
        glEndList()

这篇关于OBJFileLoader中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆