Maya API - MFnSet::removeMember

Hiya!

It seems that .removeMember(const MDagPath &obj, const MObject &component) ONLY works if the object in the MDagPath has more than one shader assigned to it. If the object has one shader assigned it returns (kFailure): Unexpected Internal Failure.

I would like to remove the shader from the face, and then add a new shader to the face - however in the case where the mesh has one shader assigned at the object level - using .removeMember and .addMember doesn’t seem to work :tear:. It works fine if the object has multiple shaders already assigned. Is there a good way of doing this that will be happy in both cases?

Thanks!

Krittah! Nice to see you around.

Disclaimer: I don’t know if this is actually how a Set Node operates internally, but my observations have shown this to be mostly true.

MFnSet is a beast. Your particular problem is really annoying. By the sound of it, unless the object is being partitioned into two SGs, you are operating on the Shape level, not the component level. Therefore, when you try to remove from the Set, you are asking to remove something that isn’t technically “in” it. This is annoying, dumb, etc. This wouldn’t be so bad, except for Maya screws you in the other direction too. You can’t add a component, because Maya recognizes that it is part of the greater whole that is already part of a set. You’re not out in the cold though!

You can clear the set ( or remove the exact shape node you are operating on ) and then re-add only the components you care about to one set, and then all other components to another set.
Alternately, you can use an MDGModifier, and just call the MEL “sets” command with -forceElement. This has the advantage of giving you Undo/Redo (on this aspect of your operation) practically for free. I generally prefer this method, because the set juggling is prone to error, and I’m content to leave that up to Autodesk, who essentially guarantee that behavior in -forceElement.

Cheers!
-Lith

Hiya :slight_smile:

Thanks! I was hoping I was just wrong or missing some cute piece of information. It seemed incredibly odd, but the way you explained it definitely makes at least some sense as to why it is that way. I ended up going with the mel command call. I’m still pretty new to the API stuff, and calling mel commands seems kinda weird - so it definitely helps when others suggest it as a solution.