一半君的总结纸

听话只听一半君

各处找来的常用python code snippet ( for maya )

各种print的用法

import maya.OpenMaya as om
om.MGlobal.displayInfo("my grey info message")
om.MGlobal.displayWarning("my pink warning")
om.MGlobal.displayError("my red error message")

# sys.stdout = sys.__stdout__
sys.stdout.write('script editor\n')
sys.__stdout__ .write('output window\n')
print 'xxx'

python_inside_maya

nathan网站大合集
Guide to PyQt/PySide in Maya (Pt. 1)
PyQt and Maya 2011

#Source code for some common Maya/PyQt functions we will be using
import os
import sip
from PyQt4 import QtGui, QtCore, uic
import maya.OpenMayaUI as apiUI
import maya.cmds as cmds

def getMayaWindow():
	"""
	Get the main Maya window as a QtGui.QMainWindow instance
	@return: QtGui.QMainWindow instance of the top level Maya windows
	"""
	ptr = apiUI.MQtUtil.mainWindow()
	if ptr is not None:
		return sip.wrapinstance(long(ptr), QtCore.QObject)

def toQtObject(mayaName):
	"""
	Convert a Maya ui path to a Qt object
	@param mayaName: Maya UI Path to convert (Ex: "scriptEditorPanel1Window|TearOffPane|scriptEditorPanel1|testButton" )
	@return: PyQt representation of that object
	"""
	ptr = apiUI.MQtUtil.findControl(mayaName)
	if ptr is None:
		ptr = apiUI.MQtUtil.findLayout(mayaName)
	if ptr is None:
		ptr = apiUI.MQtUtil.findMenuItem(mayaName)
	if ptr is not None:
		return sip.wrapinstance(long(ptr), QtCore.QObject)

#Get the absolute path to my ui file
uiFile = os.path.join(cmds.internalVar(upd=True), 'ui', 'demo.ui') # maya2011\prefs\ui\demo.ui
print 'Loading ui file:', os.path.normpath(uiFile)

#Load the ui file, and create my class
form_class, base_class = uic.loadUiType(uiFile)
class Window(base_class, form_class):
	def __init__(self, parent=getMayaWindow()):
		'''A custom window with a demo set of ui widgets'''
		#init our ui using the MayaWindow as parent
		super(base_class, self).__init__(parent)
		#uic adds a function to our class called setupUi, calling this creates all the widgets from the .ui file
		self.setupUi(self)
		self.setObjectName('myWindow')
		self.setWindowTitle("My Qt Demo Window")

def main():
	global myWindow
	myWindow = Window()
	myWindow.show()

Some PyQt for the day
Here’s a way to add popup tool tips to the 3d view ports in Maya (Based on inspiration from this post)

import sip
import maya.cmds as cmds
import maya.OpenMayaUI as apiUI
from PyQt4 import QtGui, QtCore

class ToolTipFilter(QtCore.QObject):
	'''A simple event filter to catch tooltip events'''
	def eventFilter(self, obj, event):
		if event.type() == QtCore.QEvent.ToolTip:
			QtGui.QToolTip.hideText() #Hide the old tooltip, so that it can move
			QtGui.QToolTip.showText(event.globalPos(), '%04f, %04f'%(event.globalX(), event.globalY()), obj)
			return False
		return True

#Install the event filter into all the model panels
global filter #Have to make the filter object global so it doesnt get garbage collected
filter = ToolTipFilter()

for editor in cmds.lsUI(panels=True): #For soem reason type='modelEditor' won't work...
	if cmds.objectTypeUI(editor)=='modelEditor':
		ptr = apiUI.MQtUtil.findControl(editor)
		viewWidget = sip.wrapinstance(long(ptr), QtCore.QObject)
		viewWidget.installEventFilter(filter)

Here’s some simple code to get the widget with focus, under the cursor, and the main Maya window:

import sip
import maya.cmds as cmds
import maya.OpenMayaUI as apiUI
from PyQt4 import QtGui, QtCore

def getMayaWindow():
	'Get the maya main window as a QMainWindow instance'
	ptr = apiUI.MQtUtil.mainWindow()
	return sip.wrapinstance(long(ptr), QtCore.QObject)

def getFocusWidget():
	'Get the currently focused widget'
	return QtGui.qApp.focusWidget()

def getWidgetAtMouse():
	'Get the widget under the mouse'
	currentPos = QtGui.QCursor().pos()
	widget = QtGui.qApp.widgetAt(currentPos)
	return widget

Here’s some handy debug code, this changes the tooltips for all the QWidgets in Maya to display their path, and QObject instance:

import sip
import maya.cmds as cmds
import maya.OpenMayaUI as apiUI
from PyQt4 import QtGui, QtCore

def toQtObject(mayaName):
	'''
    Given the name of a Maya UI element of any type, return the corresponding QWidget or QAction.
    If the object does not exist, returns None
    '''
	ptr = apiUI.MQtUtil.findControl(mayaName)
	if ptr is None:
		ptr = apiUI.MQtUtil.findLayout(mayaName)
		if ptr is None:
			ptr = apiUI.MQtUtil.findMenuItem(mayaName)
	if ptr is not None:
		return sip.wrapinstance(long(ptr), QtCore.QObject)

#Set the tooltip for all widgets (This takes a little bit)
for widget in cmds.lsUI(dumpWidgets=True):
	try:
		qWidget = toQtObject(widget)
		qWidget.setToolTip('(%s) %s'%(widget, qWidget))
	except:
		pass

Convert a wrapped pyqt object back to a Maya UI path:

import sip
import maya.cmds as cmds
import maya.OpenMayaUI as apiUI
from PyQt4 import QtGui, QtCore

def toQtObject(mayaName):
	'''
    Given the name of a Maya UI element of any type, return the corresponding QWidget or QAction.
    If the object does not exist, returns None
    '''
	ptr = apiUI.MQtUtil.findControl(mayaName)
	if ptr is None:
		ptr = apiUI.MQtUtil.findLayout(mayaName)
		if ptr is None:
			ptr = apiUI.MQtUtil.findMenuItem(mayaName)
	if ptr is not None:
		return sip.wrapinstance(long(ptr), QtCore.QObject)

#Set the tooltip for all widgets (This takes a little bit)
for widget in cmds.lsUI(dumpWidgets=True):
	try:
		qWidget = toQtObject(widget)
		qWidget.setToolTip('(%s) %s'%(widget, qWidget))
	except:
		pass

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: