//--------------------------------------------------- bool DagHelper::connectToList ( const MPlug& source, const MObject& destination, const MString& destinationAttribute, int* _index ) { MStatus status; MFnDependencyNode destFn ( destination ); MPlug dest = destFn.findPlug ( destinationAttribute, &status ); if ( status != MStatus::kSuccess ) return false; if ( !dest.isArray() ) return false; int index = ( _index != NULL ) ? *_index : -1; if ( index < 0 ) { index = getNextAvailableIndex ( dest, ( int ) dest.evaluateNumElements() ); if ( _index != NULL ) *_index = index; } MPlug d = dest.elementByLogicalIndex ( index ); MDGModifier modifier; modifier.connect ( source, d ); status = modifier.doIt(); return status == MStatus::kSuccess; }
// Break connections to this blendshape void BlendShape::breakConnections() { MStatus stat; MDagModifier dagModifier; // Clear the stored connections m_weightConnections.clear(); // Save node connections and break them MPlug weightsPlug = m_pBlendShapeFn->findPlug("weight",true); for (int i=0; i<weightsPlug.evaluateNumElements(); i++) { MPlug wPlug = weightsPlug.elementByPhysicalIndex(i); MPlugArray srcConnections; MPlugArray dstConnections; wPlug.connectedTo(srcConnections,false,true); wPlug.connectedTo(dstConnections,true,false); weightConnections wcon; for (int j=0; j<srcConnections.length(); j++) { wcon.srcConnections.append(srcConnections[j]); dagModifier.disconnect(wPlug,srcConnections[j]); dagModifier.doIt(); } for (int j=0; j<dstConnections.length(); j++) { wcon.dstConnections.append(dstConnections[j]); stat = dagModifier.disconnect(dstConnections[j],wPlug); if (MS::kSuccess != stat) { std::cout << "Error trying to disconnect plug " << wPlug.name().asChar() << " and plug " << dstConnections[j].name().asChar() << "\n"; std::cout << stat.errorString().asChar() << "\n"; std::cout.flush(); } stat = dagModifier.doIt(); if (MS::kSuccess != stat) { std::cout << "Error trying to disconnect plug " << wPlug.name().asChar() << " and plug " << dstConnections[j].name().asChar() << "\n"; std::cout << stat.errorString().asChar() << "\n"; std::cout.flush(); } } m_weightConnections.push_back(wcon); } }
// Restore connections on this blendshape void BlendShape::restoreConnections() { MDagModifier dagModifier; // Recreate stored connections on the weight attributes MPlug weightsPlug = m_pBlendShapeFn->findPlug("weight",true); for (int i=0; i<weightsPlug.evaluateNumElements(); i++) { MPlug wPlug = weightsPlug.elementByPhysicalIndex(i); weightConnections& wcon = m_weightConnections[i]; for (int j=0; j<wcon.srcConnections.length(); j++) { dagModifier.connect(wPlug,wcon.srcConnections[j]); dagModifier.doIt(); } for (int j=0; j<wcon.dstConnections.length(); j++) { dagModifier.connect(wcon.dstConnections[j],wPlug); dagModifier.doIt(); } } }
//----------------------------------------------------------------------------- // Returns the next available logical index for the given array plug //----------------------------------------------------------------------------- uint ValveMaya::NextAvailable( MPlug &mPlug ) { MIntArray lIndices; mPlug.evaluateNumElements(); mPlug.getExistingArrayAttributeIndices( lIndices ); const uint nIndices( lIndices.length() ); if ( nIndices == 0U ) return 0U; uint nBubble( nIndices - 1U ); int tmpVal; // Bubble sort because I think that lIndices should be in order // but it's not documented as such, so just to be safe for ( bool swapped( false ); swapped; swapped = false, --nBubble ) { for ( uint i( 0U ); i != nBubble; ++i ) { if ( lIndices[ i ] > lIndices[ i + 1 ] ) { tmpVal = lIndices[ i ]; lIndices[ i ] = lIndices[ i + 1 ]; lIndices[ i + 1 ] = tmpVal; swapped = true; } } } for ( uint i( 0U ); i != nIndices; ++i ) { if ( lIndices[ i ] != static_cast< int >( i ) ) return i; } return nIndices; }
//------------------------------------------------------ void MaterialExporter::exportMaterialsByShaderPlug() { // Get all shaders, which are in the default shader list. MObject defaultShaderList = DagHelper::getNode ( ATTR_DEFAULT_SHADER_LIST1 ); MPlug defaultShadersPlug = MFnDependencyNode ( defaultShaderList ).findPlug ( ATTR_SHADERS ); uint shaderCount = defaultShadersPlug.evaluateNumElements(); for ( uint i = 0; i < shaderCount; ++i ) { MObject shader = DagHelper::getNodeConnectedTo ( defaultShadersPlug.elementByPhysicalIndex ( i ) ); MFnDependencyNode shadingEngineFn ( shader ); // Get the name of the current material (this is the maya material id) String mayaMaterialId = DocumentExporter::mayaNameToColladaName ( shadingEngineFn.name(), true ); bool doExportMaterial = true; bool isFromReferencedFile = shadingEngineFn.isFromReferencedFile(); // bool isDefaulNode = shadingEngineFn.isDefaultNode(); // if ( isDefaulNode ) // { // doExportMaterial = false; // } // else if ( isFromReferencedFile ) if ( isFromReferencedFile ) { if ( ExportOptions::exportXRefs() && ExportOptions::dereferenceXRefs() ) doExportMaterial = true; else doExportMaterial = false; } if ( doExportMaterial ) { MObject shadingEngine = ShaderHelper::getShadingEngine ( shader ); exportMaterial ( shadingEngine ); } } }
// Load texture data from a texture node MStatus material::loadTexture(MFnDependencyNode* pTexNode,BlendModes& blendModes,MStringArray& uvsets) { texture tex; // Get texture filename MString filename, absFilename; MRenderUtil::exactFileTextureName(pTexNode->object(),absFilename); filename = absFilename.substring(absFilename.rindex('/')+1,absFilename.length()-1); MString command = "toNativePath(\""; command += absFilename; command += "\")"; MGlobal::executeCommand(command,absFilename); tex.absFilename = absFilename; tex.filename = filename; tex.uvsetIndex = 0; tex.uvsetName = ""; const char* tempChar = absFilename.asChar(); // Set texture operation type tex.blendModes = blendModes; // Get connections to uvCoord attribute of texture node MPlugArray texSrcPlugs; pTexNode->findPlug("uvCoord").connectedTo(texSrcPlugs,true,false); // Get place2dtexture node (if connected) MFnDependencyNode* pPlace2dTexNode = NULL; for (int j=0; j<texSrcPlugs.length(); j++) { if (texSrcPlugs[j].node().hasFn(MFn::kPlace2dTexture)) { pPlace2dTexNode = new MFnDependencyNode(texSrcPlugs[j].node()); continue; } } // Get uvChooser node (if connected) MFnDependencyNode* pUvChooserNode = NULL; if (pPlace2dTexNode) { MPlugArray placetexSrcPlugs; pPlace2dTexNode->findPlug("uvCoord").connectedTo(placetexSrcPlugs,true,false); for (int j=0; j<placetexSrcPlugs.length(); j++) { if (placetexSrcPlugs[j].node().hasFn(MFn::kUvChooser)) { pUvChooserNode = new MFnDependencyNode(placetexSrcPlugs[j].node()); continue; } } } // Get uvset index if (pUvChooserNode) { bool foundMesh = false; bool foundUvset = false; MPlug uvsetsPlug = pUvChooserNode->findPlug("uvSets"); MPlugArray uvsetsSrcPlugs; for (int i=0; i<uvsetsPlug.evaluateNumElements() && !foundMesh; i++) { uvsetsPlug[i].connectedTo(uvsetsSrcPlugs,true,false); for (int j=0; j<uvsetsSrcPlugs.length() && !foundMesh; j++) { if (uvsetsSrcPlugs[j].node().hasFn(MFn::kMesh)) { uvsetsSrcPlugs[j].getValue(tex.uvsetName); for (int k=0; k<uvsets.length() && !foundUvset; k++) { if (uvsets[k] == tex.uvsetName) { tex.uvsetIndex = k; foundUvset = true; } } } } } } // add texture to material texture list m_textures.push_back(tex); // free up memory if (pUvChooserNode) delete pUvChooserNode; if (pPlace2dTexNode) delete pPlace2dTexNode; return MS::kSuccess; }
void liqRibData::addAdditionalSurfaceParameters( MObject node ) { LIQDEBUGPRINTF("-> scanning for additional rman surface attributes \n"); MStatus status = MS::kSuccess; unsigned i; // work out how many elements there would be in a facevarying array if a mesh or subD // faceVaryingCount is a private data member if ( ( type() == MRT_Mesh ) || ( type() == MRT_Subdivision ) ) { faceVaryingCount = 0; MFnMesh fnMesh( node ); for ( uint pOn = 0; pOn < fnMesh.numPolygons(); pOn++ ) { faceVaryingCount += fnMesh.polygonVertexCount( pOn ); } } // find how many additional MFnDependencyNode nodeFn( node ); // find the attributes MStringArray floatAttributesFound = findAttributesByPrefix( "rmanF", nodeFn ); MStringArray pointAttributesFound = findAttributesByPrefix( "rmanP", nodeFn ); MStringArray vectorAttributesFound = findAttributesByPrefix( "rmanV", nodeFn ); MStringArray normalAttributesFound = findAttributesByPrefix( "rmanN", nodeFn ); MStringArray colorAttributesFound = findAttributesByPrefix( "rmanC", nodeFn ); MStringArray stringAttributesFound = findAttributesByPrefix( "rmanS", nodeFn ); if ( floatAttributesFound.length() > 0 ) { for ( i = 0; i < floatAttributesFound.length(); i++ ) { liqTokenPointer tokenPointerPair; MString cutString = floatAttributesFound[i].substring(5, floatAttributesFound[i].length()); MPlug fPlug = nodeFn.findPlug( floatAttributesFound[i] ); MObject plugObj; status = fPlug.getValue( plugObj ); if ( plugObj.apiType() == MFn::kDoubleArrayData ) { MFnDoubleArrayData fnDoubleArrayData( plugObj ); MDoubleArray doubleArrayData = fnDoubleArrayData.array( &status ); tokenPointerPair.set( cutString.asChar(), rFloat, ( type() == MRT_Nurbs || type() == MRT_NuCurve ) ? true : false, true, false, doubleArrayData.length() ); for( unsigned int kk = 0; kk < doubleArrayData.length(); kk++ ) { tokenPointerPair.setTokenFloat( kk, doubleArrayData[kk] ); } if ( ( type() == MRT_NuCurve ) && ( cutString == MString( "width" ) ) ) { tokenPointerPair.setDetailType( rVarying); } else if ( ( ( type() == MRT_Mesh ) || ( type() == MRT_Subdivision ) ) && ( doubleArrayData.length() == faceVaryingCount ) ) { tokenPointerPair.setDetailType( rFaceVarying); } else { tokenPointerPair.setDetailType( rVertex ); } } else { if( fPlug.isArray() ) { int nbElts = fPlug.evaluateNumElements(); float floatValue; tokenPointerPair.set( cutString.asChar(), rFloat, ( type() == MRT_Nurbs || type() == MRT_NuCurve ) ? true : false, false, true, // philippe :passed as uArray, otherwise it will think it is a single float nbElts ); MPlug elementPlug; for( unsigned int kk = 0; kk < nbElts; kk++ ) { elementPlug = fPlug.elementByPhysicalIndex(kk); elementPlug.getValue( floatValue ); tokenPointerPair.setTokenFloat( kk, floatValue ); } tokenPointerPair.setDetailType( rConstant ); } else { float floatValue; tokenPointerPair.set( cutString.asChar(), rFloat, ( type() == MRT_Nurbs || type() == MRT_NuCurve ) ? true : false, false, false, 0 ); fPlug.getValue( floatValue ); tokenPointerPair.setTokenFloat( 0, floatValue ); tokenPointerPair.setDetailType( rConstant ); } } tokenPointerArray.push_back( tokenPointerPair ); } } if ( pointAttributesFound.length() > 0 ) { for ( i = 0; i < pointAttributesFound.length(); i++ ) { liqTokenPointer tokenPointerPair; MString cutString = pointAttributesFound[i].substring(5, pointAttributesFound[i].length()); MPlug pPlug = nodeFn.findPlug( pointAttributesFound[i] ); MObject plugObj; status = pPlug.getValue( plugObj ); if ( plugObj.apiType() == MFn::kPointArrayData ) { MFnPointArrayData fnPointArrayData( plugObj ); MPointArray pointArrayData = fnPointArrayData.array( &status ); tokenPointerPair.set( cutString.asChar(), rPoint, ( type() == MRT_Nurbs || type() == MRT_NuCurve ) ? true : false, true, false, pointArrayData.length() ); if ( type() == MRT_Nurbs || type() == MRT_NuCurve ) { for ( int kk = 0; kk < pointArrayData.length(); kk++ ) { tokenPointerPair.setTokenFloat( kk, pointArrayData[kk].x, pointArrayData[kk].y, pointArrayData[kk].z, pointArrayData[kk].w ); } } else { for ( int kk = 0; kk < pointArrayData.length(); kk++ ) { tokenPointerPair.setTokenFloat( kk, pointArrayData[kk].x, pointArrayData[kk].y, pointArrayData[kk].z ); } } tokenPointerPair.setDetailType( rVertex ); } else { // Hmmmm float ? double ? float x, y, z; tokenPointerPair.set( cutString.asChar(), rPoint, ( type() == MRT_Nurbs || type() == MRT_NuCurve ) ? true : false, false, false, 0 ); // Hmmm should check as for arrays if we are in nurbs mode : 4 values pPlug.child(0).getValue( x ); pPlug.child(1).getValue( y ); pPlug.child(2).getValue( z ); tokenPointerPair.setTokenFloat( 0, x, y, z ); tokenPointerPair.setDetailType( rConstant ); } tokenPointerArray.push_back( tokenPointerPair ); } } parseVectorAttributes( nodeFn, vectorAttributesFound, rVector ); parseVectorAttributes( nodeFn, normalAttributesFound, rNormal ); parseVectorAttributes( nodeFn, colorAttributesFound, rColor ); if ( stringAttributesFound.length() > 0 ) { for ( i = 0; i < stringAttributesFound.length(); i++ ) { liqTokenPointer tokenPointerPair; MString cutString = stringAttributesFound[i].substring(5, stringAttributesFound[i].length()); MPlug sPlug = nodeFn.findPlug( stringAttributesFound[i] ); MObject plugObj; status = sPlug.getValue( plugObj ); tokenPointerPair.set( cutString.asChar(), rString, ( type() == MRT_Nurbs || type() == MRT_NuCurve ) ? true : false, false, false, 0 ); MString stringVal; sPlug.getValue( stringVal ); tokenPointerPair.setTokenString( 0, stringVal.asChar(), stringVal.length() ); tokenPointerPair.setDetailType( rConstant ); tokenPointerArray.push_back( tokenPointerPair ); } } }
// Load texture data from a texture node MStatus Material::loadTexture(MFnDependencyNode* pTexNode,TexOpType& opType,MStringArray& uvsets,ParamList& params) { Texture tex; // Get texture filename MString filename, absFilename; MRenderUtil::exactFileTextureName(pTexNode->object(),absFilename); filename = absFilename.substring(absFilename.rindex('/')+1,absFilename.length()-1); MString command = "toNativePath(\""; command += absFilename; command += "\")"; MGlobal::executeCommand(command,absFilename); tex.absFilename = absFilename; tex.filename = filename; tex.uvsetIndex = 0; tex.uvsetName = ""; // Set texture operation type tex.opType = opType; // Get connections to uvCoord attribute of texture node MPlugArray texSrcPlugs; pTexNode->findPlug("uvCoord").connectedTo(texSrcPlugs,true,false); // Get place2dtexture node (if connected) MFnDependencyNode* pPlace2dTexNode = NULL; for (int j=0; j<texSrcPlugs.length(); j++) { if (texSrcPlugs[j].node().hasFn(MFn::kPlace2dTexture)) { pPlace2dTexNode = new MFnDependencyNode(texSrcPlugs[j].node()); continue; } } // Get uvChooser node (if connected) MFnDependencyNode* pUvChooserNode = NULL; if (pPlace2dTexNode) { MPlugArray placetexSrcPlugs; pPlace2dTexNode->findPlug("uvCoord").connectedTo(placetexSrcPlugs,true,false); for (int j=0; j<placetexSrcPlugs.length(); j++) { if (placetexSrcPlugs[j].node().hasFn(MFn::kUvChooser)) { pUvChooserNode = new MFnDependencyNode(placetexSrcPlugs[j].node()); continue; } } } // Get uvset index if (pUvChooserNode) { bool foundMesh = false; bool foundUvset = false; MPlug uvsetsPlug = pUvChooserNode->findPlug("uvSets"); MPlugArray uvsetsSrcPlugs; for (int i=0; i<uvsetsPlug.evaluateNumElements() && !foundMesh; i++) { uvsetsPlug[i].connectedTo(uvsetsSrcPlugs,true,false); for (int j=0; j<uvsetsSrcPlugs.length() && !foundMesh; j++) { if (uvsetsSrcPlugs[j].node().hasFn(MFn::kMesh)) { uvsetsSrcPlugs[j].getValue(tex.uvsetName); for (int k=0; k<uvsets.length() && !foundUvset; k++) { if (uvsets[k] == tex.uvsetName) { tex.uvsetIndex = k; foundUvset = true; } } } } } } // Get texture options from Place2dTexture node if (pPlace2dTexNode) { // Get address mode //U bool wrapU, mirrorU; pPlace2dTexNode->findPlug("wrapU").getValue(wrapU); pPlace2dTexNode->findPlug("mirrorU").getValue(mirrorU); if (mirrorU) tex.am_u = TAM_MIRROR; else if (wrapU) tex.am_u = TAM_WRAP; else tex.am_u = TAM_CLAMP; // V bool wrapV,mirrorV; pPlace2dTexNode->findPlug("wrapV").getValue(wrapV); pPlace2dTexNode->findPlug("mirrorV").getValue(mirrorV); if (mirrorV) tex.am_v = TAM_MIRROR; else if (wrapV) tex.am_v = TAM_WRAP; else tex.am_v = TAM_CLAMP; // Get texture scale double covU,covV; pPlace2dTexNode->findPlug("coverageU").getValue(covU); pPlace2dTexNode->findPlug("coverageV").getValue(covV); tex.scale_u = covU; if (fabs(tex.scale_u) < PRECISION) tex.scale_u = 0; tex.scale_v = covV; if (fabs(tex.scale_v) < PRECISION) tex.scale_v = 0; // Get texture scroll double transU,transV; pPlace2dTexNode->findPlug("translateFrameU").getValue(transU); pPlace2dTexNode->findPlug("translateFrameV").getValue(transV); tex.scroll_u = -0.5 * (covU-1.0)/covU - transU/covU; if (fabs(tex.scroll_u) < PRECISION) tex.scroll_u = 0; tex.scroll_v = 0.5 * (covV-1.0)/covV + transV/covV; if (fabs(tex.scroll_v) < PRECISION) tex.scroll_v = 0; // Get texture rotation double rot; pPlace2dTexNode->findPlug("rotateFrame").getValue(rot); tex.rot = -rot; if (fabs(rot) < PRECISION) tex.rot = 0; } // add texture to material texture list m_textures.push_back(tex); // free up memory if (pUvChooserNode) delete pUvChooserNode; if (pPlace2dTexNode) delete pPlace2dTexNode; return MS::kSuccess; }