#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Help to complete this code in python maya


    hi everyone, I have this problem with this code of python in maya, what it is supposed to do is when I hit two colors it should create a gradient between the two colors on the sphere, so far I have created the sphere but Im getting this error when I hit the three colors and dont know how to fix it:

    myWindow()
    True
    True
    False
    False
    ['red']
    False
    True
    False
    ['red', 'green']
    False
    False
    True
    # Warning: you have already selected 2 colors #
    ['red', 'green']
    [0, 1, 0] None
    the step is 0.0025
    # Error: global name 'colorTempArray' is not defined
    # Traceback (most recent call last):
    # File "<maya console>", line 1, in <module>
    # File "<maya console>", line 103, in myColor
    # File "<maya console>", line 123, in calColor
    # File "<maya console>", line 148, in colorRange
    # NameError: global name 'colorTempArray' is not defined #

    ---------------------------------------------------------

    this is the code so far:

    #import

    import maya.cmds as cmds
    import maya.mel as mel
    import random

    #globals

    red = False
    green = False
    blue = False
    yellow = False
    myWindowVar = False
    thisWindow = None
    numFaces = 600
    myColors = list()
    myColorCountList = list()
    randomVal = False
    radiusField = None
    subY = None
    subx = None
    randomButton = None

    def myWindow():
    global myWindowVar
    global thisWindow
    global radiusField
    global subX
    global subY
    global randomButton

    if myWindowVar == True :
    print " I'm deleting this window"
    cmds.deleteUI (thisWindow, window = True)
    cmds.showWindow(thisWindow)

    if myWindowVar == False :
    myWindowVar = True
    myColors = []
    thisWindow = cmds.window(title = "color picker", width = 150)
    cmds.columnLayout (adjustableColumn = True)
    cmds.button (label = "create sphere" , command = "runBuild()")
    cmds.text( label = "radius" )
    radiusField = cmds.floatField ( value = 5 )
    cmds.text(label = "subdivision X" )
    subX = cmds.intField( value = 20, minValue = 3 )
    cmds.text(label = "subdivision Y" )
    subY = cmds.intField( value = 20, minValue = 3 )
    randomButton = cmds.radioButtonGrp( label = "Random Colors", labelArray2=["True", "False"], numberOfRadioButtons=2 )
    cmds.button( label="red", command="myColor(red=True)", backgroundColor = (1,0,0), enableBackground=True )
    cmds.button( label="green", command="myColor(green=True)", backgroundColor = (0,1,0), enableBackground=True )
    cmds.button( label="blue", command="myColor(blue=True)", backgroundColor = (0,0,1), enableBackground=True )
    cmds.showWindow()
    print myWindowVar

    def runBuild():
    "run creates the sphere"
    global numFaces
    global myColors
    global myColorCountList
    global randomVal

    myColorCountList = []
    myColors = []
    cmds.file( new = True, f = True)
    mel.eval("DisplayShaded")
    numFaces = createSphere()

    def createSphere() :
    "create polySphere"

    global radiusField
    global subX
    global subY
    global numFaces
    radius = cmds.floatField( radiusField, q = True, value = True )
    subX = cmds.intField( subX, q = True, value = True )
    subY = cmds.intField( subY, q = True, value = True )
    cmds.polySphere(sx = subX, sy=subY, r = radius)
    polyCount = cmds.polyEvaluate(f = True)
    numFaces = polyCount
    return polyCount



    def myColor(red=False,green=False,blue=False):
    "set two colors from the user input"
    global myColors
    print red
    print green
    print blue
    myColorCountList.append(1)
    if len(myColorCountList) < 3:
    if len(myColors)< 2:
    if red == True:
    myColors.append('red')
    if green == True:
    myColors.append('green')
    if blue == True:
    myColors.append('blue')
    else:
    cmds.warning("you have already selected 2 colors")
    calColor( myColors )
    print myColors

    def calColor( myColors ):
    print myColors
    color1 = None
    color2 = None
    if myColors[0] == 'red':
    color1 = [1,0,0]
    elif myColors[0] == 'green':
    color1 = [0,1,0]
    elif myColors[0] == 'blue':
    color1 = [0,0,1]
    if myColors[1] == 'red':
    color1 = [1,0,0]
    elif myColors[1] == 'green':
    color1 = [0,1,0]
    elif myColors[1] == 'blue':
    color1 = [0,0,1]
    print color1,color2
    colorRange(color1,color2)

    def colorRange(color1,color2):
    global numFaces
    global myColors
    global myColorCountList
    global randomButton
    step = 1.0 / numFaces
    step = float ("%.4f" % step)
    colorArray = []
    color1TempArray = []
    color2TempArray = []
    color2Array = []
    tempColor = 1
    finalColorList = []
    print "the step is %s" % step

    if color1 == [1,0,0]:
    i = 0.0
    while 1 <= 1:
    i = step + i
    colorTempArray.append(float(i))
    colorTempArray.sort ()
    colorTempArray.reverse ()
    for x in range (0,numFaces):
    if colorTempArray[x] > 1:
    colorTempArray[x] = 1.0
    if colorTempArray[x] < 0.01:
    colorTempArray[x] = 0.01
    colorArray.append([colorTempArray[x],0,0])

    if color1 == [0,1,0]:
    i = 0.0
    while 1 <= 1:
    i = step + i
    colorTempArray.append(float(i))
    colorTempArray.sort ()
    colorTempArray.reverse ()
    for x in range (0,numFaces):
    if colorTempArray[x] > 1:
    colorTempArray[x] = 1.0
    if colorTempArray[x] < 0.01:
    colorTempArray[x] = 0.01
    colorArray.append([0,colorTempArray[x],0])

    if color1 == [0,0,1]:
    i = 0.0
    while 1 <= 1:
    i = step + i
    colorTempArray.append(float(i))
    colorTempArray.sort ()
    for x in range (0,numFaces):
    if colorTempArray[x] > 1:
    colorTempArray[x] = 1.0
    if colorTempArray[x] < 0.01:
    colorTempArray[x] = 0.01
    colorArray.append([0,0,colorTempArray[x]])

    if color2 == [1,0,0]:
    i = 0.0
    while 1 <= 1:
    i = step + i
    colorTempArray.append(float(i))
    colorTempArray.sort ()
    for x in range (0,numFaces):
    if colorTempArray[x] > 1:
    colorTempArray[x] = 1.0
    if colorTempArray[x] < 0.01:
    colorTempArray[x] = 0.01
    color2Array.append([colorTempArray[x],0,0])

    if color2 == [0,1,0]:
    i = 0.0
    while 1 <= 1:
    i = step + i
    colorTempArray.append(float(i))
    colorTempArray.sort ()
    for x in range (0,numFaces):
    if colorTempArray[x] > 1:
    colorTempArray[x] = 1.0
    if colorTempArray[x] < 0.01:
    colorTempArray[x] = 0.01
    color2Array.append([0,colorTempArray[x],0])

    if color2 == [0,0,1]:
    i = 0.0
    while 1 <= 1:
    i = step + i
    colorTempArray.append(float(i))
    colorTempArray.sort ()
    for x in range (0,numFaces):
    if colorTempArray[x] > 1:
    colorTempArray[x] = 1.0
    if colorTempArray[x] < 0.01:
    colorTempArray[x] = 0.01
    color2Array.append([0,0,colorTempArray[x]])

    for y in range(0,len(colorArray)):
    for z in range(0,3):
    tempx = colorArray[y][0] + colorArray2[y][0]
    tempy = colorArray[y][1] + colorArray2[y][1]
    tempz = colorArray[y][2] + colorArray2[y][2]
    tempColorAdd = tempx, tempy, tempz
    finalColorList.append(tempColorAdd)
    randomVal = cmds.radioButtonGrp( randomButtom, q = True, select = True)
    print ' Random value is: %s' % randomVal
    if randomVal == 1:
    random.shuffle(finalColorList)
    print finalColorList
    assignToFace(finalColorList)

    def assignToFace(finalColorList):
    global bevelButton
    global numFaces
    cmds.select ('pSphere1', r = True)
    mySphere = cmds.ls(sl = True)[0]
    #bevelVal = cmds.radioButtonGrp (bevelButton, q = True, select = True)
    for i in range(0,numFaces):
    assignmNewMaterial('myShader'+str(i), finalColorList[i],
    'blinn', mySphere + '.f['+ str[i] + ']')


    def createMaterial( name, color, type ):
    cmds.sets( renderable=True, noSurfaceShader=True, empty=True, name=name + 'SG' )
    cmds.shadingNode( type, asShader=True, name=name )
    cmds.setAttr( name+'.color', color[0], color[1], color[2], type='double3')
    cmds.connectAttr(name+'.outColor', name+'SG.surfaceShader')

    def assignMaterial (name, object):
    cmds.sets(object, edit=True, forceElement=name+'SG')

    def assignNewMaterial( name, color, type, object):
    createMaterial (name, color, type)
    assignMaterial (name, object)

    myWindow()

    ---------------------------------------------------

    I'm learning python in maya, any suggestions on how to customize the ui or add a little something else would be greatly appreciate

    thanks for the help
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    I'll try to help if you buy autodesk for me.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo