Python Scripting Help!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am a university student, that is brand new to Python, and Qt completely, and am having some problems. I have to build a tool, and I am making a tool that creates and lays out shading nodes for Arnold shaders. The way the tool works is that you type in a name, then choose to add a prefix or a suffix to the name, and then based on what channels you want for the shader, it creates the required nodes, connects them and then renames them.
However I am getting problems with the tool, as it does not create all of the nodes properly and runs into errors. The biggest problem is that it does not take into consideration if the channel checkboxes and will create them anyway.
Please if you have any answers on how to get this to work, let me know, also, as I am new I would rather do what I can to get it to work rather than to optimise my code. This is probably my weakest section, so any corrections would be much appreciated. I have attached the Qt UI below.
import maya.cmds as cmds
from maya import OpenMayaUI as omui
import os.path
from functools import partial
import mtoa.utils
try:
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2.QtUiTools import *
from shiboken2 import wrapInstance
except ImportError:
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtUiTools import *
from shiboken import wrapInstance
mayaMainWindowPtr = omui.MQtUtil.mainWindow()
mayaMainWindow = wrapInstance(long(mayaMainWindowPtr), QWidget)
class QtSampler(QWidget):
def initUI(self):
loader = QUiLoader()
currentDir = os.path.dirname(__file__)
file = QFile(currentDir + "/Arnold_Shader_Layout.ui")
file.open(QFile.ReadOnly)
self.ui = loader.load(file, parentWidget=self)
file.close()
def __init__(self,*args, **kwargs):
super(QtSampler,self, *args).__init__(**kwargs)
self.setParent(mayaMainWindow)
self.setWindowFlags(Qt.Window)
self.initUI()
### Define global event triggers
# Define name_input
self.ui.name_input.textChanged.connect(self.update_name_input)
# Define prefix_radio_button and suffix_radio_button
self.ui.prefix_radio_button.clicked.connect(partial(self.update_prefix_radio_button, 'Prefix'))
self.ui.suffix_radio_button.clicked.connect(partial(self.update_suffix_radio_button, 'Suffix'))
### Define local event triggers for custom shader
# Define checkboxes for different channels
self.ui.albedo_colour.stateChanged.connect(self.update_albedo_colour)
self.ui.specular_colour.stateChanged.connect(self.update_specular_colour)
self.ui.specular_roughness.stateChanged.connect(self.update_specular_roughness)
self.ui.metalness_map.stateChanged.connect(self.update_metalness_map)
self.ui.sss_map.stateChanged.connect(self.update_sss_map)
self.ui.transmission_map.stateChanged.connect(self.update_transmission_map)
self.ui.bump_map.stateChanged.connect(self.update_bump_map)
self.ui.custom_shader_button.clicked.connect(self.update_custom_shader_button)
### Define global event triggers as functions
# Define update_name_input and runs 'if' functions
def update_name_input(self,*args):
global shader_name
shader_name = self.ui.name_input.toPlainText()
if len(shader_name) >= 2:
# Produces error if numerical characters are used in the name
if (not shader_name.isalpha()):
cmds.error('Error, shader name contains numerical characters. Please remove any integers.')
# Converts the shader name into a string
else:
shader_name = str(shader_name)
# Produces error if the length of the name is less than or equal to 2
else:
cmds.error('Error, shader name is less than two characters long. Please enter a longer name')
# Define update_prefix_radio_button
def update_prefix_radio_button(self, prefix_value):
global pre_suf
pre_suf = 1
return pre_suf
# Define update_suffix_radio_button
def update_suffix_radio_button(self, suffix_value):
global pre_suf
pre_suf = 0
return pre_suf
### Define local event triggers for the custom shader as functions
# Define all checkboxes as functions
def update_albedo_colour(self, *args):
global albedo_colour_value
albedo_colour_value = True
return albedo_colour_value
def update_specular_colour(self, *args):
global specular_colour_value
specular_colour_value = True
return specular_colour_value
def update_specular_roughness(self, *args):
global specular_roughness_value
specular_roughness_value = True
return specular_roughness_value
def update_metalness_map(self, *args):
global metalness_map_value
metalness_map_value = True
return metalness_map_value
def update_transmission_map(self, *args):
global transmission_map_value
transmission_map_value = True
return transmission_map_value
def update_sss_map(self, *args):
global sss_map_value
sss_map_value = True
return sss_map_value
def update_bump_map(self, *args):
global bump_map_value
bump_map_value = True
return bump_map_value
# Define button for custom shader tool
def update_custom_shader_button(self,*args):
# Base Standard Surface Shader
cmds.shadingNode('aiStandardSurface', asShader=True, name='custombase')
# Diffuse Colour choice
if albedo_colour_value == True:
cmds.shadingNode('aiMixShader', asShader=True, name='customdiffuse1')
cmds.shadingNode('aiColorCorrect', asShader=True, name='customdiffuse2')
cmds.shadingNode('aiImage', asShader=True, name='customdiffuse3')
# Connect diffuse colour nodes
cmds.connectAttr('customdiffuse1.outColor', 'custombase.baseColor')
cmds.connectAttr('customdiffuse2.outColor', 'customdiffuse1.shader2')
cmds.connectAttr('customdiffuse3.outColor', 'customdiffuse2.input')
# Rename diffuse colour nodes
if pre_suf == True:
cmds.rename('customdiffuse1', ("difcol_mix_" + shader_name))
cmds.rename('customdiffuse2', ("difcol_cc_" + shader_name))
cmds.rename('customdiffuse3', ("difcol_img_" + shader_name))
else:
cmds.rename('customdiffuse1', (shader_name) + "_difcol_mix")
cmds.rename('customdiffuse2', (shader_name) + "_difcol_cc")
cmds.rename('customdiffuse3', (shader_name) + "_difcol_img")
cmds.rename('customdiffuse5', (shader_name) + "_difcol_maskcc")
cmds.rename('customdiffuse6', (shader_name) + "_difcol_maskimg")
else:
print("Ignoring diffuse colour channel")
# Specular Colour choice
if specular_colour_value == True:
# Creates specular colour nodes
cmds.shadingNode('aiColorCorrect', asShader=True, name='customspecular1')
cmds.shadingNode('aiImage', asShader=True, name='customspecular2')
# Connects specular colour nodes
cmds.connectAttr('customspecular1.outColor', 'custombase.specularColor')
cmds.connectAttr('customspecular2.outColor', 'customspecular1.input')
# Rename specular colour nodes dependent on prefix or suffix choice
if pre_suf == True:
cmds.rename('customspecular1', ('specCC_' + shader_name))
cmds.rename('customspecular2', ('specimg_' + shader_name))
else:
cmds.rename('customspecular1', (shader_name + '_specCC'))
cmds.rename('customspecular2', (shader_name + '_specimg'))
else:
print("Ignoring specular colour channel")
# Specular Roughness choice
if specular_roughness_value == True:
# Create specular roughness nodes
cmds.shadingNode('aiColorCorrect', asShader=True, name="customroughness1")
cmds.shadingNode('aiImage', asShader=True, name="customroughness2")
# Connect specular roughness nodes
cmds.connectAttr('customroughness1.outColorR', 'custombase.specularRoughness')
cmds.connectAttr('customroughness2.outColor', 'customroughness1.input')
if pre_suf == True:
cmds.rename('customroughness1', ('specroughcc_' + shader_name))
cmds.rename('customroughness2', ('specroughimg_' + shader_name))
else:
cmds.rename('customroughness1', (shader_name + '_specroughcc'))
cmds.rename('customroughness2', (shader_name + '_specroughimg'))
else:
print("Ignoring specular roughness channel")
# Metalness choice
if metalness_map_value == True:
# Create metalness nodes
cmds.shadingNode('aiColorCorrect', asShader=True, name='metalness1')
cmds.shadingNode('aiImage', asShader=True, name='metalness2')
# Connects all of the metalness nodes
cmds.connectAttr('metalness1.outColorR', 'custombase.metalness')
cmds.connectAttr('metalness2.outColor', 'metalness1.input')
if pre_suf == 1:
cmds.rename('metalness1', ('metalcc_' + shader_name))
cmds.rename('metalness2', ('metalimg_' + shader_name))
else:
cmds.rename('metalness1', (shader_name + '_metalcc'))
cmds.rename('metalness2', (shader_name + '_metalimg'))
else:
print("Ignoring metalness channel")
if transmission_map_value == True:
# Creates required nodes for the transmission shader
cmds.shadingNode('aiClamp', asShader=True, name='transmission1')
cmds.shadingNode('aiColorCorrect', asShader=True, name='transmission2')
cmds.shadingNode('aiImage', asShader=True, name='transmission3')
# Connects all of the transmission nodes
cmds.connectAttr('transmission2.outColor', 'transmission1.input')
cmds.connectAttr('transmission3.outColor', 'transmission2.input')
cmds.connectAttr('transmission1.outColorR', 'custombase.transmission')
if pre_suf == 1:
# Renames the nodes with a prefix
cmds.rename('transmission1', ('tra_clamp_' + shader_name))
cmds.rename('transmission2', ('tra_cc_' + shader_name))
cmds.rename('transmission3', ('tra_img_' + shader_name))
else:
cmds.rename('transmission1', (shader_name + '_tra_clamp'))
cmds.rename('transmission2', (shader_name + '_tra_cc'))
cmds.rename('transmission3', (shader_name + '_tra_img'))
else:
print("Ignoring transmission channel")
if sss_map_value == True:
# Create the nodes for SSS map
cmds.shadingNode('aiMixShader', asShader=True, name='SSS1')
cmds.shadingNode('aiMultiply', asShader=True, name='SSS2')
cmds.shadingNode('aiClamp', asShader=True, name='SSS3')
cmds.shadingNode('aiColorCorrect', asShader=True, name='SSS4')
cmds.shadingNode('aiImage', asShader=True, name='SSS5')
cmds.shadingNode('aiColorCorrect', asShader=True, name='SSS6')
cmds.shadingNode('aiImage', asShader=True, name='SSS7')
cmds.shadingNode('aiColorCorrect', asShader=True, name='SSS8')
cmds.shadingNode('aiImage', asShader=True, name='SSS9')
# Connect all of the nodes for SSS map
cmds.connectAttr('SSS2.outColor', 'SSS1.shader1')
cmds.connectAttr('SSS3.outColor', 'SSS2.input1')
cmds.connectAttr('SSS4.outColor', 'SSS3.input')
cmds.connectAttr('SSS5.outColor', 'SSS4.input')
cmds.connectAttr('SSS6.outColor', 'SSS2.input2')
cmds.connectAttr('SSS7.outColor', 'SSS6.input')
cmds.connectAttr('SSS8.outColor', 'SSS1.shader2')
cmds.connectAttr('SSS9.outColor', 'SSS8.input')
cmds.connectAttr('SSS1.outColor', 'custombase.subsurfaceColor')
# Rename all fo the nodes for the SSS
if pre_suf == 1:
cmds.rename('SSS1', ('sss_mix_' + shader_name))
cmds.rename('SSS2', ('sss_mult_' + shader_name))
cmds.rename('SSS3', ('sss_maskclamp_' + shader_name))
cmds.rename('SSS4', ('sss_maskcc_' + shader_name))
cmds.rename('SSS5', ('sss_maskimg' + shader_name))
cmds.rename('SSS6', ('sss_cc' + shader_name))
cmds.rename('SSS7', ('sss_img' + shader_name))
cmds.rename('SSS8', ('sss_base_cc' + shader_name))
cmds.rename('SSS9', ('sss_base_img' + shader_name))
else:
cmds.rename('SSS1', (shader_name + '_sss_mix'))
cmds.rename('SSS2', (shader_name + '_sss_mult'))
cmds.rename('SSS3', (shader_name + '_sss_maskclamp'))
cmds.rename('SSS4', (shader_name + '_sss_maskcc'))
cmds.rename('SSS5', (shader_name + '_sss_maskimg'))
cmds.rename('SSS6', (shader_name + '_sss_cc'))
cmds.rename('SSS7', (shader_name + '_sss_img'))
cmds.rename('SSS8', (shader_name + '_sss_base_cc'))
cmds.rename('SSS9', (shader_name + '_sss_base_img'))
else:
print("Ignoring Sub Surface Scattering Channel")
# Bump choice
if bump_map_value == True:
# Create the nodes for bump map
cmds.shadingNode('bump2d', asShader=True, name='bump1')
cmds.shadingNode('aiClamp', asShader=True, name='bump2')
cmds.shadingNode('aiColorCorrect', asShader=True, name='bump3')
cmds.shadingNode('aiImage', asShader=True, name='bump4')
# Connects the bump nodes together
cmds.connectAttr('bump1.outNormal', 'custombase.normalCamera')
cmds.connectAttr('bump2.outColorR', 'bump1.bumpValue')
cmds.connectAttr('bump3.outColor', 'bump2.input')
cmds.connectAttr('bump4.outColor', 'bump3.input')
if pre_suf == 1:
cmds.rename('bump1', ('bump2d_' + shader_name))
cmds.rename('bump2', ('bumpclamp_' + shader_name))
cmds.rename('bump3', ('bumpcc_' + shader_name))
cmds.rename('bump4', ('bumpimg_' + shader_name))
else:
cmds.rename('bump1', (shader_name + '_bump2d'))
cmds.rename('bump2', (shader_name + '_bumpclamp'))
cmds.rename('bump3', (shader_name + '_bumpcc'))
cmds.rename('bump4', (shader_name + '_bumpimg'))
else:
print("Ignoring Bump and Normal channels")
# Rename the base shader to the shader_name
cmds.rename('custombase', (shader_name))
def main():
ui = QtSampler()
ui.show()
return ui
if __name__ == '__main__':
main()