samprogramming
Programming Blog (from SCRATCH!!)
93 posts
Don't wanna be here? Send us removal request.
samprogramming · 4 years ago
Text
Lambda Syntax
Without Lambdas
def mult(a, x):    return(a*x)
def triple(a):    return(mult(a, x=3))
print(triple(a=11))
With Lambdas
def mult(x):    return(lambda a: a*x)
triple = mult(x=3)
print(triple(a=11))
Conclusion:
Wow... a hole one fewer lines. Can’t say I think it’s worth the effort.
0 notes
samprogramming · 4 years ago
Text
My first plugin
import maya.api.OpenMaya as om import maya.cmds as cmds
def maya_useNewAPI():    pass
def initializePlugin(plugin):
   vendor = 'Sam Leheny'    version = '1.0.0'
   om.MFnPlugin(plugin, vendor, version)
def uninitializePlugin(plugin):    pass
0 notes
samprogramming · 5 years ago
Text
A button that changes itself - using global variables
import maya.cmds as cmds
testButton = None
def createWindow():    global testButton    testWindow = cmds.window()    cmds.columnLayout()    testButton = cmds.button(label = '   X   ', command='buttonFunction()')    cmds.showWindow(testWindow)
createWindow()
def buttonFunction():    buttonName = cmds.button(testButton, q=True, label=True)    if buttonName == '   X   ':        cmds.button(testButton, edit=True, label='   Y   ')    else:        cmds.button(testButton, edit=True, label='   X   ')
0 notes
samprogramming · 6 years ago
Text
Python: menuBar
import maya.cmds as cmds
myWindow = cmds.window(menuBar=True) cmds.menu(label='File', tearOff=False) cmds.menuItem(label='Reset_Settings') cmds.menu(label='Help', tearOff=False) cmds.menuItem(label='How To Use')
cmds.showWindow(myWindow)
0 notes
samprogramming · 6 years ago
Text
Python: Dropdown menus (optionMenu)
import maya.cmds as cmds
myWindow = cmds.window() cmds.columnLayout() dropdown = cmds.optionMenu(label='Colours') cmds.menuItem(label='Yellow' ) cmds.menuItem(label='Purple' ) cmds.menuItem(label='Grey') cmds.menuItem(label='Blue') cmds.menuItem(label='Purple again') cmds.menuItem(label='Orange') cmds.showWindow(myWindow) runButton = cmds.button(label='Go!', width=250, command='button_command()')
#print the selected colour def button_command():    selectedColour = cmds.optionMenu(dropdown, q=True, value=True)    print(selectedColour)
Tumblr media
0 notes
samprogramming · 6 years ago
Text
Python: Fields
import maya.cmds as cmds
windowName = “Test_Window”
if cmds.window(windowName, exists=True):    cmds.deleteUI(windowName)
myWin = cmds.window(windowName)
mainFormL = cmds.formLayout() yourNameText = cmds.text(label=“Name:”) yourNameField = cmds.textField(“name”, placeholderText=“Your name”, width=160) ageText = cmds.text(label=“Age:”) ageField = cmds.intField(“your age”, min=0, step=1, width=160) interestText = cmds.text(l=“Interest:”) interestField = cmds.textField(“loveThing”, placeholderText=“A thing you love”, width=160) runButton = cmds.button(label=‘Go!’, width=250, command='button_command()’)
cmds.formLayout(mainFormL,                edit=True,                attachForm = [                    (yourNameText, “left”, 20),                    (ageText, “left”, 20),                    (interestText, “left”, 20),                    (yourNameText, “top”, 19),                    (yourNameField, “top”, 15),                    (yourNameField, “left”, 110),                    (ageField, “left”, 110),                    (interestField, “left”, 110),                    (runButton, “left”, 20)],                attachControl = [                    (ageText, “top”, 11, yourNameText),                    (interestText, “top”, 11, ageText),                    (ageField, “top”, 5, yourNameField),                    (interestField, “top”, 5, ageField),                    (runButton, “top”, 11, interestField)]) cmds.showWindow(myWin)
def button_command():    x = cmds.textField(yourNameField, q=True, text=True)    y_init = cmds.intField(ageField, q=True, value=True)    z = cmds.textField(interestField, q=True, text=True)    y = str(y_init)    print('My name is ’ + x + ’, I am ’ + y + ’ years young, and I love ’ + z + ’.’)
Tumblr media
0 notes
samprogramming · 6 years ago
Text
Python - Windows - 6
6.1 - Wrap the window in an function, and make various functions dynamic.
import maya.cmds as cmds
def button_command():    print("Thanks, you rattlesnake.")
def button_window(beforeText, buttonLabel, buttonCommand):    myWindow = cmds.window(title="Test Window",                           width = 300,                           height = 100,                           sizeable = False,                           backgroundColor = [0.15, 0.06, 0.24],                           maximizeButton = False)    cmds.columnLayout(adjustableColumn=True,                      )    cmds.text(beforeText)    cmds.button(label=buttonLabel,                height=70,                command=buttonCommand)    cmds.showWindow(myWindow)
button_window("Click the button, you rattlesnake!", "Click me, you rattlesnake", "button_command()")
Tumblr media
0 notes
samprogramming · 6 years ago
Text
Python - Windows - 5
5.1 - Add a button to the window with the button command.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.columnLayout(adjustableColumn=True,                  ) cmds.text("testing, 1, 2...") cmds.button() cmds.showWindow(myWindow)
Tumblr media
5.2 - Change the button’s text using the label flag.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.columnLayout(adjustableColumn=True,                  ) cmds.text("testing, 1, 2...") cmds.button(label="I a button, yo") cmds.showWindow(myWindow)
Tumblr media
5.3 - Change the height of the button using the height flag.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.columnLayout(adjustableColumn=True,                  ) cmds.text("testing, 1, 2...") cmds.button(label="I a button, yo",            height=70) cmds.showWindow(myWindow)
Tumblr media
5.4 - Create a definition, and assign it to execute when the button is pressed using the button’s command flag.
import maya.cmds as cmds
def button_command():    print("ACTION!")
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.columnLayout(adjustableColumn=True,                  ) cmds.text("testing, 1, 2...") cmds.button(label="I a button, yo",            height=70,            command="button_command()") cmds.showWindow(myWindow)
Tumblr media
0 notes
samprogramming · 6 years ago
Text
Python - Windows - 4
4.1 - Add a layout to the window.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.columnLayout() cmds.showWindow(myWindow)
4.2 - Add some text to the window, now that it has a layout.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.columnLayout() cmds.text("testing, 1, 2...") cmds.showWindow(myWindow)
Tumblr media
4.3 - Place the text in the middle of the column by using the columLayout’s adjustableColumn flag.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.columnLayout(adjustableColumn=True) cmds.text("testing, 1, 2...") cmds.showWindow(myWindow)
Tumblr media
0 notes
samprogramming · 6 years ago
Text
Python - Windows - 3
3.1 - Change the background colour of the window using the backgroundColor flag.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24]) cmds.showWindow(myWindow)
Tumblr media
3.2 - Since our window cannot be interactively resized, we’ve no need for the maximise button in the top right of the window. Disable it using the maximizeButton flag.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False,                       backgroundColor = [0.15, 0.06, 0.24],                       maximizeButton = False) cmds.showWindow(myWindow)
Tumblr media
0 notes
samprogramming · 6 years ago
Text
Python - Windows - 2
2.1 - Give the window assigned to the variable a name using the title flag.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window") cmds.showWindow(myWindow)
Tumblr media
2.2 - Specify the dimensions of the window using the width and height flags.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400) cmds.showWindow(myWindow)
Tumblr media
2.3 - Prevent the window from being interactively resized after creation using the sizeable flag.
import maya.cmds as cmds
myWindow = cmds.window(title="Test Window",                       width = 400,                       height = 400,                       sizeable = False) cmds.showWindow(myWindow)
0 notes
samprogramming · 6 years ago
Text
Python - Windows - 1
1.1 - Assign a window command to a variable.
import maya.cmds as cmds
myWindow = cmds.window()
1.2 - Use the show window command to execute on that window.
import maya.cmds as cmds
myWindow = cmds.window()
cmds.showWindow(myWindow)
Tumblr media
0 notes
samprogramming · 6 years ago
Text
Python 23 - Selection
23.1 - Select an object.
import maya.cmds as cmds
cmds.select("pCube1")
23.2 - Deselect an object.
import maya.cmds as cmds
cmds.select("pCube1", deselect=True)
23.3 - Select and object, then add another to the selection.
import maya.cmds as cmds
cmds.select("pCube1",)
cmds.select("pCube2", add=True)
23.4 - Clear a selection of multiple objects.
import maya.cmds as cmds
cmds.select("pCube1",) cmds.select("pCube2", add=True) cmds.select(clear=True)
0 notes
samprogramming · 6 years ago
Text
Python 22 - Dictionaries
22.1 - Create a dictionary and print one of its entries to the console.
import maya.cmds as cmds
x = {"A":[112, 314], "B":[016, 512], "C":[680, 546]} print(x["A"])
22.2 - Use the get function to return the value of a dictionary’s entry.
import maya.cmds as cmds
x = {"A":[112, 314], "B":[016, 512], "C":[680, 546]}
print(x.get("B"))
22.3 - Test an entry’s membership to a dictionary with the in method.
import maya.cmds as cmds
x = {"A":[112, 314], "B":[016, 512], "C":[680, 546]} print("C" in x)
22.4 - Use the keys and values methods to return all the keys and then all the values of a dictionary.
import maya.cmds as cmds
x = {"A":[112, 314], "B":[016, 512], "C":[680, 546]} print(x.keys()) print(x.values())
0 notes
samprogramming · 6 years ago
Text
Python 21 - Sorting lists
21.1 - Sort a list.
import maya.cmds as cmds
x = [6, 3, 9, 2, 1, 8, 4, 8, 5] x.sort() print(x)
21.2 - Print a sorted list without altering the list variable itself;
import maya.cmds as cmds
x = [6, 3, 9, 2, 1, 8, 4, 8, 5] print(sorted(x))
21.3 - Print a sorted list in reverse order.
import maya.cmds as cmds
x = [6, 3, 9, 2, 1, 8, 4, 8, 5] print(sorted(x, reverse=True))
0 notes
samprogramming · 6 years ago
Text
Python 20 - List functions
20.1 - Append a list.
import maya.cmds as cmds
x = [1, 2, 3, 4, [5, 6]] x[4].append(7) x.append(8) print(x)
20.2 - Extend a list.
import maya.cmds as cmds
x = [1, 2, 3, 4, [5, 6]] x[4].append(7) x.append(8) y = ["a", "b", "c", "d"] x.extend(y) print(x)
20.3 - Insert a new value into a list.
import maya.cmds as cmds
x = [1, 2, 3, 4, 5, 6] x.insert(2, 2.5) print(x)
20.4 - Remove a value from a list.
import maya.cmds as cmds
x = [1, 2, 2.5, 3, 4, 5, 6] x.remove(2.5) print(x)
0 notes
samprogramming · 6 years ago
Text
Python 19 - String functions
19.1 - Print the number of characters in a string.
import maya.cmds as cmds
x = "Hello world!"
print (len(x))
19.2 - Print the seventh character of a string.
import maya.cmds as cmds
x = "Hello world!"
print (x[6])
19.3 - Convert a string to all uppercase.
import maya.cmds as cmds
x = "Hello world!"
print (x.upper())
19.4 - Convert a string to all lowercase.
import maya.cmds as cmds
x = "Hello world!"
print (x.lower())
19.5 - Split a string and print something from the resulting list.
import maya.cmds as cmds
y = "1, 2, 3, 4, 5"
y.split(', ')
print y[3]
19.6 - Join a list together to form a string using the Join function.
import maya.cmds as cmds
listZ = ['hello', 'world']
stringZ = ', '.join(listZ)
print stringZ
0 notes