MPlugs: Create Plug

With this post I’m going back to a topic I touched on briefly a couple months ago – MPlugs. In this installment I’ll be covering how to create MPlugs on an existing node.

First we need a node to work with. So lets make a polygon sphere.

Now we’ll add a new plug of the type String. It’s important to note here that an MPlug can refer to a few different types of attributes. MPlugs are the thing that moves data between two nodes while the attribute is the type of data being moved. So whenever you make an MPlug you need to assign it a data type. Here is how that’s done for a String.

First get the sphere as a MObject. Then create an instance of MDGModifier. This is used to change the structure of the dependency graph. In our case, adding a new attribute to the sphere.

Creating the attribute requires choosing the correct attribute class. There are nine different attribute classes to choose. Some of which I’ll cover in later posts, but for now all we need is MFnTypedAttribute. When creating the attribute we specify which data type it will be using MFnData (kString for a String). And finally, MDGModifier is used to add the newly created attribute to the MObject.


The end result is a new attribute of type String added to our poly sphere.