Skip to content

Instantly share code, notes, and snippets.

@scroll
Last active August 29, 2015 14:06
Show Gist options
  • Save scroll/f0a643dd8dead1e0d35a to your computer and use it in GitHub Desktop.
Save scroll/f0a643dd8dead1e0d35a to your computer and use it in GitHub Desktop.
html in maya
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4 import QtWebKit
import maya.cmds as cmds
import maya.OpenMayaUI as OpenMayaUI
import sip
def getMayaMainWindow():
ptr = OpenMayaUI.MQtUtil.mainWindow()
return sip.wrapinstance(long(ptr), QtCore.QObject)
#Need to inherit from QObject
class MayaHTMLBridge(QtCore.QObject):
def __init__(self):
print 'in Init MayaHTMLBridge'
QtCore.QObject.__init__(self)
#Any slots created will be exposed to javascript as a method
@QtCore.pyqtSlot(unicode)
def makeGeoObject(self, objType):
if objType=="Cube":
cmds.polyCube()
elif objType=="Sphere":
cmds.polySphere()
elif objType=="Torus":
cmds.polyTorus()
class MayaHTMLDialog(QtGui.QDialog):
def __init__(self, parent):
QtGui.QDialog.__init__(self, parent)
self.maya_html_bridge = MayaHTMLBridge()
self.setWindowTitle("Maya HTML Bridge")
self.setWindowFlags(QtCore.Qt.Tool)
#Create the web view control
self.view = QtWebKit.QWebView()
self.view.load(QtCore.QUrl("http://localhost:8000/index.html"))
webframe = self.view.page().mainFrame()
print webframe.url()
print webframe.childFrames()
#This signal gets triggered whenever the page is loaded
webframe.javaScriptWindowObjectCleared.connect(self.applyMayaHTMLBridge)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.view)
def applyMayaHTMLBridge(self):
print 'In applyMayaHTMLBridge'
webframe = self.view.page().mainFrame()
webframe.addToJavaScriptWindowObject("maya_interface",
self.maya_html_bridge)
def Launch():
dlg = MayaHTMLDialog(getMayaMainWindow())
dlg.show()
Launch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment