如何通过maya api 创建自己的locator,可以通过OpenMayaMPx.MPxLocatorNode 来实现。也可以将现有的曲线转为custom locator ,或者反过来转为曲线都可以通过api实现,甚至可以实现重绘 polygon。
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | ''' DESCRIPTION: create custom locator USAGE: Add this file to your plug-in path and load it in Maya from the Plug-in Manager (Window -> Settings/Preferences -> Plug-in Manager mel: createNode myCustomLocator; python: import maya.cmds as cmds; cmds.createNode('myCustomLocator') REQUIRES: customLocator.py - MAYA PYTHON Plug-in AUTHORS: Deng Zhenggang - zg@cgdzg.com, www.cgdzg.com Version: 1.0 - July 15, 2014 - Initial Revision ''' import sys import maya.OpenMaya as OpenMaya import maya.OpenMayaMPx as OpenMayaMPx import maya.OpenMayaRender as OpenMayaRender import maya.OpenMayaUI as OpenMayaUI kVersionNumber = '1.0' kAuthor = 'cgdzg' kPluginNodeName = "myCustomLocator" kPluginNodeId = OpenMaya.MTypeId(0x8801A) glRenderer = OpenMayaRender.MHardwareRenderer.theRenderer() glFT = glRenderer.glFunctionTable() # shape definition shapeVertexList = {'cube' : ([[-1.0, 1.0, 1.0], [-1.0, 1.0, -1.0], [1.0, 1.0, -1.0], [1.0, 1.0, 1.0], [-1.0, 1.0, 1.0], [-1.0, -1.0, 1.0], [-1.0, -1.0, -1.0], [-1.0, 1.0, -1.0], [-1.0, 1.0, 1.0], [-1.0, -1.0, 1.0], [1.0, -1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, -1.0], [1.0, -1.0, -1.0], [1.0, -1.0, 1.0], [1.0, -1.0, -1.0], [-1.0, -1.0, -1.0]]), 'square' : ([[-1.0, 0.0, -1.0], [-1.0, 0.0, 1.0], [1.0, 0.0, 1.0], [1.0, 0.0, -1.0], [-1.0, 0.0, -1.0]]), 'squareCross': ([[1.0, 0.0, 0.0], [0.6, 0.0, -0.4], [0.6, 0.0, -0.2], [0.2, 0.0, -0.2], [0.2, 0.0, -0.6], [0.4, 0.0, -0.6], [0.0, 0.0, -1.0], [-0.4, 0.0, -0.6], [-0.2, 0.0, -0.6], [-0.2, 0.0, -0.2], [-0.6, 0.0, -0.2], [-0.6, 0.0, -0.4], [-1.0, 0.0, 0.0], [-0.6, 0.0, 0.4], [-0.6, 0.0, 0.2], [-0.2, 0.0, 0.2], [-0.2, 0.0, 0.6], [-0.4, 0.0, 0.6], [0.0, 0.0, 1.0], [0.4, 0.0, 0.6], [0.2, 0.0, 0.6], [0.2, 0.0, 0.2], [0.6, 0.0, 0.2], [0.6, 0.0, 0.4], [1.0, 0.0, 0.0]]), 'circle' : ([[0.78361162489122504, 4.7982373409884682e-17, -0.78361162489122382], [-1.2643170607829326e-16, 6.7857323231109134e-17, -1.1081941875543879], [-0.78361162489122427, 4.7982373409884713e-17, -0.78361162489122427], [-1.1081941875543879, 1.9663354616187859e-32, -3.2112695072372299e-16], [-0.78361162489122449, -4.7982373409884694e-17, 0.78361162489122405], [-3.3392053635905195e-16, -6.7857323231109146e-17, 1.1081941875543881], [0.78361162489122382, -4.7982373409884719e-17, 0.78361162489122438], [1.1081941875543879, -3.6446300679047921e-32, 5.9521325992805852e-16], [0.78361162489122504, 4.7982373409884682e-17, -0.78361162489122382], [-1.2643170607829326e-16, 6.7857323231109134e-17, -1.1081941875543879], [-0.78361162489122427, 4.7982373409884713e-17, -0.78361162489122427]]), 'ball' : ([[0.0, 1.0, 0.0], [0.0, 0.92388000000000003, 0.382683], [0.0, 0.70710700000000004, 0.70710700000000004], [0.0, 0.382683, 0.92388000000000003], [0.0, 0.0, 1.0], [0.0, -0.382683, 0.92388000000000003], [0.0, -0.70710700000000004, 0.70710700000000004], [0.0, -0.92388000000000003, 0.382683], [0.0, -1.0, 0.0], [0.0, -0.92388000000000003, -0.382683], [0.0, -0.70710700000000004, -0.70710700000000004], [0.0, -0.382683, -0.92388000000000003], [0.0, 0.0, -1.0], [0.0, 0.382683, -0.92388000000000003], [0.0, 0.70710700000000004, -0.70710700000000004], [0.0, 0.92388000000000003, -0.382683], [0.0, 1.0, 0.0], [0.382683, 0.92388000000000003, 0.0], [0.70710700000000004, 0.70710700000000004, 0.0], [0.92388000000000003, 0.382683, 0.0], [1.0, 0.0, 0.0], [0.92388000000000003, -0.382683, 0.0], [0.70710700000000004, -0.70710700000000004, 0.0], [0.382683, -0.92388000000000003, 0.0], [0.0, -1.0, 0.0], [-0.382683, -0.92388000000000003, 0.0], [-0.70710700000000004, -0.70710700000000004, 0.0], [-0.92388000000000003, -0.382683, 0.0], [-1.0, 0.0, 0.0], [-0.92388000000000003, 0.382683, 0.0], [-0.70710700000000004, 0.70710700000000004, 0.0], [-0.382683, 0.92388000000000003, 0.0], [0.0, 1.0, 0.0], [0.0, 0.92388000000000003, -0.382683], [0.0, 0.70710700000000004, -0.70710700000000004], [0.0, 0.382683, -0.92388000000000003], [0.0, 0.0, -1.0], [-0.382683, 0.0, -0.92388000000000003], [-0.70710700000000004, 0.0, -0.70710700000000004], [-0.92388000000000003, 0.0, -0.382683], [-1.0, 0.0, 0.0], [-0.92388000000000003, 0.0, 0.382683], [-0.70710700000000004, 0.0, 0.70710700000000004], [-0.382683, 0.0, 0.92388000000000003], [0.0, 0.0, 1.0], [0.382683, 0.0, 0.92388000000000003], [0.70710700000000004, 0.0, 0.70710700000000004], [0.92388000000000003, 0.0, 0.382683], [1.0, 0.0, 0.0], [0.92388000000000003, 0.0, -0.382683], [0.70710700000000004, 0.0, -0.70710700000000004], [0.382683, 0.0, -0.92388000000000003], [0.0, 0.0, -1.0]]), 'cross' : ([[-0.24998399999999998, -2.2203039407031609e-16, -0.99993599999999994], [-0.24998399999999998, -5.5507598517579023e-17, -0.24998399999999998], [-0.99993599999999994, -5.5507598517579023e-17, -0.24998399999999998], [-0.99993599999999994, 5.5507598517579023e-17, 0.24998399999999998], [-0.24998399999999998, 5.5507598517579023e-17, 0.24998399999999998], [-0.24998399999999998, 2.2203039407031609e-16, 0.99993599999999994], [0.24998399999999998, 2.2203039407031609e-16, 0.99993599999999994], [0.24998399999999998, 5.5507598517579023e-17, 0.24998399999999998], [0.99993599999999994, 5.5507598517579023e-17, 0.24998399999999998], [0.99993599999999994, -5.5507598517579023e-17, -0.24998399999999998], [0.24998399999999998, -5.5507598517579023e-17, -0.24998399999999998], [0.24998399999999998, -2.2203039407031609e-16, -0.99993599999999994], [-0.24998399999999998, -2.2203039407031609e-16, -0.99993599999999994]]) } shapeList = ['cube', 'square', 'squareCross', 'circle', 'ball', 'cross'] defaultShape = 'cube' def getShapeId(val): for i in range(0, len(shapeVertexList)): if shapeList[i] == val: return i def getShapeName(val): return shapeList[val] def getLinearKnotList(point): knot_list = [] for i in range(0, len(shapeVertexList)): knot_list.append(float(i)) return knot_list # Register plugin def initializePlugin(obj): plugin = OpenMayaMPx.MFnPlugin(obj, kAuthor, kVersionNumber) try: # register node plugin.registerNode(kPluginNodeName, kPluginNodeId, CustomLocator.node_creator, CustomLocator.node_initializer, OpenMayaMPx.MPxNode.kLocatorNode) except: sys.stderr.write("Failed to register node: %s" % kPluginNodeName) raise # De-regsiter plugin def uninitializePlugin(obj): plugin = OpenMayaMPx.MFnPlugin(obj) try: plugin.deregisterNode(kPluginNodeId) except: sys.stderr.write("Failed to deregister node: %s" % kPluginNodeName) raise # CustomLocator class CustomLocator(OpenMayaMPx.MPxLocatorNode): colorObj = OpenMaya.MObject() kColorAttrLongName = 'color' kColorAttrName = 'col' lineStyle = OpenMaya.MObject() kLineStyleAttrName = 'lst' kLineStyleAttrLongName = 'lineStyle' width = OpenMaya.MObject() kWidthAttrName = 'w' kWidthAttrLongName = 'width' kTransparency = OpenMaya.MObject() kTransparencyAttrName = 'ltp' kTransparencyAttrLongName = 'transparency' presets = OpenMaya.MObject() kPresetsAttrName = 'pre' kPresetsAttrLongName = 'presets' # Initialize def __init__(self): OpenMayaMPx.MPxLocatorNode.__init__(self) def draw(self, view, path, style, status): thisNode = self.thisMObject() dagThis = OpenMaya.MFnDagNode(thisNode) # ----------------------- local position and scale X/Y/Z val = [0, 0, 0, 0, 0, 0, 0, 0, 0] val[0] = dagThis.findPlug('localScaleX').asDouble() val[1] = dagThis.findPlug('localScaleY').asDouble() val[2] = dagThis.findPlug('localScaleZ').asDouble() val[3] = dagThis.findPlug('localPositionX').asDouble() val[4] = dagThis.findPlug('localPositionY').asDouble() val[5] = dagThis.findPlug('localPositionZ').asDouble() # ----------------------- line width lineWidth = OpenMaya.MPlug(thisNode, self.width).asInt() lineStyle = OpenMaya.MPlug(thisNode, self.lineStyle).asInt() fTrans = OpenMaya.MPlug(thisNode, self.transparency).asFloat() preset = OpenMaya.MPlug(thisNode, self.presets).asInt() # ----------------------- color plug = OpenMaya.MPlug(thisNode, self.colorObj) color = plug.asMDataHandle().asFloat3() #----------------------- start openGL view.beginGL() if status == OpenMayaUI.M3dView.kFlatShaded or style == OpenMayaUI.M3dView.kGouraudShaded: glFT.glPushAttrib(OpenMayaRender.MGL_CURRENT_BIT) glFT.glPopAttrib() # draw color if status == OpenMayaUI.M3dView.kDormant: glFT.glColor4f(color[0], color[1], color[2], fTrans) #-------------------- glFT.glEnable(OpenMayaRender.MGL_BLEND) glFT.glBlendFunc(OpenMayaRender.MGL_SRC_ALPHA, OpenMayaRender.MGL_ONE_MINUS_SRC_ALPHA) glFT.glDepthFunc(OpenMayaRender.MGL_LESS) if lineStyle == 0: stipple, factor, pattern = 0, 1, 43690 if lineStyle == 1: stipple, factor, pattern = 1, 2, 43690 if lineStyle == 2: stipple, factor, pattern = 1, 1, 43690 if lineStyle == 3: stipple, factor, pattern = 1, 1, 255 #----------------------------- glFT.glLineWidth(lineWidth) if stipple: glFT.glLineStipple(factor, pattern) glFT.glEnable(OpenMayaRender.MGL_LINE_STIPPLE) #------------------------------- draw self.drawContext(preset, val) glFT.glLineWidth(1.0) ################## # Reset draw settings kLeadColor = 18 kActiveColor = 15 kActiveAffectedColor = 8 kDormantColor = 4 kHiliteColor = 17 if status == OpenMayaUI.M3dView.kLead: view.setDrawColor(kLeadColor, OpenMayaUI.M3dView.kActiveColors) elif status == OpenMayaUI.M3dView.kActive: view.setDrawColor(kActiveColor, OpenMayaUI.M3dView.kActiveColors) elif status == OpenMayaUI.M3dView.kActiveAffected: view.setDrawColor(kActiveAffectedColor, OpenMayaUI.M3dView.kActiveColors) elif status == OpenMayaUI.M3dView.kDormant: view.setDrawColor(kDormantColor, OpenMayaUI.M3dView.kActiveColors) elif status == OpenMayaUI.M3dView.kHilite: view.setDrawColor(kHiliteColor, OpenMayaUI.M3dView.kActiveColors) ############################# if stipple: glFT.glDisable(OpenMayaRender.MGL_LINE_STIPPLE) # ----------------------- stop openGL view.endGL() def drawContext(self, preset, val): shapeName = getShapeName(preset) glFT.glBegin(OpenMayaRender.MGL_LINE_STRIP) # get shape vertex last = len(shapeVertexList[shapeName]) - 1 for i in range(last): glFT.glVertex3f(shapeVertexList[shapeName][i][0]*val[0]+val[3], shapeVertexList[shapeName][i][1]*val[1]+val[4], shapeVertexList[shapeName][i][2]*val[2]+val[5]) glFT.glVertex3f(shapeVertexList[shapeName][i+1][0]*val[0]+val[3], shapeVertexList[shapeName][i+1][1]*val[1]+val[4], shapeVertexList[shapeName][i+1][2]*val[2]+val[5]) glFT.glEnd() def boundingBox(self): return OpenMaya.MBoundingBox() def postConstructor(self): # Rename Transform1 to maya stand name. OpenMaya.MFnDependencyNode(self.thisMObject()).setName("CustomLocatorShape#") @staticmethod def node_creator(): return OpenMayaMPx.asMPxPtr(CustomLocator()) @staticmethod def node_initializer(): nAttr = OpenMaya.MFnNumericAttribute() eAttr = OpenMaya.MFnEnumAttribute() # ----------------------- presets CustomLocator.presets = eAttr.create(CustomLocator.kPresetsAttrLongName, CustomLocator.kPresetsAttrName) for i in range(0, len(shapeList)): eAttr.addField(shapeList[i], i) eAttr.setKeyable(True) eAttr.setReadable(True) CustomLocator.addAttribute(CustomLocator.presets) # ----------------------- line style CustomLocator.lineStyle = eAttr.create(CustomLocator.kLineStyleAttrLongName, CustomLocator.kLineStyleAttrName) eAttr.addField('Solid', 0) eAttr.addField('Rough', 1) eAttr.addField('Fine', 2) eAttr.addField('Dash', 3) eAttr.setKeyable(True) CustomLocator.addAttribute(CustomLocator.lineStyle) # ----------------------- line width CustomLocator.width = nAttr.create(CustomLocator.kWidthAttrLongName, CustomLocator.kWidthAttrName, OpenMaya.MFnNumericData.kInt, 2.0) nAttr.setMin(1) nAttr.setMax(10) nAttr.setKeyable(True) CustomLocator.addAttribute(CustomLocator.width) # ----------------------- transparency CustomLocator.transparency = nAttr.create(CustomLocator.kTransparencyAttrLongName, CustomLocator.kTransparencyAttrName,OpenMaya.MFnNumericData.kFloat, 1.0) nAttr.setMin(0.0) nAttr.setMax(1.0) nAttr.setKeyable(True) CustomLocator.addAttribute(CustomLocator.transparency) # ----------------------- color CustomLocator.colorObj = nAttr.createColor(CustomLocator.kColorAttrLongName, CustomLocator.kColorAttrName) # set default color nAttr.setDefault(0.0, 0.016, 0.376) nAttr.setKeyable(True) nAttr.setReadable(True) nAttr.setWritable(True) nAttr.setStorable(True) nAttr.setUsedAsColor(True) CustomLocator.addAttribute(CustomLocator.colorObj) |
Comments (0)