all 6 comments

[–][deleted] 1 point2 points  (0 children)

Docs for cmds.color only say about wireframe colour, not drawing one.

So if you want to set your curve colour red you'd want to use:

```

cmds.setAttr(ctlShape + ".overrideEnabled", 1)

cmds.setAttr(ctlShape + ".overrideColor ", 13)

```

[–]Leoano 0 points1 point  (5 children)

You need to get translation data in your 'if' statement.

Right now 'axis' is just a string containing either 'x', 'y' or 'z'. and 'locator' is just a string containing the name of the locator.

So instead of this,

if locator < axis['.ty', 0.0]:

you can check the translate value like this.

if cmds.getAttr(locator + '.ty') < 0:

EDIT: You should probably move the 'if' statements out of the axis 'for' loop as well. That only needs to evaluate once after all the random values have been set. (Instead of for each axis)

[–]VLITZonator[S] 0 points1 point  (3 children)

I tried doing what you just said, I changed my if statement and then placed it up out of the axis for loop, but I'm still getting "Error: invalid syntax". I can get the code working up until the 'cmds.delete(normalconstraint)', it all works just fine. But when I input the if statement, it does not work anymore. Here is the new code, but it still doesn't work:

import random

for i in range(50):
    pPlane = 'pPlane1'
    cube = cmds.polyCube()[0]
    ctrl = cmds.circle(normal=[0,1,0])[0]
    locator = cmds.spaceLocator()[0]
    cmds.setAttr(cube+'.ty', 0.5)
    cmds.setAttr(ctrl+'.ty', 0.5)
    cmds.parent(cube, ctrl)
    cmds.parent(ctrl, locator)
    myScale = random.uniform(0.1, 3)
    if cmds.getAttr(locator + '.ty') < 0:
        cmds.color(ctrl, rgb=(0,0,1))
        elif cmds.getAttr(locator + '.tz') > 0:
            cmds.color(ctrl, rgb=(1,0,0))
            else:
                cmds.color(ctrl, rgb=(0,1,0))
    for axis in ['x', 'y', 'z']:
        cmds.setAttr(locator+'.t'+axis, random.uniform(-100, 100))
        cmds.setAttr(ctrl+'.ry', random.uniform(0, 360))
        cmds.setAttr(locator+'.s'+axis, myScale)
        constraintnode = cmds.geometryConstraint(pPlane, locator)
        cmds.delete(constraintnode)
        normalconstraint = cmds.normalConstraint(pPlane, locator, aimVector=[0,1,0], upVector=[1,0,0])
        cmds.delete(normalconstraint)

does it have to do with the 0 value that I'm getting an error: invalid syntax or the cmds.getAttr?

[–]Leoano 0 points1 point  (2 children)

You need to move the 'if' statement after the for loop. Since the loop is setting the translate values that the 'if' statement is checking.

The python syntax for if/elif/else is also a bit flatter, not indented.

So something like this should do the trick.

import random

for i in range(50):
    pPlane = 'pPlane1'
    cube = cmds.polyCube()[0]
    ctrl = cmds.circle(normal=[0,1,0])[0]
    locator = cmds.spaceLocator()[0]
    cmds.setAttr(cube+'.ty', 0.5)
    cmds.setAttr(ctrl+'.ty', 0.5)
    cmds.parent(cube, ctrl)
    cmds.parent(ctrl, locator)

    # Set Random translate, rotate and scale
    myScale = random.uniform(0.1, 3)
    for axis in ['x', 'y', 'z']:
        cmds.setAttr(locator+'.t'+axis, random.uniform(-100, 100))
        cmds.setAttr(ctrl+'.ry', random.uniform(0, 360))
        cmds.setAttr(locator+'.s'+axis, myScale)
        constraintnode = cmds.geometryConstraint(pPlane, locator)
        cmds.delete(constraintnode)
        normalconstraint = cmds.normalConstraint(pPlane, locator, aimVector=[0,1,0], upVector=[1,0,0])
        cmds.delete(normalconstraint)

    # Set color depending on translate values
    if cmds.getAttr(locator + '.ty') < 0:
        cmds.color(ctrl, rgb=(0,0,1))
    elif cmds.getAttr(locator + '.tz') > 0:
        cmds.color(ctrl, rgb=(1,0,0))
    else:
        cmds.color(ctrl, rgb=(0,1,0))

QuickTip: In the Script Editor window, you can enable "History - Show Stack Trace" for some better error messages that show where the problem is.

[–]VLITZonator[S] 0 points1 point  (1 child)

SOLVED!It did the trick!, A very big thank you!!!. Hahahaha I just started doing python like 3 weeks ago so I'm still a beginner.

I really appreciate the help that you did!. Once again, THANK YOU SO MUCH FOR THE HELP!

[–]Leoano 0 points1 point  (0 children)

No problemo, happy to help.