Skip to content Skip to sidebar Skip to footer

What Does It Mean For An Object To Be "in The Underworld?"

I have this code: def block_stacks(num): stack = cmds.group(empty=True, name='Stacks#') size = num for var in range(num): i = 0 r_rot = random.uniform(0

Solution 1:

I believe the error means that you can't parent a dg node (something that has no transform) to a dag node. For example, try parenting an objectSet to a transform. It won't let you, because dg nodes have no transforms themselves and cannot belong in a hierarchy.

Now it's giving you this error because you're trying to parent the cube's polyCube input, which has no transform! This is being done by accident because you're assuming that cmds.polyCube returns the cube's transform. It does not. In fact, it returns a list of 2 items: the cube's transform, and the cube's polyCube input. And since cmds.parent can accept a list in its first parameter, you are essentially trying to parent the transform AND polyCube to the stack transform. You can easily avoid this by grabbing the command's first index like this: cmds.polyCube()[0]

Now another issue is that all of the cubes move to the same place. This is because your i variable is INSIDE the for loop. So every iteration i resets to 0 instead of being incremented, thus they all move to the same position.

Another issue is that in a lot of your commands you are using "block*". Doing this doesn't refer to the block variable, instead it will actually grab all transforms that start with the name "block". In fact you don't need the "*" at all, just pass the variable block.

With all of this in mind, here's the working code:

import random
import maya.cmds as cmds


defblock_stacks(num):
    stack = cmds.group(empty=True, name='Stacks#')
    i = 0# Need to move this OUT of the loop otherwise it always resets to 0 and all of the blocks will move to the same place.for var inrange(num):
        r_rot = random.uniform(0,359)
        block = cmds.polyCube(h=0.5, w=0.5, d=0.5, name='block#')[0]  # This command actually returns a list of 2 items, the transform and the polyCube input, so grab the first index.
        cmds.parent(block, stack)
        cmds.move(0, 5.38 + i, 0, block)  # Pass the variable.
        cmds.rotate(0, r_rot, 0, block)
        rR = random.uniform(0, 1.0)
        rG = random.uniform(0, 1.0)
        rB = random.uniform(0, 1.0)
        cmds.polyColorPerVertex(block, rgb=[rR, rG, rB], cdo=True)
        i += 0.5


block_stacks(5)

Post a Comment for "What Does It Mean For An Object To Be "in The Underworld?""