Skip to content

Instantly share code, notes, and snippets.

Created November 17, 2015 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fa586ab574848e05bbe5 to your computer and use it in GitHub Desktop.
Save anonymous/fa586ab574848e05bbe5 to your computer and use it in GitHub Desktop.
plug-in to manage mesh invisible faces
'''
import maya.cmds as cmds
cmds.loadPlugin("/path/to/invisibleFacesMixer.py", quiet=True)
cmds.file(new=True, f=True)
cmds.flushUndo()
cmds.unloadPlugin("invisibleFacesMixer.py")
cmds.loadPlugin("/path/to/invisibleFacesMixer.py")
#lastFile = cmds.optionVar(q='RecentFilesList')[-1]; cmds.file(lastFile, o=True, f=True)
clm = cmds.createNode('invisibleFacesMixer')
f1 = ' '.join([str(i) for i in range(80 ,100)])
f2 = ' '.join([str(i) for i in range(120,140)])
f3 = ' '.join([str(i) for i in range(160,180)])
cmds.setAttr('%s.invisibleFacesData[0].faceIds' %clm, f1, type='string')
cmds.setAttr('%s.invisibleFacesData[1].faceIds' %clm, f2, type='string')
cmds.setAttr('%s.invisibleFacesData[2].faceIds' %clm, f3, type='string')
meshGenerator = cmds.createNode('polyTorus')
mesh1 = cmds.createNode('mesh')
mesh2 = cmds.createNode('mesh')
cmds.move(5, 0, 0, r=True)
cmds.connectAttr('%s.output'%meshGenerator, '%s.inMesh' %mesh1)
cmds.connectAttr('%s.outMesh' %mesh1, '%s.inMesh' %clm)
cmds.connectAttr('%s.outMesh' %clm, '%s.inMesh' %mesh2)
cmds.setAttr('%s.displayInvisibleFaces' %mesh2, 1)
cmds.sets([mesh1, mesh2], e=True, forceElement='initialShadingGroup')
cmds.select(clm, r=True)
cmds.delete(mesh1,ch=True)
# As of now, if you play with the various invisibleFacesMixer1 hide attributes, works ok
# But if you perform this operation, it breaks the node behavior
cmds.select('%s.f[115]' %mesh1, r=True)
cmds.polyHole(assignHole=True)
'''
import sys
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
kPluginNodeTypeName = "invisibleFacesMixer" # type du node
kPluginNodeTypeId = OpenMaya.MTypeId(0x8705)
class InvisibleFacesMixer(OpenMayaMPx.MPxNode): # Premiere lette Majuscule
# class variables
# Ici on fait des MObject() representant les attributs du node
inMesh = OpenMaya.MObject()
outMesh = OpenMaya.MObject()
compListData = OpenMaya.MObject()
compList = OpenMaya.MObject()
showHide = OpenMaya.MObject()
def __init__(self):
OpenMayaMPx.MPxNode.__init__(self)
def compute(self, plug, data):
if not plug == self.outMesh:
return OpenMaya.kUnknownParameter
hInMesh = data.inputValue(self.inMesh)
oInMesh = hInMesh.data()
hOutMesh = data.outputValue(InvisibleFacesMixer.outMesh)
dataCreator = OpenMaya.MFnMeshData()
newOutputData = dataCreator.create()
fnOutMesh = OpenMaya.MFnMesh()
fnOutMesh.copy(oInMesh, newOutputData)
fnInMesh = OpenMaya.MFnMesh(oInMesh)
invFacesIn = fnInMesh.getInvisibleFaces()
print 'invFaces before clean:', invFacesIn
# If I perform the following block, the node
# fails to produce correct results if there is any
# invisibleFaces on the source mesh
# Culprit code start
# Cleaning incoming invisibleFaces
a = OpenMaya.MUintArray()
for i in invFacesIn:
a.append(i)
fnOutMesh.setInvisibleFaces(a, True)
invFacesOut = fnOutMesh.getInvisibleFaces()
print 'invFaces after clean:', invFacesOut
# Culprit code end
arrayHandle = data.inputArrayValue(self.compListData)
count = arrayHandle.elementCount()
idsSet = set()
for i in xrange(count):
arrayHandle.jumpToArrayElement(i)
hshowHide = arrayHandle.inputValue().child(self.showHide)
hCompIds = arrayHandle.inputValue().child(self.compList)
showHideVal = hshowHide.asBool()
compIds = hCompIds.asString()
if not showHideVal or not compIds:
continue
for compId in compIds.split(' '):
idsSet.add(int(compId))
b = OpenMaya.MUintArray()
for compId in idsSet:
b.append(compId)
print 'invFaces to install:', b
fnOutMesh.setInvisibleFaces(b, False)
invFacesOut2 = fnOutMesh.getInvisibleFaces()
print 'invFaces final:', invFacesOut2
hOutMesh.setMObject(newOutputData)
data.setClean(plug)
def nodeInitializer():
nAttrInMesh = OpenMaya.MFnTypedAttribute()
InvisibleFacesMixer.inMesh = nAttrInMesh.create('inMesh', 'i', OpenMaya.MFnData.kMesh)
# nAttrInMesh.setStorable(True)
nAttrInMesh.setConnectable(True)
nAttrOutMesh = OpenMaya.MFnTypedAttribute()
InvisibleFacesMixer.outMesh = nAttrOutMesh.create('outMesh', 'o', OpenMaya.MFnData.kMesh)
nAttrOutMesh.setConnectable(True)
# nAttrOutMesh.setStorable(True)
# compListData attribute
cAttrCompListData = OpenMaya.MFnCompoundAttribute()
InvisibleFacesMixer.compListData = cAttrCompListData.create('invisibleFacesData', 'ifd')
cAttrCompListData.setStorable(True)
cAttrCompListData.setArray(True)
nAttrshowHide = OpenMaya.MFnNumericAttribute()
InvisibleFacesMixer.showHide = nAttrshowHide.create('hide', 'h', OpenMaya.MFnNumericData.kBoolean, 0)
nAttrshowHide.setStorable(True)
nAttrshowHide.setKeyable(True)
nAttrCompList = OpenMaya.MFnTypedAttribute()
InvisibleFacesMixer.compList = nAttrCompList.create('faceIds', 'fids', OpenMaya.MFnData.kString)
nAttrCompList.setStorable(True)
# Add child attributes to compound
cAttrCompListData.addChild(InvisibleFacesMixer.showHide)
cAttrCompListData.addChild(InvisibleFacesMixer.compList)
# add attributes
InvisibleFacesMixer.addAttribute(InvisibleFacesMixer.inMesh)
InvisibleFacesMixer.addAttribute(InvisibleFacesMixer.outMesh)
InvisibleFacesMixer.addAttribute(InvisibleFacesMixer.compListData)
# affects
InvisibleFacesMixer.attributeAffects(InvisibleFacesMixer.inMesh, InvisibleFacesMixer.outMesh)
InvisibleFacesMixer.attributeAffects(InvisibleFacesMixer.showHide, InvisibleFacesMixer.outMesh)
InvisibleFacesMixer.attributeAffects(InvisibleFacesMixer.compList, InvisibleFacesMixer.outMesh)
InvisibleFacesMixer.attributeAffects(InvisibleFacesMixer.compListData, InvisibleFacesMixer.outMesh)
def nodeCreator():
return OpenMayaMPx.asMPxPtr(InvisibleFacesMixer())
def initializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject, "Nicolas Combecave", "1.0", "Any")
try:
mplugin.registerNode(kPluginNodeTypeName, kPluginNodeTypeId, nodeCreator, nodeInitializer)
except:
sys.stderr.write("Failed to register node: %s\n" % kPluginNodeTypeName)
raise
def uninitializePlugin(mobject):
mplugin = OpenMayaMPx.MFnPlugin(mobject)
try:
mplugin.deregisterNode(kPluginNodeTypeId)
except:
sys.stderr.write("Failed to unregister node: %s\n" % kPluginNodeTypeName)
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment