create a grid of cubes

Posted by on Sep 29, 2010 in code, how to, Maya, MEL, python | No Comments

Grid of cubes in Maya.

# create a grid of cubes, their bottom plane set to zero y, and the center point at zero y.
x = -20
count = 0
cubes = []
while x <= 20:
	z = -20
	while z <= 20:
		cube = mc.polyCube( name = ('polyCube_' + str(count) + '_' + str(x) + str(z)) )
		count +=1
		cubeName=cube[0]
		cubes.append(cubeName)
		mc.setAttr((cubeName + '.translateY'), .5);
		mc.makeIdentity(apply=True, t =True, r=True, s=True, n= 0)
		mc.ResetTransformations()
		mc.move(x, 0, z)
		z += 1
	x +=1

Block of cubes

import maya.cmds as mc
# create a block grid of cubes
# change the mult number below to change the amount of cubes created.
mult =  3;
count = 0
cubes = []
y = mult - mult
while y <= mult *2:
    x = mult * -1
    while x <= mult:
	    z = mult * -1
	    while z <= mult:
		    cube = mc.polyCube( name = ('cube_' + str(count) ) )
		    count +=1
		    cubeName=cube[0]
		    cubes.append(cubeName)
		    mc.move(x, y, z)
		    z += 1
	    x +=1
    y +=1

Make a 3d cube grid, based on a selected object’s bounding box.

//MEL
proc makeCubeGrid(int $cubeGridDiv)
{
    string $cube[];
    string $cubes[] = {};
    string $sel[] = `ls -long -sl`;
    float $bBox[] = `polyEvaluate -b -ae $sel[0]`;

    float $w = ($bBox[1] - $bBox[0]) / $cubeGridDiv;
    float $h = ($bBox[3] - $bBox[2]) / $cubeGridDiv;
    float $d = ($bBox[5] - $bBox[4]) / $cubeGridDiv;

    float $p[3];
    for($i=0;$i<$cubeGridDiv;$i++)
    {
        $p[0] = $bBox[0] + (($bBox[1] - $bBox[0]) / $cubeGridDiv) * $i;
        for($k=0;$k<$cubeGridDiv;$k++)
        {
            $p[1] = $bBox[2] + (($bBox[3] - $bBox[2]) / $cubeGridDiv) * $k;
            for($n=0;$n<$cubeGridDiv;$n++)
            {
                $p[2] = $bBox[4] + (($bBox[5] - $bBox[4]) / $cubeGridDiv) * $n;
                $cube = `polyCube -w $w -h $h -d $d -sx 1 -sy 1 -sz 1 -ax 0 0 0 -ch 0`;
                move ($p[0] + $w / 2) ($p[1] + $h / 2) ($p[2] + $d / 2) $cube[0];
                makeIdentity -apply true -t 1 -r 1 -s 1 -n 0 $cube[0];
                $cubes[`size $cubes`] = $cube[0];
            };
        };
    };
    string $group = `group -em -n "cubes"`;
    parent $cubes $group;
};

makeCubeGrid(10);

Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponShare on RedditDigg this

Leave a Reply