Esempio n. 1
0
MObject createShadingGroup(const MString& iName)
{
    MStatus status;

    MFnSet fnSet;
    MSelectionList selList;
    MObject shadingGroup = fnSet.create(selList,
                                        MFnSet::kRenderableOnly,
                                        &status);
    if (status != MS::kSuccess)
    {
        MString theError("Could not create shading engine: ");
        theError += iName;
        MGlobal::displayError(theError);

        return shadingGroup;
    }

    fnSet.setName(iName);

    return shadingGroup;
}
MStatus AlembicAssignFacesetCommand::doIt(const MArgList& args)
{
  ESS_PROFILE_SCOPE("AlembicAssignFacesetCommand::doIt");

  MStatus status;
  MArgParser argData(syntax(), args, &status);

  if (argData.isFlagSet("help"))
  {
    MGlobal::displayInfo("[ExocortexAlembic]: ExocortexAlembic_assignFaceset command:");
    MGlobal::displayInfo("                    -a : attribute of the shape with the faceset indices");
    MGlobal::displayInfo("                    -m : mesh to assign the faceset on");
    //MGlobal::displayInfo("                    -s : set to assign the faceset on");
    return MS::kSuccess;
  }

  /*
  if (!argData.isFlagSet("xset"))
  {
    MGlobal::displayError("[ExocortexAlembic]: ExocortexAlembic_assignFaceSets command missing set");
    return MS::kFailure;
  }
  //*/

  if (!argData.isFlagSet("attribute"))
  {
    MGlobal::displayError("[ExocortexAlembic]: ExocortexAlembic_assignFaceSets command missing attribute");
    return MS::kFailure;
  }

  if (!argData.isFlagSet("mesh"))
  {
    MGlobal::displayError("[ExocortexAlembic]: ExocortexAlembic_assignFaceSets command missing mesh");
    return MS::kFailure;
  }

  // load the mesh!
  MString argstr;
  MSelectionList sl;
  argstr = argData.flagArgumentString("mesh", 0);
  sl.add(argstr);
  MDagPath dagp;
  sl.getDagPath(0, dagp);
  MFnMesh fnMesh(dagp, &status);
  if (status != MS::kSuccess)
  {
    MGlobal::displayError("invalid mesh shape");
    return MS::kFailure;
  }

  // check if the attribute is valid
  argstr = argData.flagArgumentString("attribute", 0);
  MObject attr = fnMesh.attribute(argstr);
  MFnAttribute mfnAttr(attr);
  MPlug plug = fnMesh.findPlug(attr, true);

  if (!mfnAttr.isReadable() || plug.isNull())
  {
    MGlobal::displayError("invalid attribute");
    return MS::kFailure;
  }

  MFnIntArrayData arr(plug.asMObject(), &status);   // read the attribute
  if (status != MS::kSuccess || arr.length() == 0)
  {
    MGlobal::displayError("invalid attribute");
    return MS::kFailure;
  }

  {
    MObject oMesh = fnMesh.object();
    MSelectionList faces;
    int pol_idx = 0, facesetIdx = 0;
    for (MItMeshPolygon iter(oMesh); !iter.isDone(); iter.next(), ++pol_idx)
    {
      if (pol_idx == arr[facesetIdx])
      {
        faces.add(iter.currentItem());
        ++facesetIdx;

        if (facesetIdx == arr.length())
          break;
      }
    }

    MFnSet fnSet;
    MObject oSet = fnSet.create(sl, MFnSet::kFacetsOnly, &status);
    if (!status)
    {
      MGlobal::displayError("invalid set: " + status.errorString());
      return MS::kFailure;
    }
    setResult(fnSet.name());
  }
  return MS::kSuccess;
}