MFnMesh.duplicateFaces

How can I duplicate faces as one seperate object?
This has to be in a MPxCommand for performance issues.
This code lets my maya crash… Any ideas?

import maya.OpenMaya as OpenMaya
import maya.cmds as cmds

plane = cmds.polyPlane(subdivisionsX=2,subdivisionsY=2)[0]
selList = OpenMaya.MSelectionList()
selList.add('pPlane1')
dagPath = OpenMaya.MDagPath()
selList.getDagPath(0, dagPath, OpenMaya.MObject())
dagPath.extendToShape()
fnMesh = OpenMaya.MFnMesh(dagPath)

faces = OpenMaya.MIntArray()
faces.append(1)
faces.append(2)

fnMesh.duplicateFaces(faces, OpenMaya.MFloatVector())
fnMesh.updateSurface()
cmds.select('%s.f[1:2]' % plane)

While this may not exactly answer your question, I’ve always just taken the easy route of:

  • Duplicate object using cmds.duplicate()
  • Select all faces on duplicate object
  • Remove faces you want to keep
  • Delete remaining faces

That leaves you with a new object and only the faces that you wish to keep. This works really well, though gets a tad bit slow with super dense meshes.

Just create the plane without construction history. From my limited knowledge of the mesh api, maya will freak out if you perform edits directly to a mesh that has history without using a node to perform the edit. If you want to maintain history then you need to write your own node or use the polyChipOff node, which is the node used by Maya to perform any poly extractions/separations/duplications

import maya.OpenMaya as OpenMaya
import maya.cmds as cmds

plane = cmds.polyPlane(ch=0, subdivisionsX=2, subdivisionsY=2)[0]
selList = OpenMaya.MSelectionList()
selList.add('pPlane1')
dagPath = OpenMaya.MDagPath()
selList.getDagPath(0, dagPath)
dagPath.extendToShape()
fnMesh = OpenMaya.MFnMesh(dagPath)

faces = OpenMaya.MIntArray()
faces.append(1)
faces.append(2)

fnMesh.duplicateFaces(faces, None)
fnMesh.updateSurface()
cmds.select('%s.f[4:5]' % plane)

Also, jtilden, being in Novato, do you work at 2k?

I’ll probably take the route of jtilden. I don’t want the indices to be changed when duplicating these faces.
Thanks for the help. polyChipOff is kinda slow. I have thousand of super dens meshes, so low execution time is priority.
I can’t find a way to keep the faces together and detach them. (I’m new to the api, any help welcome)

The indices of the existing faces don’t change, the new faces get added at the end of the polygon index range. f[4:5] refers to the newly created faces in that case.

I don’t have a lot of experience with the polygon api, but from what I can tell there is no way to detach faces and have it create a new mesh. You would probably be better off taking the selected faces and using their verts/normals/windings and creating a new mesh with that.

Using jtilden’s method in an MPxCommand would probably be the most straightforward method though.