code of the day cotd

Code Of The Day: detachseparate-mel

A good place to start this first code of the day is something small but useful. The most downloaded script on creative crash is the Maya script detachseparate-mel by JeffD:

The functionality of the script is similar to the Mesh->Extract on a Polygon Face with the difference that the script creates a separate hierarchy for the new face. My guess at the popularity of this particular script stems from this functionality being available in 3DS Max and thus those artists working in Maya wanted the same feature.

Jeff D writes in the comments: “This script will effectively detach and separate selected faces from a poly mesh. Advantages of this script over Maya’s built in detach/separate is that this script doesn’t create a third parent node. It also allows you to break off only the selected faces as opposed to exploding every sub-mesh of an object.”

Language: MEL
Type: Maya Editor Script
Number of lines: 61

Functionality: 8.5
The code does what is expected when a polygon face (that is attached to other faces) is selected. The code does not handle the case where nothing is selected, when the object selected only has a single face (like a plane) or if the object is not a polygon (like a subdivision surface). Each of these cases generates the same error when the code gets to the duplication line, but it would be nice if the code checked for a few of these and gave a different warning depending on the error.

Design: 7
The organization of multiple iterators in the same loop (with different values), is a little more complex than it needs to be and could be refactored to be simpler. Additional error checking would be ideal to prevent unneccessary computation if the object can not be duplicated (or the selection is invalid). Array indexes are not checked before they are used, values returned from the Maya API are not checked before they are used.

Style: 9
The code layout is consistent and logical. Most variables are declared and initialized at the top of the function to make the code easier to read and follow. The formatting is consistent and the use of whitespace and comments is pretty clear. Some of the variable names such as using step, step2, step3, skip and skip2 for loop iterators are a little unusual.

Documentation: 9
The comments and documentation are quite straight forward, a couple extra comments on some of the magic numbers (like 34) would help clarify a few things without having to look at the filterExpand documentation, some additional comments on why the loops were setup with multiple iterators in the same expression would also be useful.

Reusability: 7
The code could also pass in the filterExpand type variable into the function instead of hardcoding the 34 (for Polygon Faces) which could allow the code could potentially be made more flexible for other types of splits (and not just polygon faces). As a single function, the code would have to be broken up if pieces if any of it were to be used separately.

Overall: 8.5
The code quality is pretty good and the functionality does what it intends. Additional error checking and future reusability design decisions would be a nice to have, but for the most part the script does exactly what is required. For those looking for the same detach 3DS Max workflow, or one that requires that extracted faces be separate from a parent node, this is a nice little script to use.

Until next time,
Michael Hubbard

Leave a Reply

Your email address will not be published. Required fields are marked *