A good way to store mesh components in a set ?

瀏覽次數:52 次
跳到第一則未讀訊息

Alejandro

未讀,
2021年6月17日 晚上7:44:252021/6/17
收件者:Python Programming for Autodesk Maya
Hello guys!,

What would be the fastest solution to store all the face components for each object I select into its correspondent set?

I'm trying something like this:

mesh = maya.cmds.ls(selection=True, type='mesh')[0]
selection_list = OpenMaya.MSelectionList()
selection_list.add(mesh)
dag_path = selection_list.getDagPath(0)
mfn_mesh = OpenMaya.MFnMesh(dag_path)

v_count, ids = mfn_mesh.getTriangles()

but these are only triangle counts and vtx indexes, how could I have the full face element and store in a set ?

thank you !



Marcus Ottosson

未讀,
2021年6月18日 凌晨3:01:432021/6/18
收件者:python_in...@googlegroups.com

Something like this should have you covered.

for mesh in cmds.ls(selection=True, type="mesh"):
    cmds.select("%s.f[0:]" % mesh)
    cmds.sets(name="%s_set" % mesh)

The .f[] syntax is for selecting faces, it’s what you see echoed in the Script Editor as you select a face interactively via the viewport.


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/24a2b1e9-f975-4461-97a1-e1155b235973n%40googlegroups.com.

Alejandro

未讀,
2021年6月18日 清晨7:38:532021/6/18
收件者:Python Programming for Autodesk Maya
Thanks a lot Marcus, 

I was looking for an option with the API for speed, since I have scenes with more than 1k objects, do you think it's not needed in this case ?

Marcus Ottosson

未讀,
2021年6月18日 上午9:42:352021/6/18
收件者:python_in...@googlegroups.com
You tell me. How long does it take currently? How often do you intend on running it? How long would you like it to take?

Alejandro

未讀,
2021年6月18日 上午9:56:392021/6/18
收件者:Python Programming for Autodesk Maya
I made a few tests on the usual number of elements, 2k / 1k, and it goes smoothly, not bad!

Thanks a lot Marcus :)

Marcus Ottosson

未讀,
2021年6月18日 上午10:12:392021/6/18
收件者:python_in...@googlegroups.com

Cool. :) It should be fast, you’re more or less passing the burden of actually iterating over the scene to those two commands which are already implemented in C++. So it’s likely that trying to replicate this via the Python API would be more expensive.


Juan Moraga

未讀,
2021年6月18日 中午12:54:492021/6/18
收件者:python_in...@googlegroups.com
I totally agree. I had a problem like this before and I ended up using a method similar to Marcus'.
As an additional note, something I found handy in the past was the "-flatten" flag of the cmds.ls() command, which will return you a list with one item for each component (faces, verts etc.).

for mesh in cmds.ls(selection=True, type="transform"):
    faces = cmds.ls(mesh + ".f[0:]", fl = True)
    print(faces)

By the way, wouldn't the ls() in the for loop be with the type = "transform" flag instead of type = "mesh" flag?

Kind regards,





--
Juan Moraga
Lead Technical Director @ MondoTV

Portfolio: https://juanmoraga.carbonmade.com/
LInkedin: https://www.linkedin.com/in/juanmoragamartin/

Rudi Hammad

未讀,
2021年6月19日 上午8:26:112021/6/19
收件者:Python Programming for Autodesk Maya
Not sure if that is what you are looking for, but if you want  to use the API you can do something like

mSel = OpenMaya.MSelectionList()
mSel.add("yourObject")
mDag = OpenMaya.MDagPath()
cmpMObj = OpenMaya.MObject()
mSel.getDagPath(0, mDag, cmpMObj)

faces = list()
mIt = OpenMaya.MItMeshPolygon(mDag, cmpMObj)
while not mIt.isDone():
    faces.append(mIt.index())
    mIt.next()
回覆所有人
回覆作者
轉寄
0 則新訊息