Dorian Fevrier's blog - Mot-clé - xformJe m’appelle FEVRIER Dorian, je suis infographiste 3D passionné par mon métier, l’informatique en général, l’internet, la programmation et l’évolution de tout ce petit monde. Vous trouverez sur ce blog des tutoriaux, mes coups de cœurs, avis, etc.2024-01-02T23:48:05+01:00FEVRIER Dorianurn:md5:695d9c73474c33ce3dab043823509c4bDotclearQuickly retrieve vertex positions of a Maya mesh (English Translation)urn:md5:5d032309b99261355833f3413e46f8432011-09-27T22:16:00+02:002013-07-26T18:05:41+02:00NarannScript et codeapienmayameshpythonvertexxform<p><img src="https://www.fevrierdorian.com/blog/public/billets/2011_03_30_Recuperer_Position_Vertex_Rapidement/recuperer_Position_Vertex_Rapidement_tn.png" alt="recuperer_Position_Vertex_Rapidement_tn.png" style="float:left; margin: 0 1em 1em 0;" title="recuperer_Position_Vertex_Rapidement_tn.png, mar. 2011" height="150" width="150" />If you ever had to recover vertex positions of a mesh object, you probably crash yourself against the <em>xform</em> command and his legendary slowness. :baffed:</p>
<p>In this post, I purpose a piece of Python script using the Python Maya API that allows you to faster recover the list of all vertex positions of an object.</p>
<p>Primarily didactic, these codes may interest peoples who want to look a little more deeply how to use the API while having a concrete application. :laClasse:</p> <h3>Using xform</h3>
<p>Here is the <a href="http://download.autodesk.com/us/maya/2011help/Commands/xform.html">xform</a> version of the script:</p>
<pre class="python python"><span style="color: #ff7700;font-weight:bold;">def</span> getVtxPos<span style="color: black;">(</span> shapeNode <span style="color: black;">)</span> :
vtxWorldPosition = <span style="color: black;">[</span><span style="color: black;">]</span> <span style="color: #808080; font-style: italic;"># will contain positions un space of all object vertex</span>
vtxIndexList = <span style="color: #dc143c;">cmds</span>.<span style="color: black;">getAttr</span><span style="color: black;">(</span> shapeNode+<span style="color: #483d8b;">".vrts"</span>, multiIndices=<span style="color: #008000;">True</span> <span style="color: black;">)</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> vtxIndexList :
curPointPosition = <span style="color: #dc143c;">cmds</span>.<span style="color: black;">xform</span><span style="color: black;">(</span> <span style="color: #008000;">str</span><span style="color: black;">(</span>shapeNode<span style="color: black;">)</span>+<span style="color: #483d8b;">".pnts["</span>+<span style="color: #008000;">str</span><span style="color: black;">(</span>i<span style="color: black;">)</span>+<span style="color: #483d8b;">"]"</span>, query=<span style="color: #008000;">True</span>, translation=<span style="color: #008000;">True</span>, worldSpace=<span style="color: #008000;">True</span> <span style="color: black;">)</span> <span style="color: #808080; font-style: italic;"># [1.1269192869360154, 4.5408735275268555, 1.3387055339628269]</span>
vtxWorldPosition.<span style="color: black;">append</span><span style="color: black;">(</span> curPointPosition <span style="color: black;">)</span>
<span style="color: #ff7700;font-weight:bold;">return</span> vtxWorldPosition</pre>
<h3>With Maya API</h3>
<p>Alternative to xform.</p>
<pre class="python python"><span style="color: #ff7700;font-weight:bold;">import</span> maya.<span style="color: #dc143c;">OpenMaya</span> <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #dc143c;">OpenMaya</span>
<span style="color: #ff7700;font-weight:bold;">def</span> particleFillSelection<span style="color: black;">(</span> <span style="color: black;">)</span>:
<span style="color: #808080; font-style: italic;"># get the active selection</span>
selection = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MSelectionList</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MGlobal</span>.<span style="color: black;">getActiveSelectionList</span><span style="color: black;">(</span> selection <span style="color: black;">)</span>
iterSel = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MItSelectionList</span><span style="color: black;">(</span>selection, <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MFn</span>.<span style="color: black;">kMesh</span><span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># go througt selection</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff7700;font-weight:bold;">not</span> iterSel.<span style="color: black;">isDone</span><span style="color: black;">(</span><span style="color: black;">)</span>:
<span style="color: #808080; font-style: italic;"># get dagPath</span>
dagPath = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MDagPath</span><span style="color: black;">(</span><span style="color: black;">)</span>
iterSel.<span style="color: black;">getDagPath</span><span style="color: black;">(</span> dagPath <span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># create empty point array</span>
inMeshMPointArray = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MPointArray</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># create function set and get points in world space</span>
currentInMeshMFnMesh = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MFnMesh</span><span style="color: black;">(</span>dagPath<span style="color: black;">)</span>
currentInMeshMFnMesh.<span style="color: black;">getPoints</span><span style="color: black;">(</span>inMeshMPointArray, <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MSpace</span>.<span style="color: black;">kWorld</span><span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># put each point to a list</span>
pointList = <span style="color: black;">[</span><span style="color: black;">]</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">(</span> inMeshMPointArray.<span style="color: black;">length</span><span style="color: black;">(</span><span style="color: black;">)</span> <span style="color: black;">)</span> :
pointList.<span style="color: black;">append</span><span style="color: black;">(</span> <span style="color: black;">[</span>inMeshMPointArray<span style="color: black;">[</span>i<span style="color: black;">]</span><span style="color: black;">[</span><span style="color: #ff4500;">0</span><span style="color: black;">]</span>, inMeshMPointArray<span style="color: black;">[</span>i<span style="color: black;">]</span><span style="color: black;">[</span><span style="color: #ff4500;">1</span><span style="color: black;">]</span>, inMeshMPointArray<span style="color: black;">[</span>i<span style="color: black;">]</span><span style="color: black;">[</span><span style="color: #ff4500;">2</span><span style="color: black;">]</span><span style="color: black;">]</span> <span style="color: black;">)</span>
<span style="color: #ff7700;font-weight:bold;">return</span> pointList</pre>
<blockquote><p>Notice: I thought that specifying the size of the list from the start would gain time but it changes nothing. It seems that <a href="http://stackoverflow.com/questions/311775/python-create-a-list-with-initial-capacity">Python handles it well</a>. So, I learned a new saying: <em>Premature optimization is the root of all evil</em>. :gniarkgniark:</p></blockquote>
<h3>Benchmarks</h3>
<p>Well, it's fine to say it's faster. But how much? :reflexionIntense:</p>
<p>Here are times I have with a 50x50 pSphere smoothed 4 (633602 vertices):</p>
<ul>
<li>With xform: 39 sec</li>
<li>With Python Maya API: 4 sec</li>
</ul>
<p>10x faster! This would give me almost want to code in C++, just to see results (which is suppose to be 10x faster than Python!)!!! :grenadelauncher:</p>
<h3>Conclusion</h3>
<p>Well now it's up to you to know when use it.</p>
<p>You can try to optimize <a href="https://www.fevrierdorian.com/blog/post/2011/03/14/Remplir-un-mesh-de-spheres-dans-Maya-La-methode-d-un-senior">Djelloul's algorythm</a> and let me know your results! :sourit:</p>
<p>Now, I couldn't do without the API if I have to retrieve vertex positions of a mesh. It's much faster. Even if it's a little more complicate, I admit, but when you have this code, you keep it and use it at the right time! :aupoil:</p>
<p>Hope you like this trick!</p>
<p>See you soon!</p>
<center>:marioCours:</center>
Récupérer rapidement la position des vertices d'un mesh Mayaurn:md5:912013d3913b678655c3562e58a746b32011-03-30T22:40:00+02:002013-07-26T18:11:05+02:00NarannScript et codeapicodefrmayapythonscripttutovertexxform<p><img src="https://www.fevrierdorian.com/blog/public/billets/2011_03_30_Recuperer_Position_Vertex_Rapidement/recuperer_Position_Vertex_Rapidement_tn.png" alt="recuperer_Position_Vertex_Rapidement_tn.png" style="float:left; margin: 0 1em 1em 0;" title="recuperer_Position_Vertex_Rapidement_tn.png, mar. 2011" height="150" width="150" />Si vous avez déjà été amené à récupérer la position dans l'espace de tous les vertices d'un objet, vous avez surement dû vous heurter à la commande <em>xform</em> et à sa lenteur légendaire. :baffed:</p>
<p>Dans ce billet, je vous propose un petit bout de script Python utilisant l'API Maya en Python qui permet de récupérer, plus rapidement, la liste de toutes les positions des vertices d'un objet.</p>
<p>Principalement didactique, ces codes, pourront intéresser les personnes qui souhaitent regarder un peu plus profondément comment utiliser l'API tout en ayant un cas concret d'application. :laClasse:</p> <h3>En utilisant xform</h3>
<p>Voici la version du script qui utilise la commande <a href="http://download.autodesk.com/us/maya/2011help/Commands/xform.html">xform</a>:</p>
<pre class="python python"><span style="color: #ff7700;font-weight:bold;">def</span> getVtxPos<span style="color: black;">(</span> shapeNode <span style="color: black;">)</span> :
vtxWorldPosition = <span style="color: black;">[</span><span style="color: black;">]</span> <span style="color: #808080; font-style: italic;"># contiendra les positions dans l'espace de tout les vertex de l'objet</span>
vtxIndexList = <span style="color: #dc143c;">cmds</span>.<span style="color: black;">getAttr</span><span style="color: black;">(</span> shapeNode+<span style="color: #483d8b;">".vrts"</span>, multiIndices=<span style="color: #008000;">True</span> <span style="color: black;">)</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> vtxIndexList :
curPointPosition = <span style="color: #dc143c;">cmds</span>.<span style="color: black;">xform</span><span style="color: black;">(</span> <span style="color: #008000;">str</span><span style="color: black;">(</span>shapeNode<span style="color: black;">)</span>+<span style="color: #483d8b;">".pnts["</span>+<span style="color: #008000;">str</span><span style="color: black;">(</span>i<span style="color: black;">)</span>+<span style="color: #483d8b;">"]"</span>, query=<span style="color: #008000;">True</span>, translation=<span style="color: #008000;">True</span>, worldSpace=<span style="color: #008000;">True</span> <span style="color: black;">)</span> <span style="color: #808080; font-style: italic;"># [1.1269192869360154, 4.5408735275268555, 1.3387055339628269]</span>
vtxWorldPosition.<span style="color: black;">append</span><span style="color: black;">(</span> curPointPosition <span style="color: black;">)</span>
<span style="color: #ff7700;font-weight:bold;">return</span> vtxWorldPosition</pre>
<h3>La version avec l'API Maya</h3>
<p>Alternative à xform.</p>
<pre class="python python"><span style="color: #ff7700;font-weight:bold;">import</span> maya.<span style="color: #dc143c;">OpenMaya</span> <span style="color: #ff7700;font-weight:bold;">as</span> <span style="color: #dc143c;">OpenMaya</span>
<span style="color: #ff7700;font-weight:bold;">def</span> particleFillSelection<span style="color: black;">(</span> <span style="color: black;">)</span>:
<span style="color: #808080; font-style: italic;"># get the active selection</span>
selection = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MSelectionList</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MGlobal</span>.<span style="color: black;">getActiveSelectionList</span><span style="color: black;">(</span> selection <span style="color: black;">)</span>
iterSel = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MItSelectionList</span><span style="color: black;">(</span>selection, <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MFn</span>.<span style="color: black;">kMesh</span><span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># go througt selection</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff7700;font-weight:bold;">not</span> iterSel.<span style="color: black;">isDone</span><span style="color: black;">(</span><span style="color: black;">)</span>:
<span style="color: #808080; font-style: italic;"># get dagPath</span>
dagPath = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MDagPath</span><span style="color: black;">(</span><span style="color: black;">)</span>
iterSel.<span style="color: black;">getDagPath</span><span style="color: black;">(</span> dagPath <span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># create empty point array</span>
inMeshMPointArray = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MPointArray</span><span style="color: black;">(</span><span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># create function set and get points in world space</span>
currentInMeshMFnMesh = <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MFnMesh</span><span style="color: black;">(</span>dagPath<span style="color: black;">)</span>
currentInMeshMFnMesh.<span style="color: black;">getPoints</span><span style="color: black;">(</span>inMeshMPointArray, <span style="color: #dc143c;">OpenMaya</span>.<span style="color: black;">MSpace</span>.<span style="color: black;">kWorld</span><span style="color: black;">)</span>
<span style="color: #808080; font-style: italic;"># put each point to a list</span>
pointList = <span style="color: black;">[</span><span style="color: black;">]</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">(</span> inMeshMPointArray.<span style="color: black;">length</span><span style="color: black;">(</span><span style="color: black;">)</span> <span style="color: black;">)</span> :
pointList.<span style="color: black;">append</span><span style="color: black;">(</span> <span style="color: black;">[</span>inMeshMPointArray<span style="color: black;">[</span>i<span style="color: black;">]</span><span style="color: black;">[</span><span style="color: #ff4500;">0</span><span style="color: black;">]</span>, inMeshMPointArray<span style="color: black;">[</span>i<span style="color: black;">]</span><span style="color: black;">[</span><span style="color: #ff4500;">1</span><span style="color: black;">]</span>, inMeshMPointArray<span style="color: black;">[</span>i<span style="color: black;">]</span><span style="color: black;">[</span><span style="color: #ff4500;">2</span><span style="color: black;">]</span><span style="color: black;">]</span> <span style="color: black;">)</span>
<span style="color: #ff7700;font-weight:bold;">return</span> pointList</pre>
<blockquote><p>Note j'ai pensé qu'en spécifiant la taille de la liste dès le départ on gagnerait encore du temps mais ça ne change rien. Il semblerait que <a href="http://stackoverflow.com/questions/311775/python-create-a-list-with-initial-capacity">Python gère ça bien</a>. Du coup, j'ai appris un nouveau dicton: <em>Premature optimization is the root of all evil</em>. :gniarkgniark:</p></blockquote>
<h3>Les temps</h3>
<p>Bon, c'est bien beau de dire que c'est plus rapide. Mais de combien? :reflexionIntense:</p>
<p>Voici les temps que j’obtiens pour une pSphere 50x50 smoothée 4 (soit 633602 vertices):</p>
<ul>
<li>Avec xform: 39 sec</li>
<li>Avec l'API Python de Maya: 4 sec</li>
</ul>
<p>10x plus rapide mine de rien. Ça me donnerait presque envie de le coder en C++, juste pour voir! :grenadelauncher:</p>
<h3>Conclusion</h3>
<p>Bon, et bien maintenant c'est à vous de voir quoi en tirer.</p>
<p>Vous pouvez essayer d'optimiser <a href="https://www.fevrierdorian.com/blog/post/2011/03/14/Remplir-un-mesh-de-spheres-dans-Maya-La-methode-d-un-senior">l’algorithme de Djelloul</a> et me faire part de vos résultats! :sourit:</p>
<p>Toujours est t'il que personnellement, je ne pourrais plus me passer de l'API si j'ai à récupérer la position des vertices d'un mesh c'est beaucoup plus rapide! Moyennant une complexité supérieur, je veux bien l'avouer, mais quand on a ce bout de code, on le garde et on le ressort au bon moment! :aupoil:</p>
<p>A bientôt!</p>
<center>:marioCours:</center>