Skip to content

Instantly share code, notes, and snippets.

@petfactory
Last active November 8, 2022 16:16
Show Gist options
  • Save petfactory/a40185504e677f0f9c2f to your computer and use it in GitHub Desktop.
Save petfactory/a40185504e677f0f9c2f to your computer and use it in GitHub Desktop.
Using a maya gradient control with PySide
from PySide import QtCore, QtGui
from shiboken import wrapInstance
import maya.OpenMayaUI as omui
from functools import partial
import pymel.core as pm
def maya_main_window():
main_window_ptr = omui.MQtUtil.mainWindow()
return wrapInstance(long(main_window_ptr), QtGui.QWidget)
class CreateCurveWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(CreateCurveWidget, self).__init__(parent)
self.setWindowFlags(QtCore.Qt.Tool)
self.resize(300,100)
self.setWindowTitle("Create curve")
# layout
self.main_layout = QtGui.QVBoxLayout()
self.setLayout(self.main_layout)
# Create a gradient control
self.gc = pm.gradientControlNoAttr(h=100, changeCommand=self.key_changed)
ptr = omui.MQtUtil.findControl(self.gc.name())
self.gradient_control_widget = wrapInstance(long(ptr), QtGui.QWidget)
self.main_layout.addWidget(self.gradient_control_widget)
# value (y), pos (x), interpolation
self.gc.setAsString('.1,.25,1, .9,.75,1')
# button
button = QtGui.QPushButton('Button')
button.clicked.connect(self.button_clicked)
self.main_layout.addWidget(button)
# button
doit_button = QtGui.QPushButton('doit')
doit_button.clicked.connect(self.doit_button_clicked)
self.main_layout.addWidget(doit_button)
def key_changed(self, val):
print(val)
def button_clicked(self):
# set the interpolation of the current key
#pm.gradientControlNoAttr(self.gc, e=True, currentKeyInterpValue=3)
# get the current key
print(self.gc.getCurrentKey())
print(self.gc.getAsString())
def doit_button_clicked(self):
num = 40
inc = 1.0 / (num-1)
spacing = 40
for n in range(num):
u = inc * n
# Query for the value on the curve at a given position.
val = pm.gradientControlNoAttr(self.gc, q=True, valueAtPoint=u)
sp = pm.polySphere(r=.5)[0]
sp.translate.set(u*spacing, val*spacing, 0)
def show():
win = CreateCurveWidget(parent=maya_main_window())
win.show()
return win
try:
win.close()
except NameError:
pass
win = show()
win.move(100,150)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment