Example #1
0
    def ReadVertexArray(self, af, length, br, offset):
        br.SeekSet(offset)
        # ----------------------------------------------------------------------
        # -- convert array to float (so we have n + m cases, not n*m)
        data = []
        # -- vector<float>

        # -- print ("af.dataType=" + (af.dataType as string) + ": af.arrayType=" + (af.arrayType as string)  )

        if af.dataType == 3:  # u16
            tmp = []
            # -- size = length/2
            count = length / 2
            if int(count) != count:
                if common.GLOBALS.PARANOID:
                    raise ValueError('invalid count (length not even)')
                else:
                    log.error(
                        'invalid array length. last point will use whatever is neext in file'
                    )
            count = int(count)
            scale = 1 / (2**af.decimalPoint)
            tmp = [-1] * count
            data = tmp.copy()
            for i in range(count):
                tmp[i] = br.GetSHORT()
                data[i] = tmp[i] * scale
            # --throw "TODO: testing"
            # --messageBox "3"

        elif af.dataType == 4:  # f32
            count = length / 4
            if int(count) != count:
                if common.GLOBALS.PARANOID:
                    raise ValueError('invalid count (length not *4)')
                else:
                    log.error(
                        'invalid array length. last point will use whatever is neext in file'
                    )
            count = int(count)
            for _ in range(count):
                data.append(br.GetFloat())
            # --throw "TODO: testing2"
            # --print (format "ZZZ % %" length count )

        elif af.dataType == 5:  # rgb(a)
            tmp = []
            # -- size = length
            for _ in range(length):
                data.append(br.GetByte())
            # --messageBox "Vtx1: af.dataType == 5. NYI"

        else:
            if common.GLOBALS.PARANOID:
                raise ValueError('unknown array type %d' % af.dataType)
            else:
                log.warning("unknown array data type %d", af.dataType)

        # --print "DATA: "
        # --print data
        # ----------------------------------------------------------------------
        # -- stuff floats into appropriate vertex array
        if af.arrayType == 9:  # -- positions
            if af.componentCount == 0:  # -- xy [Needs testing]
                self.positions = []
                posCount = len(data) / 2
                if int(posCount) != posCount:
                    if common.GLOBALS.PARANOID:
                        raise ValueError('invalid posCount (length not even)')
                    else:
                        log.error(
                            'invalid array posCount. last point will use whatever is neext in file'
                        )

                posCount = math.ceil(posCount)
                k = 0
                for j in range(posCount):
                    pos = Vector((data[k], data[k + 1], 0))
                    self.positions.append(pos)
                    k += 2
                log.info("DT %d %d. Needs testings", af.dataType,
                         af.componentCount)

            elif af.componentCount == 1:  # -- xyz

                self.positions = []
                posCount = len(data) / 3
                if int(posCount) != posCount:
                    log.info("amount of position coordinates (%d) is not //3",
                             len(data))
                    for com in range(int(posCount - int(posCount) * 3)):
                        data.append(None)
                #raise ValueError('invalid posCount (length not *3)')
                posCount = int(posCount)
                k = 0
                for _ in range(posCount):
                    pos = Vector((data[k], data[k + 1], data[k + 2]))
                    self.positions.append(pos)
                    # pos.setXYZFlip(data[k], data[k+1], data[k+2])
                    k += 3
                if len(data) - posCount * 3 == 1:
                    pos = Vector((data[-2], data[-1], max(data) * 2))
                    self.positions.append(pos)

                if len(data) - posCount * 3 == 2:
                    pos = Vector((data[-1], max(data) * 2, max(data) * 2))
                    self.positions.append(pos)
            # --
            # --print (format "LEN %. COUNT %" length (data.count / 3))
            # --print self.positions

            # --messagebox (format "DT % %" af.dataType af.componentCount)

            else:
                log.warning(
                    "unsupported componentCount for self.positions array")
                # --messageBox (format "vtx1: unsupported componentCount for self.positions array: %" af.componentCount)

        elif af.arrayType == 0xa:  # -- normals TODO: Test [0xa=10]
            if af.componentCount == 0:  # -- xyz
                normalsCount = len(data) // 3
                if normalsCount != len(data):
                    log.info("length of normal coordinates (%d) is not //3",
                             len(data))
                self.normals = []
                # -- arrays.self.normals.resize(data.size()/3);
                k = 0
                for _ in range(normalsCount):
                    utmp = Vector((data[k], data[k + 1], data[k + 2]))
                    self.normals.append(utmp)
                    k += 3

                if len(data) - normalsCount * 3 == 1:
                    pos = Vector((data[-2], data[-1], max(data) * 2))
                    self.normals.append(pos)

                if len(data) - normalsCount * 3 == 2:
                    pos = Vector((data[-1], max(data) * 2, max(data) * 2))
                    self.normals.append(pos)
                    # --for(int j = 0, k = 0; j < arrays.self.normals.size(); ++j, k += 3)
                    # --  arrays.self.normals[j].setXYZ(data[k], data[k + 1], data[k + 2]);
            else:
                if common.GLOBALS.PARANOID:
                    raise ValueError(
                        "vtx1: unsupported componentCount for normals array")
                else:
                    log.warning("unsupported componentCount for normals array")

        elif af.arrayType == 0xb or af.arrayType == 0xc:  # -- color0 or color1
            index = af.arrayType - 0xb
            if len(self.colors) <= index:
                self.colors.append(list())
            if af.componentCount == 0:  # -- rgb
                # -- self.colors[data.count / 3] = 0 --initialize???
                colorCount = len(data) // 3
                k = 1
                for j in range(colorCount):
                    self.colors[index].append(
                        self.GetColor(data[k], data[k + 1], data[k + 2], 255))
                    k += 3
            elif af.componentCount == 1:  # -- rgba
                self.colors[index] = []
                colorCount = len(data) // 4
                k = 0  # fixed
                for j in range(colorCount):
                    self.colors[index].append(
                        self.GetColor(data[k], data[k + 1], data[k + 2],
                                      data[k + 3]))
                    k += 4

            else:
                if common.GLOBALS.PARANOID:
                    raise ValueError(
                        "vtx1: unsupported componentCount for colors array")
                else:
                    log.warning("unsupported componentCount for colors array")
        # -- texcoords 0 - 7 [13]

        elif af.arrayType == 0xd or\
             af.arrayType == 0xe or\
             af.arrayType == 0xf or\
             af.arrayType == 0x10 or\
             af.arrayType == 0x11 or\
             af.arrayType == 0x12 or\
             af.arrayType == 0x13 or\
             af.arrayType == 0x14:
            # -- std::vector<TexCoord> self.texCoords[8] self.texCoords
            index = (af.arrayType - 0xd)
            if af.componentCount == 0:  # --s
                # self.texCoords[index] = []  # DONE BEFORE
                # -- self.texCoords[index].resize(data.size());
                for j in range(len(data)):
                    utmp = TexCoord()
                    utmp.SetST(data[j], 0)
                    self.texCoords[index].append(utmp)

                # --for(int j = 0; j < arrays.self.texCoords[index].size(); ++j)
                # --  arrays.self.texCoords[index][j].setST(data[j], 0);

            elif af.componentCount == 1:  # -- st
                texCount = len(data) // 2
                if texCount * 2 != len(data):
                    log.warning("wrong length of UV coords (not even)")
                # self.texCoords[index] = []
                # -- arrays.self.texCoords[index].resize(data.size()/2);

                k = 0  # fixed
                for j in range(texCount):
                    utmp = TexCoord()
                    utmp.SetST(data[k], data[k + 1])
                    self.texCoords[index].append(utmp)
                    k += 2
                # --for(int j = 0, k = 0; j < arrays.self.texCoords[index].size(); ++j, k += 2)
                # --   arrays.self.texCoords[index][j].setST(data[k], data[k + 1]);

            else:
                if common.GLOBALS.PARANOID:
                    raise ValueError(
                        "vtx1: unsupported componentCount for texcoords array")
                else:
                    log.warning(
                        'unsupported componentCount for texcoords array')
        else:
            if common.GLOBALS.PARANOID:
                raise ValueError("WRONG ArrayType in VTX1")
            else:
                log.warning('WRONG ArrayType in VTX1')