I have a python library for importing our game assets to Maya. Part of the process involves transforming coordinate systems and scale to Maya. It makes use of several open maya api 2.0 classes to do so: MMatrix, MQuaternion, Mvector, MPoint, and MTransformationMatrix. The linear algebra methods of Maya api 2.0 make it straightforward.
Now my task is to generate Maya-independent data classes for meshes, skeletons , skinning etc for use with other DCCs. So I wish to remove any Maya dependencies in the coordinate system and scale conversions. I’m currently exploring numpy - but right off the bat I see no equivalent of a quaternion, for example. So I wonder if there are any python packages perhaps better for 3D game mathing?
I suppose , alternately, there could be a way to just use Maya api 2.0 in another package? I assume not…
If you want built-in quaternions, you could use scipy. Specifically scipy.spatial.transform.Rotation. Unfortunately that Rotation class doesn’t allow for more than 1 array dimension (ie, it’s either a single rotation, or a list of rotations. But not a list of lists of rotations). You can get around that, but sometimes its annoying if you were doing numpy broadcasting.
And I’d say to stick with numpy. Maybe somebody has made something nicer since I last looked, but numpy has been able to handle everything I’ve thrown at it, and I’ve thrown a LOT at it. Plus once you start coding like numpy wants (absolutely NO loops if you can help it) it is stupidly fast.
Another thing you could leverage is the USD python library. The Gf and Vt modules have a lot of the linear algebra stuff built in. Plus, the Vt arrays have FromNumpy constructors that quickly read the underlying memory. And they follow the buffer protocol, so np.array(vtarray) is just as fast. Though I think I’d still say to stick with numpy because it’s a lot more portable.
I also tried working with blender’s mathutils library for a while. It was decent, but they don’t put it on pypi.org and dealing with compiling it myself was an absolute pain.
I feel like the natural evolution of that task is you’re going to have to edit meshes outside of a dcc. So I’ve got a difficult problem for you to make that easier in the future.
When I did this I had to ask on stackoverflow because I couldn’t figure it out, but working through and understanding the answer I got really opened my eyes. This problem leveled me up with numpy.
Can you reverse order of faces in a mesh given numpy arrays for the counts and indices? Can this be done fully vectorized?
ChatGPT gave me a good answer given that as a prompt.
“fully vectorized” means “absolutely no python loops, not even list comprehensions”
The motivation for that is I was once given a mesh right out of ZBrush’s dynamesh that had 300000 polygons, and one of those polygons had 2000 vertices. All the solutions I was able to come up with either broke the indices array into 300k parts and looped over them in python (slow) or made an array with shape (300k, 2k) and I got memory errors. And I didn’t want to have 2 separate ways to do the same task. That’s twice the bugs.
The reason I needed to reorder (not just reverse, but reversing makes an easier prompt) the faces is so that I could consistently sha256-hash the faces/indices arrays. That way if modeling published a new mesh with a different vertex order, we could flag it.
Similar techniques can be used to do things like “reverse the windings of every face”, or “move a vertex to the average of its neighbors”, or “compute the normals for each face-vertex”, or “roll the indices of each face so the lowest index is always first”, or “build a neighbor counts, neighbor indices pair of arrays”
thanks again. keeping note of this thread so I can return to it later
because every task you listed smells like something we’ll want to do at some point.