Better way?

Surely, there is a better way to convert float Mats into double Mats from Python?

        matF = self.transform.getMat()
        matD = Mat4D()
        # stupid
        matD.setRow (0, VBase4D(matF.getRow(0).getCell(0), matF.getRow(0).getCell(1), matF.getRow(0).getCell(2), matF.getRow(0).getCell(3)))
        matD.setRow (1, VBase4D(matF.getRow(1).getCell(0), matF.getRow(1).getCell(1), matF.getRow(1).getCell(2), matF.getRow(1).getCell(3)))
        matD.setRow (2, VBase4D(matF.getRow(2).getCell(0), matF.getRow(2).getCell(1), matF.getRow(2).getCell(2), matF.getRow(2).getCell(3)))
        matD.setRow (3, VBase4D(matF.getRow(3).getCell(0), matF.getRow(3).getCell(1), matF.getRow(3).getCell(2), matF.getRow(3).getCell(3)))

:cry:

why not do a for loop?

        for x in range(0,3):
          matD.setRow (x, VBase4D(matF.getRow(x).getCell(0), matF.getRow(x).getCell(1), matF.getRow(x).getCell(2), matF.getRow(x).getCell(3))) 

Hm, in C++, there is LCAST. I don’t think we have an equivalent in Python.

Also see here:
blueprints.launchpad.net/panda3 … converters
But unfortunately, that doesn’t work for matrices, as *mat will result in a tuple of four LMatrix4f::Row objects.

I 've got two other neat solutions (besides the fact that I think there should be a conversion method) - a readable one:

matD = Mat4D()
for i in range(4):
    for j in range(4):
        matD[i][j] = matF[i][j]

and a one-liner:

matD = Mat4D(*sum([[matF[i][j] for i in range(4)] for j in range(4)], []))

which is arguably very non-pythonic, but fun :slight_smile:

Mat4D( *list(m[0])+list(m[1])+list(m[2])+list(m[3]) )

OR

Mat4D( *sum([list(e) for e in m], []) )

m is your matF.

okay, that’s better. I’ve come up with

matD = Mat4D(*(cell for row in matF for cell in row))

which I think is both readable and concise.

inline loops are sooo “yuk!” -but hey, only my opinion

Yeah, I was wondering why the constructors for MatF/MatD didn’t just accept their opposite-precision cousin.

I didn’t realize that this problem extended to all the other dual-precision data representations, too, or how they are templated representations.

Everybody else: Thanks for the cute suggestions, especially the inline loop one. Glad to see people are keeping the spirit of my original code, but honestly I was more hoping someone would just say, “Oh yeah, here’s a direct conversion method that’s kind of obscure and not documented.” :slight_smile:

At least I learned that Mats can be treated as a nestled list. Didn’t occur to me to check for that, since I was just looking for a method that dumped the Mat as a raw string of digits, a Python equiv of get_data.

Yeah, we really should expose some Python function to be the equivalent of LCAST. I guess it just doesn’t come up that often, sorry about that.

David

The only reason I encountered it was finding out that EggData nodes store their transforms in double-precision matrices.

I was wanting to make my loaded Egg data conform to what would eventually be the net transform of its scene graph parent node.

        matF = parentNodePath.getMat()
        matD = Mat4D()
        # less stupid, but still stupid
        for i in range(4):
            for j in range(4):
                matD[i][j] = matF[i][j]
        eggNode.transform (matD)

Luckily this isn’t a per-frame operation so speed isn’t of the essence. :slight_smile: