Thursday, March 14, 2013

Polygon Creation Tool

import maya.cmds as cmds
from optwin import AR_OptionsWindow;
class AR_PolyOptionsWindow(AR_OptionsWindow):
    def __init__(self):
        AR_OptionsWindow.__init__(self);
        self.title = 'Polygon Creation Options'; //reassign name
        self.actionName = 'Create';  //reassign action button name

    def displayOptions(self):
        self.objType = cmds.radioButtonGrp(
            label = 'Object Type:   ',
            labelArray4=[
                'Cube',
                'Cone',
                'Cylinder',
                'Sphere'
            ],
            numberOfRadioButtons=4,
            select=1  //set default selected item to 1(Cube)
        );
        self.xformGrp = cmds.frameLayout( //create a frame layout and stores it in the xformGrp attribute.
            label='Transformations',
            collapsable=True
        );
        cmds.formLayout(
            self.optionsForm, e=True,
            attachControl=(
                [self.xformGrp, 'top',2,self.objType]
            ),
            attachForm=(
            [self.xformGrp, 'left',0],
            [self.xformGrp, 'right',0]
            )
        );  
        self.xformCol = cmds.columnLayout();
        self.position = cmds.floatFieldGrp(
            label='Position: ',
            numberOfFields=3
        );
        self.rotation = cmds.floatFieldGrp(
            label='Rotation(XYZ): ',
            numberOfFields=3
        );
        self.scale = cmds.floatFieldGrp(
            label='Scale: ',
            numberOfFields=3,
            value=[1.0,1.0,1.0,1.0]  //set default value
        );
        cmds.setParent(self.optionsForm);  //call to the setParent command to make optionsForm the current parent again.

        self.color = cmds.colorSliderGrp( //add a color picker control, the colorSliderGrp command creates a color picker with a luminance slider next to it.
            label='Polygon Color: '
        );
        cmds.formLayout(
            self.optionsForm, e=True,
            attachControl=(
                [self.color, 'top',0,self.xformGrp]
            ),
            attachForm=(
                [self.color, 'left',0]
            )
        );
    def applyBtnCmd(self, *args):
        self.objIndAsCmd = { //create a dictionary to map the radio indices to different function pointers.
            1:cmds.polyCube,
            2:cmds.polyCone,
            3:cmds.polyCylinder,
            4:cmds.polySphere
        };
        objIndex = cmds.radioButtonGrp( //query the objType radio button group and use the dictionary to execute the command that corresponds to the currently selected index.
            self.objType, q=True,
            select=True
        );
        newObject = self.objIndAsCmd[objIndex]();

        pos = cmds.floatFieldGrp(  //apply translation to the newly created object's transform node
            self.position, q=True,
            value=True
        );
        rot = cmds.floatFieldGrp(  //apply rotation to the newly created object's transform node
            self.rotation, q=True,
            value=True
        );
        scale = cmds.floatFieldGrp(  //apply scale to the newly created object's transform node
            self.scale, q=True,
            value=True
        );
        cmds.xform(newObject[0], t=pos, ro=rot, s=scale);

        col = cmds.colorSliderGrp(  //apply the selected color to the new model's vertices.
            self.color, q=True,
            rgbValue=True
        );
        cmds.polyColorPerVertex(
            newObject[0],
            colorRGB=col,
            colorDisplayOption=True
        );
AR_PolyOptionsWindow.showUI();
Result:

No comments:

Post a Comment