[zz]Maya C++ API Programming Tips
Maya C++ API Programming Tips
source : http://wanochoi.com/?page_id=1588
How to handle the multiple outputs of a DG node
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
|
MStatus MyNode::initialize() { MFnNumericAttribute nAttr; outputsObj = nAttr.create( "outputs" , "outputs" , MFnNumericData.kFloat, 0.f ); nAttr.setArray( true ); addAttribute( outputsObj ); ... } MStatus MyNode::compute( const MPlug& plug, MDataBlock& data ) { if ( plug != outputsObj ) { return MS::kUnknownParameter; } MArrayDataHandle outputsHnd = data.outputArrayValue( outputsObj ); const int nOutputs = ( int )outputsHnd.elementCount(); for ( int i=0; i<100; ++i ) { outputsHnd.jumpToElement( i ); outputsHnd.outputValue().set( i ); } outputsHnd.setAllClean(); return MS::kSuccess; } |
How to capture the current viewport as an image file
1
2
3
4
|
MImage image; M3dView view = M3dView::active3dView(); view.readColorBuffer( image, true ); image.writeToFile( "snapshot.jpg" , "jpg" ); |
How to save the current frame buffer to a JPG file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
GLint viewport[4]; glGetIntegerv( GL_VIEWPORT, viewport ); int width = viewport[2]; int height = viewport[3]; int depth = 4; unsigned char * pixels = new unsigned int [width*height*depth]; glReadBuffer( GL_FRONT ); glReadPixels( 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels ); MImage image; image.setPixels( pixels, width, height ); image.writeToFile( "snapshot.jopg" , "jpg" ); delete [] pixels; |
How to set the names of a custom DG node and its parent node
1
2
3
4
5
6
7
8
9
|
void myNode::postConstructor() { MPxNode::postConstructor(); MObject thisMObj = thisMObject(); MFnDependencyNode nodeFn; nodeFn.setObject( thisMObj ); nodeFn.setName( "myNodeShape#" ); } |
How to compile my source code conditionally according to the Maya API version
1
2
3
4
5
6
7
|
#if MAYA_API_VERSION &gt;= 201300 ... #elif MAYA_API_VERSION &gt;= 201400 ... #elif MAYA_API_VERSION &gt;= 201500 ... #endif |
How to get the current Maya version
1
2
|
MString mayaVer = MGlobal::mayaVersion(); int apiVer = MGlobal::apiVersion(); |
How to avoid the conflict with Cuda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
... #define short2 MAYA_short2 #define short3 MAYA_short3 #define long2 MAYA_long2 #define long3 MAYA_long3 #define int2 MAYA_int2 #define int3 MAYA_int3 #define float2 MAYA_float2 #define float3 MAYA_float3 #define double2 MAYA_double2 #define double3 MAYA_double3 #define double4 MAYA_double4 #include &lt;cuda.h&gt; ... |
How to set the MFloatPoint from a MPoint
1
2
3
|
MPoint dp; // double type MFloatPoint fp; // float type fp.setCast( dp ); |
How to get the pixel values of an image
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
|
MImage img; img.readFromFile( "filePathName" ); // load from a file int w=0, h=0; // image width, height img.getSize( w, h ); int d = img.depth(); // image depth unsigned char * c = (unsigned char *)NULL; if ( img.pixelType() == MImage::kByte ) { c = img.pixels(); } for ( int j=0; j&lt;h; ++j ) for ( int i=0; i&lt;w; ++i ) {{ int idx = d*(i+j*w); unsigned char pixelVal[4]; // RGB(d=3) or RGBA(d=4) for ( int k=0; k&lt;d; ++k ) { pixelVal[k] = c[idx++]; } ... }} |
How to get the DAG path from a node name
1
2
3
4
5
6
7
|
MString nodeName( "nodeName" ); MDagPath dagPath; MSelectionList sList; if ( MGlobal::getSelectionListByName( nodeName, sList ) ) { sList.getDagPath( 0, dagPath ); } |
How to get the DG node object from a node name
1
2
3
4
5
6
7
|
MString nodeName( "nodeName" ); MObject nodeObj; MSelectionList sList; if ( MGLobal::getSelectionListByName( nodeName, sList ) ) { sList.getDependNode( 0, nodeObj ); } |
How to get the DAG path from a DAG node object
1
2
3
4
|
MObject dagNodeObj = ...; MDagPath dagPath; MFnDagNode dagFn( dagNodeObj ); dagFn.getPath( dagPath ); |
How to get the node name from a node object
1
2
3
4
5
6
7
8
9
10
|
MObject nodeObj = ...; MString nodeName; if ( nodeObj.hasFn( MFn::kDagNode ) ) { MFnDagNode dagNodeFn( nodeObj ); nodeName = dagNode.fullPathName(); } else if ( nodeObj.hasFn( MFn::kDependencyNode ) ) { MFnDependencyNode dgNodeFn( nodeObj ); nodeName = dgNodeFn.name(); } |
How to get the parent DAG node object
1
2
3
4
5
6
7
|
MObject parentDagNodeObj; MFnDagNode dagFn( thisMObject() ); MObject obj = dagFn.parent( 0 ); if ( !obj.isNull() ) { parentDagNodeObj = obj; } |
How to get the shape node DAG path from a transform DAG path
1
2
3
|
MDagPath xformDagPath = ... MDagPath shapeDagPath = xformDagPath; shapeDagPath.extendToShape(); |
How to get the DAG paths of selected mesh shape nodes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
MDagPathArray dagPaths; MSelectionList sList; MGlobal::getActiveSelectionList( sList ); MItSelectionList itr( sList, MFn::kMesh ); for ( ; !itr.isDone(); itr.next() ) { MDagPath dagPath; itr.getDagPath( dagPath ); MFnDagNode dagFn( dagPath ); if ( dagFn.isIntermediateObject() ) { continue ; } dagPaths.append( dagPath ); } |
How to get the all of NURBS curve node objects in the current scene
1
2
3
4
5
6
|
MObjectArray curveObjs; MItDag itr( MItDag::kDepthFirst, MFn::kNurbsCurve ); for ( ; !itr.isDone(); itr.next() ) { curveObjs.append( itr.item() ); } |
How to get the list of a selected mesh polygons
1
2
3
4
5
6
7
8
9
10
11
12
|
MIntArray selectedPolygonList; MSelectionList sList; MGlobal::getActiveSelectionList( sList ); MItSelectionList sItr( sList, MFn::kMeshPolygonComponent ); MDagPath dagPath; MObject componentObj; sItr.getDagPath( dagPath, componentObj ); MFnSingleIndexedComponent sCompFn( componentObj ); sCompFn.getElements( selectedPolygonList ); |
How to get the all of fields in the current scene
1
2
3
4
5
6
|
MItDag itr( MItDag::kDepthFirst, MFn::kField ); for ( ; !itr.isDone(); itr.next() ) { MFnField fieldFn( itr.item() ); ... } |
How to get the world matrix of the current DAG node
1
2
3
4
5
6
7
8
|
MObject thisNodeObj = thisMObject(); MFnDependencyNode thisNodeFn( thisNodeObj ); MObject worldMatrixObj = thisNodeFn.attribute( "worldMatrix" ); MPlug worldMatrixPlg( thisNodeObj, worldMatrixObj ); worldMatrixPlg = worldMatrixPlg.elementByLogicalIndex( 0 ); worldMatrixPlg.getValue( worldMatrixObj ); MFnMatrixData worldMatrixData( worldMatrixObj ); Matrix worldMatrix = worldMatrixData.matrix(); |
How to connect the output plug to the parent automatically when a custom locator node is created
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
|
class MyLocator { ... bool connected; ... }; MyLocator::MyLocator() { connected = false ; } void MyLocator::draw( M3dView&amp; view, ...) { if ( !connected ) { MObject thisNodeObj = thisMObject(); MFnDagNode dagFn( thisNodeObj ); MObject parentNodeObj = dagFn.parent( 0 ); dagFn.setObject( parentNodeObj ); MPlug parentPlg = dagFn.findPlug( "plugName" ); MPlug outputPlg = MPlug( thisNodeObj, outputObj ); if ( !outputPlg.isConnected() ) { MDGModifier dgMod; dgMod.connect( outputPlg, parentPlg ); dgMod.doIt(); connected = true ; } } ... } |
How to make a custom locator node un-selectable
1
2
3
4
5
|
void MyLocator::draw( M3dView&amp; view, ...) { if ( view.selectMode() ) { return ; } ... } |
How to detect which input attribute is changed
1
2
3
4
5
6
7
8
9
|
MStatus MyCustomNode::compute( const MPlug&amp; plug, MDataBlock&amp; data ) { if ( plug != outputObj ) { return MS::kUnknownParameter; } bool inputIsChanged = !data.isClean( inputObj ); float input = data.inputValue( inputObj ).asFloat(); ... } |
Note) It doesn’t work when inputObj is array type.
How to detect whether the current state is batch mode
1
2
3
4
|
if ( MGlobal::mayaState() == MGlobal::kBatch ) { ... } |
How to detect whether the current viewport is Viewport 2.0
1
2
3
4
5
|
M3dView view = M3dView::active3dView(); if ( view.getRendererName() == M3dView::kViewport2Renderer ) { ... } |
How not to draw a custom locator node while mouse interactions (>=2015)
1
2
3
4
5
6
7
8
|
void MyLocator::draw( M3dView&amp; view, ...) { if ( MHWRender::MFrameContext::inUserInteraction() || MHWRender::MFrameContext::userChangingViewContext() ) { ... } } |
How to draw a text in draw() of a custom locator node
1
2
3
4
5
6
7
8
|
void MyLocator::draw( M3dView&amp; view, ...) { ... view.beginGL(); view.drawText( MString( "text" ), MPoint(0,0,0), M3dView::kLeft ); view.endGL(); ... } |
How to copy the input mesh to the output mesh in compute() of a custom node
1
2
3
4
5
6
7
8
9
10
|
MStatus MyNode::compute( const MPlug&amp; plug, MDataBlock&amp; data ) { ... MFnMesh newMeshFn; MFnMeshData dataCreator; MObject newMeshData = dataCreator.create(); newMeshFn.copy( data.inputValue( inMeshObj ).asMeshTransformed(), newMeshData ); data.outputValue( outMeshObj ).set( newMeshData ); ... } |
How to deform the input mesh in compute() of a custom node like such a deformer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
MStatus MyNode::compute( const MPlug&amp; plug, MDataBlock&amp; data ) { ... MPointArray deformedPoints = ... MFnMesh newMeshFn; MFnMeshData dataCreator; MObject newMeshData = dataCreator.create(); newMeshFn.copy( data.inputValue( inMeshObj ).asMeshTransformed(), newMeshData ); newMeshFn.setPoints( deformedPoints ); data.outputValue( outMeshObj ).set( newMeshData ); ... } |
How to handle array attribute
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
|
class MyCustomNode { ... MObject valuesObj; ... MDoubleArray values; }; MObject MyCustomNode::valuesObj; MStatus MyCustomNode::initialize() { ... valuesObj = tAttr.create( "values" , "values" , MFnData::kDoubleArray ); ... } MStatus MyCustomNode::compute( const MPlug&amp; plug, MDataBlock&amp; data ) { ... // get MFnDoubleArrayData arrayData; MObject dataObj = data.inputValue( valuesObj ).data(); arrayData.setObject( dataObj ); int numValues = 100; // set if ( arrayData.length() != numValues ) { MDoubleArray array( numValues, 0.0 ); dataObj = arrayData.create( array ); data.outputValue( valuesObj ).set( dataObj ); } ... } // MEL makePaintable -at doubleArray MyCustomNode values; setAttr MyCustomNode1.values -type doubleArray 3 1.0 2.0 3.0; |
How to get the value of an attribute anywhere in a custom node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
MObject thisNodeObj = thisMObject(); double v0 = MPlug( thisNodeObj, attr0Obj ).asDouble(); float v1 = MPlug( thisNodeObj, attr1Obj ).asFloat(); int v2 = MPlug( thisNodeObj, attr2Obj ).asInt(); short v3 = MPlug( thisNodeObj, attr3Obj ).asShort(); bool v4 = MPlug( thisNodeObj, attr4Obj ).asBool(); MTime v5 = MPlug( thisNodeObj, attr5Obj ).asMTime(); char v6 = MPlug( thisNodeObj, attr6Obj ).asChar(); MString v7 = MPlug( thisNodeObj, attr7Obj ).asString(); MColor c; MPlug plg( thisNodeObj, attrObj ); plg.child(0).getValue( c.r ); plg.child(1).getValue( c.g ); plg.child(2).getValue( c.b ); |
How to get the pointer of the other connected node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
OtherNodeClassName* otherNodePtr = (OtherNodeClassName*)NULL; MObject thisNodeObj = thisMObject(); MFnDependencyNode thisNodeFn( thisNodeObj ); MPlug plg = thisNodeFn.findPlug( inputAttrObj ); MPlugArray connectedPlgs; if ( plg.isConnected() ) { if ( plg.isSource() ) { plg.connectedTo( connectedPlgs, false , true ); } else if ( plg.isDestination() ) { plg.connectedTo( connectedPlgs, true , false ); } MFnDependencyNode otherNodeFn( connectedPlgs[0].node() ); if ( otherNodeFn.typeId() == OtherNodeClassName::id ) { otherNodePtr = (OtherNodeClassName*)otherNodeFn.userNode(); } } |
How to restore GL states in draw() of a custom locator node
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
void MyLocator::draw( M3dView&amp; view, ...) { float lastPointSize=0; glGetFloatv( GL_POINT_SIZE, &amp;lastPointSize ); float lastLineWidth=0; glGetFloatv( GL_LINE_WIDTH, &amp;lastLineWidth ); bool lightingWasOn = glIsEnabled( GL_LIGHTING ) ? true : false ; if ( lightingWasOn ) { glDisable( GL_LIGHTING ); } view.beginGL(); ... view.endGL(); glPointSize( lastPointSize ); glLineWidth( lastLineWidth ); if ( lightingWasOn ) { glEnable( GL_LIGHTING ); } } |
How to call compute() of a custom locator without output connection
1
2
3
4
5
6
7
|
void MyLocator::draw( M3dView&amp; view, ...) { MObject thisNodeObj = thisMObject(); MObject obj = MPlug( thisNodeObj, outputObj ).asMObject(); ... } |
How to get the normal vector of a current camera
1
2
3
4
|
M3dView view = M3dView::active3dView(); MDagPath camDagPath; view.getCamera( camDagPath ); MVector cameraZ = MVector(0,0,1) * camDagPath.inclusiveMatrix(); |
How to create attributes in initialize() of a custom node
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
|
MStatus MyLocator::initialize() { MRampAttribute rAttr; MFnEnumAttribute eAttr; MFnUnitAttribute uAttr; MFnTypedAttribute tAttr; MFnNumericAttribute nAttr; MFnMessageAttribute gAttr; timeObj = uAttr.create( "time" , "time" , MFnUnitAttribute::kTime, 0.0 ); addAttribute( timeObj ); angleObj = uAttr.create( "angle" , "angle" , MFnUnitAttribute::kAngle, 0.0 ); addAttribute( angleObj ); boolObj = nAttr.create( "bool" , "bool" , MFnNumericData::kBoolean, false ); addAttribute( boolObj ); intObj = nAttr.create( "int" , "int" , MFnNumericData::kInt, 0 ); addAttribute( intObj ); floatObj = nAttr.create( "float" , "float" , MFnNumericData::kFloat, 0.f ); addAttribute( floatObj ); doubleObj = nAttr.create( "double" , "double" , MFnNumericData::kDouble, 0.0 ); addAttribute( doubleObj ); int2Obj = nAttr.create( "int2" , "int2" , MFnNumericData::k2Int, 0 ); addAttribute( int2Obj ); int3Obj = nAttr.create( "int3" , "int3" , MFnNumericData::k3Int, 0 ); addAttribute( int3Obj ); float2Obj = nAttr.create( "float2" , "float2" , MFnNumericData::k2Float, 0.f ); addAttribute( float2Obj ); float3Obj = nAttr.create( "float3" , "float3" , MFnNumericData::k3Float, 0.f ); addAttribute( float3Obj ); double2Obj = nAttr.create( "double2" , "double2" , MFnNumericData::k2Double, 0.0 ); addAttribute( double2Obj ); double3Obj = nAttr.create( "double3" , "double3" , MFnNumericData::k3Double, 0.0 ); addAttribute( double3Obj ); stringObj = tAttr.create( "string" , "string" , MFnData::kString, "abc" ); addAttribute( stringObj ); matrixObj = tAttr.create( "matrix" , "matrix" , MFnMatrixAttribute::kDouble ); addAttribute( matrixObj ); curveObj = tAttr.create( "curve" , "curve" , MFnData::kNurbsCurve ); addAttribute( curveObj ); meshObj = tAttr.create( "mesh" , "mesh" , MFnData::kMesh ); addAttribute( meshObj ); iaObj = tAttr.create( "iArray" , "iArray" , MFnData::kIntArray ); addAttribute( iaObj ); faObj = tAttr.create( "fArray" , "fArray" , MFnData::kFloatArray ); addAttribute( faObj ); daObj = tAttr.create( "dArray" , "dArray" , MFnData::kDoubleArray ); addAttribute( daObj ); paObj = tAttr.create( "pArray" , "pArray" , MFnData::kPointArray ); addAttribute( paObj ); vaObj = tAttr.create( "vArray" , "vArray" , MFnData::kVectorArray ); addAttribute( vaObj ); saObj = tAttr.create( "sArray" , "sArray" , MFnData::kStringArray ); addAttribute( saObj ); msgObj = gAttr.create( "message" , "message" ); addAttribute( msgObj ); clrObj = nAttr.createColor( "color" , "color" ); addAttribute( clrObj ); pntObj = nAttr.createPoint( "point" , "point" ); addAttribute( pntObj ); enumObj = eAttr.create( "enum" , "enum" 0 ); eAttr.addField( "A" , 0 ); eAttr.addField( "B" , 0 ); addAttribute( enumObj ); crvRmpObj = rAttr.createCurveRampAttr( "crvRamp" , "crvRamp" ); addAttribute( crvRmpObj ); clrRmpObj = rAttr.createColorRampAttr( "clrRamp" , "clrRamp" ); addAttribute( clrRmpObj ); fileNameObj = tAttr.create( "fileName" , "fileName" , MFnData::kString ); tAttr.setUsedAsFilename( true ); addAttribute( fileNameObj ); ... } |
end,
[zz]Maya C++ API Programming Tips的更多相关文章
- Flink DataStream API Programming Guide
Example Program The following program is a complete, working example of streaming window word count ...
- Flink-v1.12官方网站翻译-P016-Flink DataStream API Programming Guide
Flink DataStream API编程指南 Flink中的DataStream程序是对数据流实现转换的常规程序(如过滤.更新状态.定义窗口.聚合).数据流最初是由各种来源(如消息队列.套接字流. ...
- Flink DataSet API Programming Guide
https://ci.apache.org/projects/flink/flink-docs-release-0.10/apis/programming_guide.html Example ...
- Maya API Test
import maya.OpenMaya as OpenMaya import maya.OpenMayaMPx as OpenMayaMPx sl = OpenMaya.MSelectionList ...
- Maya API编程快速入门
一.Maya API编程简介 Autodesk® Maya® is an open product. This means that anyone outside of Autodesk can ch ...
- RESTful API URI 设计: 查询(Query)和标识(Identify)
相关文章:RESTful API URI 设计的一些总结. 问题场景:删除一个资源(Resources),URI 该如何设计? 应用示例:删除名称为 iPhone 6 的产品. 是不是感觉很简单呢?根 ...
- CG资源网 - Maya教程
Maya中mentalray灯光渲染终极训练视频教程 http://www.cgtsj.com/cg/f/vx3627/index.html Maya无人机建模制作训练视频教程第一季 http://w ...
- Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
1, http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c 2, Introduction ...
- Flink Program Guide (2) -- 综述 (DataStream API编程指导 -- For Java)
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
随机推荐
- php正则,删除字符串中的中英文标点符号
原理很简单,正则查找字符串,然后替换 英文标点符号,正则中有专用的模式来匹配.中文则需要一一列举 代码: <?php $str = "!@#$%^&*(中'文::﹑•中'文中' ...
- Java 正则表达式 量词 --- 三种匹配模式【贪婪型、勉强型、占有型】
1.Greediness(贪婪型):最大匹配X?.X*.X+.X{n,}都是最大匹配.例如你要用“<.+>”去匹配“a<tr>aava</tr>abb”,也许你所期 ...
- Win10/UWP开发-Ink墨迹书写
在UWP开发中,微软提供了一个新型的InkCanvas控件用来让用户能书写墨迹,在新版的Edga浏览器中微软自己也用到了该控件使用户很方便的可以在web上做笔记. InkCanvas控件使用很简单,从 ...
- Newtonsoft.Json 处理多态类型的反序列化
Newtonsoft.Json的序列化和反序列化很成熟也很好用, 最近在处理多态类型的反序列化中遇到了问题, 反序列化后只能到基类,而得不到也不能转换到子类.从网上查询了一番后,需要写一个创建类型的C ...
- 去掉tableView的header view的粘黏性
有的项目中,需要使用tableview的headerview,但是想让headerview粘在最顶不,希望和cell一起滚动,可以试试下面的代码来帮你实现这个需求: - (void)scrollVie ...
- POJ 2318
题目来源:http://poj.org/problem?id=2318 题目内容:给定一个矩形盒子(左上和右下端点的坐标),再给定n条线段,将盒子分为n+1份,之后给定m个点的坐标,对于盒子的每一段, ...
- 判断IP地址的类型
#include <stdio.h> #include <stdlib.h> void main() { ]; int ip_addr; printf("请输入IP地 ...
- ubuntu随笔
在命令行里输入 sudo nautilus 之后输入你的用户的密码,会弹出一个目录窗口来,可以复制到这来
- gnome-session 使用方法与介绍
注:译自gnome-session[1],有调整,如若有误,欢迎指正 用途 启动GNOME桌面环境 概要(synopsis) 使用方法 gnome-session [--autostart=DIR] ...
- C# 获取 oracle 存储过程的 返回值
存储过程 CREATE OR REPLACE PROCEDURE ADMIN.INSERT_OBJ ( OBJEFIRT_parms IN NVARCHAR2, OBJEDATT_parms IN N ...