This article is the 7th day article of ** Maya Advent Calender 2019 **.
When you create a modeling tool in Maya, you may get the vertex positions of the selected object. However, in a model with many vertices, it is slow to acquire all vertex positions with ** MEL ** or ** cmds **, which is a level that hinders modeling work. ** Open Maya ** is often introduced as a way to increase the processing speed, but it is a high threshold for me, and I was wondering if there was a way to increase the processing speed with ** cmds **.
I used to write this way.
from maya import cmds
sel_obj = cmds.ls(sl=True)
verts = cmds.ls(sel_obj[0] + '.vtx[*]', fl=True)
vert_pos = [cmds.xform(vert, t=True, ws=True, q=True) for vert in verts]
However, if you get it in this way, it will take a long time if the object has many vertices.
First, create a very beautiful sphere with ** Subdivisions Axis ** and ** Subdivisions Height ** in the scene, both ** 200 **.
The number of vertices is ** 39802 **.
Select this sphere and measure the above script using Python's time module.
It took about 2 seconds. At this speed, modeling work will be hindered.
from maya import cmds
sel_obj = cmds.ls(sl=True)
verts = cmds.ls(sel_obj[0] + '.vtx[*]')
vert_pos = cmds.xform(verts, t=True, ws=True, q=True)
The ** fl ** flag of the ** ls ** command is removed, and the list of acquired vertices is collectively given to the argument of ** xform **. Now let's measure the processing speed again.
** About 0.06 seconds! It's about 33 times faster. ** **
However, if you look at the returned values, it is a one-dimensional array, so in order to return the same values as when processing with for, you need to add the following code and replace it with a two-dimensional array for each XYZ.
vert_pos = [vert_pos[i:i + 3] for i in range(0, len(vert_pos), 3)]
As mentioned above, it was a method to increase the processing speed of vertex position acquisition.
It's a very small article, but it was a discovery for me, so I wrote a memo. Maya has a function that can take such a list as an argument, which can be considerably faster than processing with for, or even when giving a list, it can be faster by eliminating duplication.
As far as I can see, I can't see in the official docs if the argument can be listed, so it's annoying to just test it ...
If you have any mistakes or unclear points in the article, I would appreciate it if you could write them down. Until the end Thank you for reading.
Recommended Posts