MStatus fullLoft::initialize() { MStatus stat; MFnTypedAttribute typedAttr; inputCurve=typedAttr.create( "inputCurve", "in", MFnNurbsCurveData::kNurbsCurve, &stat ); PERRORfail("initialize create input attribute"); stat = typedAttr.setArray( true ); PERRORfail("initialize set input attribute array"); outputSurface=typedAttr.create( "outputSurface", "out", MFnNurbsSurfaceData::kNurbsSurface, &stat ); PERRORfail("initialize create output attribute"); stat = typedAttr.setStorable( false ); PERRORfail("initialize set output attribute storable"); stat = addAttribute( inputCurve ); PERRORfail("addAttribute(inputCurve)"); stat = addAttribute( outputSurface ); PERRORfail("addAttribute(outputSurface)"); stat = attributeAffects( inputCurve, outputSurface ); PERRORfail("attributeAffects(inputCurve, outputSurface)"); return MS::kSuccess; }
MStatus fullLoft::compute( const MPlug& plug, MDataBlock& data ) { MStatus stat; if ( plug == outputSurface ) // loft inputCurves into surface { MArrayDataHandle inputArrayData = data.inputArrayValue( inputCurve, &stat ); PERRORfail("fullLoft::compute getting input array data"); MDataHandle surfHandle = data.outputValue( fullLoft::outputSurface ); PERRORfail("fullLoft::compute getting output data handle"); MFnNurbsSurfaceData dataCreator; MObject newSurfData = dataCreator.create( &stat ); PERRORfail("fullLoft::compute creating new nurbs surface data block"); /* MObject newSurf = */ loft(inputArrayData, newSurfData, stat ); // No error message is needed - fullLoft::loft will output one if (!stat) return stat; // newSurf is the new surface object, but it has been packed // into the datablock we created for it, and the data block // is what we must put onto the plug. stat = surfHandle.set( newSurfData ); PERRORfail("fullLoft::compute setting surface handle"); stat = data.setClean( plug ); PERRORfail("fullLoft::compute cleaning outputSurface plug"); } else { return MS::kUnknownParameter; } return MS::kSuccess; }
/* This function gets called by Maya to evaluate the texture. */ MStatus shiftNode::compute( const MPlug& plug, MDataBlock& data ) { MStatus stat; if ((plug != aOutColor) && (plug.parent() != aOutColor)) return MS::kUnknownParameter; MDataHandle colorH; MFloatVector color; MDataHandle shiftH = data.inputValue( aShift, &stat); PERRORfail(stat, "compute getting shift attr"); bool shiftIt = shiftH.asBool(); MDataHandle distH = data.inputValue( aDist, &stat); PERRORfail(stat, "compute getting distance attr"); float distance = distH.asFloat(); MFloatVector clr; if ( shiftIt && distance != 0.0 ) { // first evaluate color at default sample posiiton clr = data.inputValue( aColor ).asFloatVector(); // uv is used by 2d textures // refPointCamera is used by 3d textures MDataHandle refPointCamH = data.inputValue( aRefPointCamera, &stat); PERRORfail(stat, "compute getting refPointCamera attr"); MFloatVector refPC = refPointCamH.asFloatVector(); // get current UV const float2 & oldUV = data.inputValue(aUv).asFloat2(); // shift and set the uv/refPointCamera values so // we can sample around the current uv/refPointCamera MDataHandle outUV = data.outputValue( aUv ); MDataHandle outPC = data.outputValue( aRefPointCamera ); outUV.set( oldUV[0]-distance, oldUV[1] ); outPC.set( refPC.x + distance, refPC.y + distance, refPC.z + distance); colorH = data.inputValue( aColor, &stat); // evaluate at new pos color = colorH.asFloatVector(); clr += color; outUV.set( oldUV[0]+distance, oldUV[1] ); outPC.set( refPC.x - distance, refPC.y + distance, refPC.z + distance); colorH = data.inputValue( aColor, &stat); // evaluate at new pos color = colorH.asFloatVector(); clr += color; outUV.set( oldUV[0], oldUV[1]-distance ); outPC.set( refPC.x + distance, refPC.y - distance, refPC.z + distance); colorH = data.inputValue( aColor, &stat); // evaluate at new pos color = colorH.asFloatVector(); clr += color; outUV.set( oldUV[0], oldUV[1]+distance ); outPC.set( refPC.x - distance, refPC.y - distance, refPC.z + distance); colorH = data.inputValue( aColor, &stat); // evaluate at new pos color = colorH.asFloatVector(); clr += color; clr /= 5.0; // average the colors from all locations // set sample data back to original values outUV.set( oldUV[0], oldUV[1] ); outPC.set( refPC.x, refPC.y, refPC.z ); } else { colorH = data.inputValue( aColor, &stat); clr = colorH.asFloatVector(); } MDataHandle outColorHandle = data.outputValue( aOutColor ); MFloatVector& oclr = outColorHandle.asFloatVector(); oclr = clr; outColorHandle.setClean(); return MS::kSuccess; }
// initializes attribute information MStatus shiftNode::initialize() { MStatus stat; MFnNumericAttribute nAttr; // shift aShift = nAttr.create( "shift", "sh", MFnNumericData::kBoolean, false ); stat = addAttribute( aShift ); PERRORfail( stat, "addAttribute shift" ); // distance aDist = nAttr.create( "distance", "dis", MFnNumericData::kFloat, 0.0 ); stat = addAttribute( aDist ); PERRORfail( stat, "addAttribute dist" ); // inColor aColor = nAttr.createColor( "color", "c" ); nAttr.setStorable(false); stat = addAttribute( aColor ); PERRORfail( stat, "addAttribute inColor" ); // refPointCamera aRefPointCamera = nAttr.createPoint( "refPointCamera", "rpc" ); nAttr.setStorable(false); nAttr.setWritable(true); nAttr.setRenderSource(true); nAttr.setHidden(true); stat = addAttribute( aRefPointCamera ); PERRORfail( stat, "addAttribute refPointCamera" ); // uv MObject u = nAttr.create( "uCoord", "u", MFnNumericData::kFloat, 0.0 ); MObject v = nAttr.create( "vCoord", "v", MFnNumericData::kFloat, 0.0 ); aUv = nAttr.create( "uvCoord", "uv", u, v ); nAttr.setStorable(false); nAttr.setWritable(true); nAttr.setRenderSource(true); nAttr.setHidden(true); stat = addAttribute( aUv ); PERRORfail( stat, "addAttribute uv" ); // create output attributes here aOutColor = nAttr.createColor( "outColor", "oc" ); PERRORfail(stat, "initialize create outColor attribute"); nAttr.setReadable( true ); nAttr.setWritable( false ); stat = addAttribute( aOutColor ); PERRORfail(stat, "addAttribute(outColor)"); // attribute affects attributeAffects ( aShift, aOutColor ); attributeAffects ( aDist, aOutColor ); attributeAffects ( aColor, aOutColor ); attributeAffects ( aRefPointCamera, aOutColor ); attributeAffects ( aUv, aOutColor ); // we need to do the followings to cause other input attributes // to evaluate with the new values we set attributeAffects ( aUv, aUv ); attributeAffects ( aRefPointCamera, aRefPointCamera ); return MS::kSuccess; }
MStatus multiCurve::initialize() { MStatus stat; MFnNumericAttribute nAttr; MFnTypedAttribute typedAttr; numCurves = nAttr.create ("numCurves", "nc", MFnNumericData::kLong, 5, &stat); PERRORfail(stat, "initialize create numCurves attribute"); CHECK_MSTATUS ( nAttr.setKeyable( true ) ); stat = addAttribute( numCurves ); PERRORfail(stat, "addAttribute(numCurves)"); curveOffset = nAttr.create ("curveOffset", "co", MFnNumericData::kDouble, 1.0, &stat); PERRORfail(stat, "initialize create curveOffset attribute"); CHECK_MSTATUS ( nAttr.setKeyable( true ) ); stat = addAttribute( curveOffset ); PERRORfail(stat, "addAttribute(curveOffset)"); inputCurve = typedAttr.create( "inputCurve", "ic", MFnNurbsCurveData::kNurbsCurve, &stat ); PERRORfail(stat, "initialize create inputCurve attribute"); CHECK_MSTATUS ( typedAttr.setReadable( false ) ); CHECK_MSTATUS ( typedAttr.setWritable( true ) ); stat = addAttribute( inputCurve ); PERRORfail(stat, "addAttribute(inputCurve)"); outputCurves = typedAttr.create( "outputCurves", "oc", MFnNurbsCurveData::kNurbsCurve, &stat ); PERRORfail(stat, "initialize create outputCurves attribute"); CHECK_MSTATUS ( typedAttr.setArray( true ) ); CHECK_MSTATUS ( typedAttr.setReadable( true ) ); CHECK_MSTATUS ( typedAttr.setWritable( false ) ); CHECK_MSTATUS ( typedAttr.setUsesArrayDataBuilder( true ) ); stat = addAttribute( outputCurves ); PERRORfail(stat, "addAttribute(outputCurves)"); stat = attributeAffects( numCurves, outputCurves ); PERRORfail(stat, "attributeAffects(inputCurve, outputCurves)"); stat = attributeAffects( curveOffset, outputCurves ); PERRORfail(stat, "attributeAffects(inputCurve, outputCurves)"); stat = attributeAffects( inputCurve, outputCurves ); PERRORfail(stat, "attributeAffects(inputCurve, outputCurves)"); return stat; }
MStatus multiCurve::compute( const MPlug& plug, MDataBlock& data ) { MStatus stat; if ( plug == outputCurves ) { MDataHandle numCurvesHandle = data.inputValue(numCurves, &stat); PERRORfail(stat, "multiCurve::compute getting numCurves"); int num = numCurvesHandle.asLong(); MDataHandle curveOffsetHandle = data.inputValue(curveOffset, &stat); PERRORfail(stat, "multiCurve::compute getting curveOffset"); double baseOffset = curveOffsetHandle.asDouble(); MDataHandle inputCurveHandle = data.inputValue(inputCurve, &stat); PERRORfail(stat, "multiCurve::compute getting inputCurve"); MObject inputCurveObject ( inputCurveHandle.asNurbsCurveTransformed() ); MFnNurbsCurve inCurveFS ( inputCurveObject ); MArrayDataHandle outputArray = data.outputArrayValue(outputCurves, &stat); PERRORfail(stat, "multiCurve::compute getting output data handle"); // Create an array data build that is preallocated to hold just // the number of curves we plan on creating. When this builder // is set in to the MArrayDataHandle at the end of the compute // method, the new array will replace the existing array in the // scene. // // If the number of elements of the multi does not change between // compute cycles, then one can reuse the space allocated on a // previous cycle by extracting the existing builder from the // MArrayDataHandle: // MArrayDataBuilder builder( outputArray.builder(&stat) ); // this later form of the builder will allow you to rewrite elements // of the array, and to grow it, but the array can only be shrunk by // explicitly removing elements with the method // MArrayDataBuilder::removeElement(unsigned index); // MArrayDataBuilder builder(outputCurves, num, &stat); PERRORfail(stat, "multiCurve::compute creating builder"); for (int curveNum = 0; curveNum < num; curveNum++) { MDataHandle outHandle = builder.addElement(curveNum); MFnNurbsCurveData dataCreator; MObject outCurveData = dataCreator.create(); MObject outputCurve = inCurveFS.copy(inputCurveObject, outCurveData, &stat); PERRORfail(stat, "multiCurve::compute copying curve"); MFnNurbsCurve outCurveFS ( outputCurve ); MPointArray cvs; double offset = baseOffset * (curveNum+1); outCurveFS.getCVs ( cvs, MSpace::kWorld ); int numCVs = cvs.length(); for (int i = 0; i < numCVs; i++) { cvs[i].x += offset; } outCurveFS.setCVs ( cvs ); outHandle.set(outCurveData); } // Set the builder back into the output array. This statement // is always required, no matter what constructor was used to // create the builder. stat = outputArray.set(builder); PERRORfail(stat, "multiCurve::compute setting the builder"); // Since we compute all the elements of the array, instead of // just marking the plug we were asked to compute as clean, mark // every element of the array as clean to prevent further calls // to this compute method during this DG evaluation cycle. stat = outputArray.setAllClean(); PERRORfail(stat, "multiCurve::compute cleaning outputCurves"); } else { return MS::kUnknownParameter; } return stat; }