Skip to content

Instantly share code, notes, and snippets.

@csprance
Created May 5, 2016 02:58
Show Gist options
  • Save csprance/ad2d49d5385856464e20f2ea695f9ead to your computer and use it in GitHub Desktop.
Save csprance/ad2d49d5385856464e20f2ea695f9ead to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
################################################################################
#
# open_bling_command_class.py
# Description: The Qt GUI for OpenBling
# Usage:
# Author: Chris Sprance Entrada Interactive
#
################################################################################
import lx
import lxifc
import lxu.command
import PySide
from PySide.QtGui import *
import modo
import os
import glob
from functools import partial
from shutil import copy
import modo.constants as c
import subprocess
# TODO: Get Onclick working to apply a matcap some custom command OpenBling.matcap `matcapname`
# TODO: Get the UI looking better
# Custom View Class
class OpenBlingClass(lxifc.CustomView):
def __init__(self):
lx.out('Initializing OpenBling')
self.layout = PySide.QtGui.QFormLayout()
self.scriptdir = os.path.normpath(lx.eval('query platformservice alias ? {kit_csprance_openbling:}'))
# grab the matcaps
self.matcaps = self.get_matcaps()
# create the parent layout
def onClicked_ApplyMatCap(self, path):
'''Method that applys the matcaps based on the image path'''
lx.out("Applying Matcap: " + path)
clip_name = 'cspranceopenbling_clip'
matcapShader_name = 'csprance_openbling_matcapShader'
scene = modo.Scene()
shader = scene.addItem(c.MATCAPSHADER_TYPE)
# shader.setParent(modo.Item("Render"))
# shader.name = matcapShader_name
# # add in the clip
# lx.eval('clip.addStill %s' % path)
#
# # rename the clip to some specific name we can find later
# lx.eval('item.name %s' % clip_name)
#
# # create the matcap shader
# lx.eval('shader.new matcapShader')
#
# # rename the matcap shader so we can find it later
# lx.eval('item.name %s' % matcapShader_name)
# lx.eval('item.mediaClip ')
def move_image(self, imagepath):
new_imagepath = os.path.join(self.scriptdir, 'matcaps', os.path.split(imagepath)[-1])
copy(imagepath, new_imagepath)
return new_imagepath
def onClicked_AddMatCap(self):
'''Method that creates matcap buttons and moves the image to the matcap folder'''
lx.out('Appending MatCap')
imagepath = modo.dialogs.customFile('fileOpen', 'Open MatCap',
('images',), ('Images',),
('*.png', '*.jpg', '*.tga',))
button = QPushButton(os.path.split(imagepath)[-1])
new_imagepath = self.move_image(imagepath)
button.isFlat()
qicon = QIcon(new_imagepath)
button.setIcon(qicon)
button.setIconSize(PySide.QtCore.QSize(90, 90))
button.setStyleSheet('height:90px; width:90px;min-width:90px;max-width:90px; text-align:left;')
button.clicked.connect(partial(self.onClicked_ApplyMatCap, path=imagepath))
# move the image to the matcaps folder
self.layout.addWidget(button)
def onClicked_OpenFolder(self):
try:
path = os.path.join(self.scriptdir, 'matcaps')
command = r'explorer "%s"' % path
subprocess.Popen(command)
except:
pass
def onClicked_RescanMatcaps(self):
# TODO: Get this method working
lx.out('Initializing OpenBling')
for i in reversed(range(self.layout.count())):
self.layout.itemAt(i).widget().setParent(None)
self.get_matcaps()
def get_matcaps(self):
matcaplist = list()
button = QPushButton('Add')
button.clicked.connect(self.onClicked_AddMatCap)
self.layout.addWidget(button)
open_folder_button = QPushButton('Open')
open_folder_button.clicked.connect(self.onClicked_OpenFolder)
self.layout.addWidget(open_folder_button)
rescan_matcaps_button = QPushButton('Rescan')
rescan_matcaps_button.clicked.connect(self.onClicked_RescanMatcaps)
self.layout.addWidget(rescan_matcaps_button)
for f in glob.glob(os.path.join(self.scriptdir, 'matcaps\\*.png')):
name = os.path.split(f)[-1]
path = f
button = QPushButton(name)
qicon = QIcon(path)
button.setIcon(qicon)
button.setIconSize(PySide.QtCore.QSize(90, 90))
button.setStyleSheet('height:90px; width:90px;min-width:90px;max-width:90px; text-align:left;')
button.clicked.connect(partial(self.onClicked_ApplyMatCap, path=path))
self.layout.addWidget(button)
matcaplist.append(path)
return matcaplist
def customview_Init(self, pane):
if pane == None:
return False
custPane = lx.object.CustomPane(pane)
if custPane.test() == False:
return False
# get the parent object
parent = custPane.GetParent()
# convert to PySide QWidget
p = lx.getQWidget(parent)
# Check that it suceeds
if p != None:
# connect all the buttons to the onClicked function
# and add it to the layout
self.layout.setContentsMargins(2, 2, 2, 2)
p.setLayout(self.layout)
return True
return False
lx.bless(OpenBlingClass, "OpenBling")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment