maya灯光导入houdini插件开发
阅读原文时间:2023年07月09日阅读:4

加入工作室时师兄给了两道测试题,由于第一道是完善师兄的一个houdini项目管理插件,我只是开发了一些小功能,所以不好意思拿出来。

第二道题就完全是由自己开发的一个小插件,功能是把maya里的灯光导入到houdini里面。刚开始拿到测试题的时候有点慌,毕竟没怎么做过插件开发,还好师兄给了足够的时间,而且后面发现也没那么难,不过生了一场病,接连在床上躺了几天,不过后来都做出来了。基本做出来之后我就佛了,一天一天做一点,顺便学东西,到最后期限,刚好就把测试题交了,也算是有惊无险加入工作室。

插件下载下载地址

接下来就开始讲怎么开发插件:

maya刚开始学插件开发,用的都是cmd这个库,因为伟添师兄博客上有很多学习笔记,确实帮了我很大的忙,而且最后发现师兄给的一个教程里面有一个就是讲灯光的,不过我已经基本按自己的思路做完了,也就没改。

而houdini里面就基本使用了hou模块开发。

由于刚开始学,而且参照师兄给的另一个插件,就都是用绝对路径写代码,可能对于用户很不友好,后来在做另一个插件时也研究了一下怎么使用相对路径,就是采用python的os库,但也不想改了,毕竟也可以记录一下自己的学习路径(更大原因还是因为懒)

首先是在qt里面做一些基本的ui界面,如图,我这里是做了好几个,因为有几种灯光,然后就可以实现灯光界面的切换

然后是写maya里面的代码,在编辑器(这里推荐vscode,或者pycharm)里面写上代码:

from PySide2 import QtWidgets, QtCore, QtGui
import pymel.core as pm
import PySide2.QtUiTools as QLoader
import maya.cmds as cmds
import json

main_ui = r"C:\Users\huangPeiXin\Desktop\sendLights\src\main.ui"
amibientLight_ui = r"C:\Users\huangPeiXin\Desktop\sendLights\src\amibientLight.ui"
areaLight_ui = r"C:\Users\huangPeiXin\Desktop\sendLights\src\areaLight.ui"
spotLight_ui = r"C:\Users\huangPeiXin\Desktop\sendLights\src\spotLight.ui"
pointLight_ui = r"C:\Users\huangPeiXin\Desktop\sendLights\src\pointLight.ui"
volumeLight_ui = r"C:\Users\huangPeiXin\Desktop\sendLights\src\volumeLight.ui"
directionalLight_ui = r"C:\Users\huangPeiXin\Desktop\sendLights\src\directionalLight.ui"
light_json = r"C:\Users\huangPeiXin\Desktop\sendLights\src\light_json.json"

def getLightAttr(light):
    #获取灯光属性

    #保存灯光名称
    lightName = str(light.getTransform())

    #用于保存每一盏灯光的属性
    lightChild = {}

    #根据灯光类型加载对应灯光属性
    if pm.objectType(light) == "ambientLight":
        lightChild["lightType"] = "ambientLight"
        lightChild["translate"] = cmds.getAttr(lightName + ".translate")
        lightChild["rotate"] = cmds.getAttr(lightName + ".rotate")
        lightChild["scale"] = cmds.getAttr(lightName + ".scale")
        lightChild["visibility"] = cmds.getAttr(lightName + ".visibility")
        lightChild["color"] = cmds.getAttr(lightName + ".color")
        lightChild["intensity"] = cmds.getAttr(lightName + ".intensity")
        lightChild["useRayTraceShadows"] = cmds.getAttr(lightName + ".useRayTraceShadows")
        lightChild["shadColor"] = [cmds.getAttr(lightName + ".shadColorR"),
        cmds.getAttr(lightName + ".shadColorG"),cmds.getAttr(lightName + ".shadColorB"),]

    elif pm.objectType(light) == "pointLight":
        lightChild["lightType"] = "pointLight"
        lightChild["translate"] = cmds.getAttr(lightName + ".translate")
        lightChild["rotate"] = cmds.getAttr(lightName + ".rotate")
        lightChild["scale"] = cmds.getAttr(lightName + ".scale")
        lightChild["visibility"] = cmds.getAttr(lightName + ".visibility")
        lightChild["color"] = cmds.getAttr(lightName + ".color")
        lightChild["intensity"] = cmds.getAttr(lightName + ".intensity")
        lightChild["useRayTraceShadows"] = cmds.getAttr(lightName + ".useRayTraceShadows")
        lightChild["shadColor"] = [cmds.getAttr(lightName + ".shadColorR"),
        cmds.getAttr(lightName + ".shadColorG"),cmds.getAttr(lightName + ".shadColorB"),]
        lightChild["useDepthMapShadows"] = cmds.getAttr(lightName + ".useDepthMapShadows")
        lightChild["dmapBias"] = cmds.getAttr(lightName + ".dmapBias")
        lightChild["aiExposure"] = cmds.getAttr(lightName + ".aiExposure")
        lightChild["aiRadius"] = cmds.getAttr(lightName + ".aiRadius")

    elif pm.objectType(light) == "spotLight":
        lightChild["lightType"] = "spotLight"
        lightChild["translate"] = cmds.getAttr(lightName + ".translate")
        lightChild["rotate"] = cmds.getAttr(lightName + ".rotate")
        lightChild["scale"] = cmds.getAttr(lightName + ".scale")
        lightChild["visibility"] = cmds.getAttr(lightName + ".visibility")
        lightChild["color"] = cmds.getAttr(lightName + ".color")
        lightChild["intensity"] = cmds.getAttr(lightName + ".intensity")
        lightChild["useRayTraceShadows"] = cmds.getAttr(lightName + ".useRayTraceShadows")
        lightChild["shadColor"] = [cmds.getAttr(lightName + ".shadColorR"),
        cmds.getAttr(lightName + ".shadColorG"),cmds.getAttr(lightName + ".shadColorB"),]
        lightChild["useDepthMapShadows"] = cmds.getAttr(lightName + ".useDepthMapShadows")
        lightChild["dmapBias"] = cmds.getAttr(lightName + ".dmapBias")
        lightChild["aiExposure"] = cmds.getAttr(lightName + ".aiExposure")
        lightChild["aiRadius"] = cmds.getAttr(lightName + ".aiRadius")
        lightChild["coneAngle"] = cmds.getAttr(lightName + ".coneAngle")
        lightChild["penumbraAngle"] = cmds.getAttr(lightName + ".penumbraAngle")
        lightChild["dropoff"] = cmds.getAttr(lightName + ".dropoff")

    elif pm.objectType(light) == "directionalLight":
        lightChild["lightType"] = "directionalLight"
        lightChild["translate"] = cmds.getAttr(lightName + ".translate")
        lightChild["rotate"] = cmds.getAttr(lightName + ".rotate")
        lightChild["scale"] = cmds.getAttr(lightName + ".scale")
        lightChild["visibility"] = cmds.getAttr(lightName + ".visibility")
        lightChild["color"] = cmds.getAttr(lightName + ".color")
        lightChild["intensity"] = cmds.getAttr(lightName + ".intensity")
        lightChild["useRayTraceShadows"] = cmds.getAttr(lightName + ".useRayTraceShadows")
        lightChild["shadColor"] = [cmds.getAttr(lightName + ".shadColorR"),
        cmds.getAttr(lightName + ".shadColorG"),cmds.getAttr(lightName + ".shadColorB"),]
        lightChild["useDepthMapShadows"] = cmds.getAttr(lightName + ".useDepthMapShadows")
        lightChild["dmapBias"] = cmds.getAttr(lightName + ".dmapBias")
        lightChild["aiExposure"] = cmds.getAttr(lightName + ".aiExposure")
        lightChild["aiAngle"] = cmds.getAttr(lightName + ".aiAngle")

    elif pm.objectType(light) == "areaLight":
        lightChild["lightType"] = "areaLight"
        lightChild["translate"] = cmds.getAttr(lightName + ".translate")
        lightChild["rotate"] = cmds.getAttr(lightName + ".rotate")
        lightChild["scale"] = cmds.getAttr(lightName + ".scale")
        lightChild["visibility"] = cmds.getAttr(lightName + ".visibility")
        lightChild["color"] = cmds.getAttr(lightName + ".color")
        lightChild["intensity"] = cmds.getAttr(lightName + ".intensity")
        lightChild["useRayTraceShadows"] = cmds.getAttr(lightName + ".useRayTraceShadows")
        lightChild["shadColor"] = [cmds.getAttr(lightName + ".shadColorR"),
        cmds.getAttr(lightName + ".shadColorG"),cmds.getAttr(lightName + ".shadColorB"),]
        lightChild["useDepthMapShadows"] = cmds.getAttr(lightName + ".useDepthMapShadows")
        lightChild["dmapBias"] = cmds.getAttr(lightName + ".dmapBias")
        lightChild["aiExposure"] = cmds.getAttr(lightName + ".aiExposure")
        lightChild["aiSpread"] = cmds.getAttr(lightName + ".aiSpread")
        lightChild["aiRoundness"] = cmds.getAttr(lightName + ".aiRoundness")
        lightChild["aiSoftEdge"] = cmds.getAttr(lightName + ".aiSoftEdge")

    elif pm.objectType(light) == "volumeLight":
        lightChild["lightType"] = "volumeLight"
        lightChild["translate"] = cmds.getAttr(lightName + ".translate")
        lightChild["rotate"] = cmds.getAttr(lightName + ".rotate")
        lightChild["scale"] = cmds.getAttr(lightName + ".scale")
        lightChild["visibility"] = cmds.getAttr(lightName + ".visibility")
        lightChild["color"] = cmds.getAttr(lightName + ".color")
        lightChild["intensity"] = cmds.getAttr(lightName + ".intensity")
        lightChild["useRayTraceShadows"] = cmds.getAttr(lightName + ".useRayTraceShadows")
        lightChild["shadColor"] = [cmds.getAttr(lightName + ".shadColorR"),
        cmds.getAttr(lightName + ".shadColorG"),cmds.getAttr(lightName + ".shadColorB"),]
        lightChild["useDepthMapShadows"] = cmds.getAttr(lightName + ".useDepthMapShadows")
        lightChild["dmapBias"] = cmds.getAttr(lightName + ".dmapBias")
        lightChild["aiExposure"] = cmds.getAttr(lightName + ".aiExposure")
        lightChild["aiRadius"] = cmds.getAttr(lightName + ".aiRadius")

    return lightChild

class LightManager(QtWidgets.QWidget):
    #灯光管理器
    def __init__(self):
        super(LightManager, self).__init__()
        self.setObjectName("project_manager")

        #加载初始界面
        loader = QLoader.QUiLoader()
        self.ui = loader.load(main_ui)
        self.ui.setParent(self)
        self.setMaximumSize(450, 700)
        self.setMinimumSize(450, 700)

        #获取初始界面控件
        self.comboBox = self.ui.findChild(QtWidgets.QComboBox, "comboBox")
        self.widget = self.ui.findChild(QtWidgets.QWidget, "widget")
        self.btn_loadAttr = self.ui.findChild(QtWidgets.QPushButton, "btn_loadAttr")
        self.btn_sendAttr = self.ui.findChild(QtWidgets.QPushButton, "btn_sendAttr")

        #获取灯光列表
        self.light_list = self.getLight()

        #加载灯光
        self.creatChild()

        #如果场景中没有灯光,弹出提示没有灯光,如果有,加载第一盏灯光
        if self.light_list == []:
            msg = QtWidgets.QMessageBox()
            msg.setParent(self)
            msg.setText("There is no light")

        else:

            self.layout = QtWidgets.QVBoxLayout()
            self.widget.setLayout(self.layout)
            self.childWin = LightWidget(self.light_list[0],self.light_list,0)
            self.childWin.setMaximumSize(600, 800)
            self.childWin.setMinimumSize(600, 800)
            self.layout.addWidget(self.childWin)

        self.comboBox.currentIndexChanged.connect(lambda:  self.creatChildWin(self.comboBox.currentIndex()))
        self.btn_sendAttr.clicked.connect(self.exportLight)
        self.btn_loadAttr.clicked.connect(self.changeLight)

    def creatChildWin(self,index):
        #创建子窗口

        self.layout.removeWidget(self.childWin)
        print pm.objectType(self.light_list[index])
        print self.light_list[index]
        self.childWin = LightWidget(self.light_list[index],self.light_list,index)
        self.childWin.setMaximumSize(420, 530)
        self.childWin.setMinimumSize(420, 530)
        self.layout.addWidget(self.childWin)

    def creatChild(self):
        #根据灯光的多少为comboBox添加子项目

        for light in self.light_list:
            self.comboBox.addItem(str(light.getTransform()))

    def getLight(self):
        #获取场景中所有的灯光

        light_list = []
        for light in pm.ls(type=["ambientLight","areaLight","spotLight","pointLight","directionalLight","volumeLight"]):
            light_list.append(light)

        return light_list

    def changeLight(self):
        #重新加载灯光属性

        #移除原有列表
        self.comboBox.clear()
        #重新加载灯光列表
        self.light_list = self.getLight()
        self.creatChild()

        #判断场景是否还有灯光
        if self.light_list == []:
            return 0

        else:

            self.layout.removeWidget(self.childWin)
            self.childWin = LightWidget(self.light_list[0],self.light_list,0)
            self.childWin.setMaximumSize(420, 530)
            self.childWin.setMinimumSize(420, 530)
            self.layout.addWidget(self.childWin)

    def exportLight(self):
        #导出灯光属性到json文件
        light_dir = {}

        #获取场景中所有的灯光
        self.light_list = self.getLight()

        for light in self.light_list:

            #获取保存灯光名称
            lightName = str(light.getTransform())

            light_dir[lightName] = getLightAttr(light)

        #写入到json文件中
        with open(light_json,'w') as f:
            json.dump(light_dir, f, indent=4)

class LightWidget(QtWidgets.QWidget):

    #灯光界面基类,用于获取灯光的基础属性,
    def __init__(self,light,light_list,index):

        super(LightWidget, self).__init__()

        # #获取属性
        self.light = light
        self.light_list = light_list
        self.lightAttr = getLightAttr(light)

        #根据灯光类型,加载不同灯光界面
        if pm.objectType(self.light_list[index])=="ambientLight":

            loader = QLoader.QUiLoader()
            self.ui = loader.load(amibientLight_ui)
            self.ui.setParent(self)
            self.lineEdit_type = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_type")
            self.lineEdit_type.setText(pm.objectType(self.light_list[index]))

        elif pm.objectType(self.light_list[index])=="areaLight":

            loader = QLoader.QUiLoader()
            self.ui = loader.load(areaLight_ui)
            self.ui.setParent(self)

            self.lineEdit_type = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_type")
            self.lineEdit_udms = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_udms")
            self.lineEdit_db = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_db")
            self.lineEdit_ae = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_ae")
            self.lineEdit_as = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_as")
            self.lineEdit_ar = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_ar")
            self.lineEdit_ase = self.ui.findChild(QtWidgets.QLineEdit,"LineEdit_ase")

            self.lineEdit_type.setText(pm.objectType(self.light_list[index]))
            self.lineEdit_udms.setText(str(self.lightAttr["useDepthMapShadows"]))
            self.lineEdit_db.setText(str(self.lightAttr["dmapBias"]))
            self.lineEdit_ae.setText(str(self.lightAttr["aiExposure"]))
            self.lineEdit_as.setText(str(self.lightAttr["aiSpread"]))
            self.lineEdit_ar.setText(str(self.lightAttr["aiRoundness"]))
            self.lineEdit_ase.setText(str(self.lightAttr["aiSoftEdge"]))

        elif pm.objectType(self.light_list[index])=="spotLight":

            loader = QLoader.QUiLoader()
            self.ui = loader.load(spotLight_ui)
            self.ui.setParent(self)

            self.lineEdit_type = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_type")
            self.lineEdit_udms = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_udms")
            self.lineEdit_db = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_db")
            self.lineEdit_ae = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_ae")
            self.lineEdit_ca = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_ca")
            self.lineEdit_pa = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_pa")
            self.lineEdit_do = self.ui.findChild(QtWidgets.QLineEdit,"LineEdit_do")
            self.lineEdit_ar = self.ui.findChild(QtWidgets.QLineEdit,"LineEdit_ar")

            self.lineEdit_type.setText(pm.objectType(self.light_list[index]))
            self.lineEdit_udms.setText(str(self.lightAttr["useDepthMapShadows"]))
            self.lineEdit_db.setText(str(self.lightAttr["dmapBias"]))
            self.lineEdit_ae.setText(str(self.lightAttr["aiExposure"]))
            self.lineEdit_ca.setText(str(self.lightAttr["coneAngle"]))
            self.lineEdit_pa.setText(str(self.lightAttr["penumbraAngle"]))
            self.lineEdit_do.setText(str(self.lightAttr["dropoff"]))
            self.lineEdit_ar.setText(str(self.lightAttr["aiRadius"]))

        elif pm.objectType(self.light_list[index])=="pointLight":
            loader = QLoader.QUiLoader()
            self.ui = loader.load(pointLight_ui)
            self.ui.setParent(self)

            self.lineEdit_type = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_type")
            self.lineEdit_udms = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_udms")
            self.lineEdit_db = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_db")
            self.lineEdit_ae = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_ae")
            self.lineEdit_ar = self.ui.findChild(QtWidgets.QLineEdit,"LineEdit_ar")

            self.lineEdit_type.setText(pm.objectType(self.light_list[index]))
            self.lineEdit_udms.setText(str(self.lightAttr["useDepthMapShadows"]))
            self.lineEdit_db.setText(str(self.lightAttr["dmapBias"]))
            self.lineEdit_ae.setText(str(self.lightAttr["aiExposure"]))
            self.lineEdit_ar.setText(str(self.lightAttr["aiRadius"]))

        elif pm.objectType(self.light_list[index])=="directionalLight":
            loader = QLoader.QUiLoader()
            self.ui = loader.load(directionalLight_ui)
            self.ui.setParent(self)

            self.lineEdit_type = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_type")
            self.lineEdit_udms = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_udms")
            self.lineEdit_db = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_db")
            self.lineEdit_ae = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_ae")
            self.lineEdit_aa = self.ui.findChild(QtWidgets.QLineEdit,"LineEdit_aa")

            self.lineEdit_type.setText(pm.objectType(self.light_list[index]))
            self.lineEdit_udms.setText(str(self.lightAttr["useDepthMapShadows"]))
            self.lineEdit_db.setText(str(self.lightAttr["dmapBias"]))
            self.lineEdit_ae.setText(str(self.lightAttr["aiExposure"]))
            self.lineEdit_aa.setText(str(self.lightAttr["aiAngle"]))

        elif pm.objectType(self.light_list[index])=="volumeLight":
            loader = QLoader.QUiLoader()
            self.ui = loader.load(volumeLight_ui)
            self.ui.setParent(self)

            self.lineEdit_type = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_type")
            self.lineEdit_udms = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_udms")
            self.lineEdit_db = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_db")
            self.lineEdit_ae = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_ae")
            self.lineEdit_ar = self.ui.findChild(QtWidgets.QLineEdit,"LineEdit_ar")

            self.lineEdit_type.setText(pm.objectType(self.light_list[index]))
            self.lineEdit_udms.setText(str(self.lightAttr["useDepthMapShadows"]))
            self.lineEdit_db.setText(str(self.lightAttr["dmapBias"]))
            self.lineEdit_ae.setText(str(self.lightAttr["aiExposure"]))
            self.lineEdit_ar.setText(str(self.lightAttr["aiRadius"]))

        #获取界面共有子控件
        self.lineEdit_tr_X = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_tr_X")
        self.lineEdit_tr_Y = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_tr_Y")
        self.lineEdit_tr_Z = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_tr_Z")
        self.lineEdit_rt_X = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_rt_X")
        self.lineEdit_rt_Y = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_rt_Y")
        self.lineEdit_rt_Z = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_rt_Z")
        self.lineEdit_sc_X = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_sc_X")
        self.lineEdit_sc_Y = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_sc_Y")
        self.lineEdit_sc_Z = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_sc_Z")
        self.lineEdit_sco_X = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_sco_X")
        self.lineEdit_sco_Y = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_sco_Y")
        self.lineEdit_sco_Z = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_sco_Z")
        self.lineEdit_int = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_int")
        self.lineEdit_urt = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_urt")
        self.lineEdit_vis = self.ui.findChild(QtWidgets.QLineEdit, "LineEdit_vis")

        self.setValue()

    def setValue(self):
        #设置控件的值

        self.lineEdit_tr_X.setText(str(self.lightAttr["translate"][0][0]))
        self.lineEdit_tr_Y.setText(str(self.lightAttr["translate"][0][1]))
        self.lineEdit_tr_Z.setText(str(self.lightAttr["translate"][0][2]))
        self.lineEdit_rt_X.setText(str(self.lightAttr["rotate"][0][0]))
        self.lineEdit_rt_Y.setText(str(self.lightAttr["rotate"][0][1]))
        self.lineEdit_rt_Z.setText(str(self.lightAttr["rotate"][0][2]))
        self.lineEdit_sc_X.setText(str(self.lightAttr["scale"][0][0]))
        self.lineEdit_sc_Y.setText(str(self.lightAttr["scale"][0][1]))
        self.lineEdit_sc_Z.setText(str(self.lightAttr["scale"][0][2]))
        self.lineEdit_sco_X.setText(str(self.lightAttr["shadColor"][0]))
        self.lineEdit_sco_Y.setText(str(self.lightAttr["shadColor"][1]))
        self.lineEdit_sco_Z.setText(str(self.lightAttr["shadColor"][2]))
        self.lineEdit_int.setText(str(self.lightAttr["intensity"]))
        self.lineEdit_vis.setText(str(self.lightAttr["visibility"]))
        self.lineEdit_urt.setText(str(self.lightAttr["useRayTraceShadows"]))

#创建窗口
win = LightManager()

win.show()

然后再写houdini里的脚本:

import json
import hou
#加载json文件
with open(r"C:\Users\Administrator\Desktop\sendLights\src\light_json.json") as js:
    data = json.load(js)

class Light():
    """ 灯光基类"""
    def __init__(self, key):

        self.obj = hou.node("obj")
        self.key = key

    #创建灯光
    def createLight(self):
        print self.key
        self.light = self.obj.createNode("hlight")
        self.light.setName(self.key)

    #设置灯光位置
    def setPostition(self):
        self.posX = self.light.parm("tx")
        self.posY = self.light.parm("ty")
        self.posZ = self.light.parm("tz")
        self.posX.set(data[self.key]["translate"][0][0])
        self.posY.set(data[self.key]["translate"][0][1])
        self.posZ.set(data[self.key]["translate"][0][2])

    #设置灯光旋转
    def setRotate(self):
        self.posX = self.light.parm("rx")
        self.posY = self.light.parm("ry")
        self.posZ = self.light.parm("rz")
        self.posX.set(data[self.key]["rotate"][0][0])
        self.posY.set(data[self.key]["rotate"][0][1])
        self.posZ.set(data[self.key]["rotate"][0][2])

    #设置投影
    def setShadow(self):

        self.st = self.light.parm("shadow_type")

        if data[self.key]["useDepthMapShadows"]:
            self.st.set(2)
            self.bias = self.light.parm("shadow_bias")
            self.bias.set(data[self.key]["dmapBias"])

        elif data[self.key]["useRayTraceShadows"]:
            self.st.set(1)

        self.scr = self.light.parm("shadow_colorr")
        self.scg = self.light.parm("shadow_colorg")
        self.scb = self.light.parm("shadow_colorb")
        self.scr.set(data[self.key]["shadColor"][0])
        self.scg.set(data[self.key]["shadColor"][1])
        self.scb.set(data[self.key]["shadColor"][2])

    #设置显示模式
    def setDisplay(self):

        self.tdis = self.light.parm("tdisplay")

        self.tdis.set(True)
        self.dis = self.light.parm("display")
        if data[self.key]["visibility"]:
            self.dis.set(1)
        else:
            self.dis.set(0)

    #设置扩散
    def setExposure(self):
        self.le = self.light.parm("light_exposure")
        self.le.set(data[self.key]["aiExposure"])

    #设置颜色属性
    def setColor(self):

        self.colX = self.light.parm("light_colorr")
        self.colY = self.light.parm("light_colorg")
        self.colZ = self.light.parm("light_colorb")
        self.colX.set(data[self.key]["color"][0][0])
        self.colY.set(data[self.key]["color"][0][1])
        self.colZ.set(data[self.key]["color"][0][2])

    #灯光强度
    def setIntensity(self):

        self.inte = self.light.parm("light_intensity")
        self.inte.set(data[self.key]["intensity"])

class AmbientLight(Light):
    """环境光类,继承自灯光基类"""
    def __init__(self,key):
        Light.__init__(self,key)

        self.createLight()
        self.setPostition()
        self.setRotate()
        self.setScale()
        self.setColor()
        self.setIntensity()
        self.setDisplay()

    #重写创建灯光
    def createLight(self):
        print self.key

        #创建节点,并且重命名
        self.light = self.obj.createNode("ambient")
        self.light.setName(self.key)
        self.file = self.light.children()[0]
        self.transform = self.light.createNode("xform")

        #连接节点
        self.transform.setFirstInput(self.file)

        #按下L键
        self.light.layoutChildren()

        #设置显示模式
        self.transform.setDisplayFlag(True)
        self.transform.setRenderFlag(True)

    #重写位置属性
    def setPostition(self):

        self.posX = self.transform.parm("tx")
        self.posY = self.transform.parm("ty")
        self.posZ = self.transform.parm("tz")
        self.posX.set(data[self.key]["translate"][0][0])
        self.posY.set(data[self.key]["translate"][0][1])
        self.posZ.set(data[self.key]["translate"][0][2])

    #重写旋转属性
    def setRotate(self):    

        self.rotX = self.transform.parm("rx")
        self.rotY = self.transform.parm("ry")
        self.rotZ = self.transform.parm("rz")
        self.rotX.set(data[self.key]["rotate"][0][0])
        self.rotY.set(data[self.key]["rotate"][0][1])
        self.rotZ.set(data[self.key]["rotate"][0][2])

    #设置大小属性
    def setScale(self):
        self.rotX = self.transform.parm("sx")
        self.rotY = self.transform.parm("sy")
        self.rotZ = self.transform.parm("sz")
        self.rotX.set(data[self.key]["scale"][0][0])
        self.rotY.set(data[self.key]["scale"][0][1])
        self.rotZ.set(data[self.key]["scale"][0][2])

    #重写显示属性
    def setDisplay(self):
        self.dis = self.light.parm("l_tdisplay")
        self.dis.set(True)
        self.disd = self.light.parm("l_display")
        if data[self.key]["visibility"]:
            self.disd.set(1)
        else:
            self.disd.set(0)

class DirectionalLight(Light):
    """平行光,继承自灯光基类"""
    def __init__(self,key):
        Light.__init__(self,key)

        self.createLight()
        self.setPostition()
        self.setRotate()
        self.setColor()
        self.setIntensity()
        self.setDisplay()
        self.setShadow()
        self.setExposure()

    #重写创建灯光
    def createLight(self):
        print self.key
        self.light = self.obj.createNode("hlight")
        self.light.setName(self.key)
        self.lightType = self.light.parm("light_type")
        self.lightType.set(7)        

    #scale aiAngle

class PointLight(Light):
    """点光源,继承自灯光基类"""
    def __init__(self,key):
        Light.__init__(self,key)

        self.createLight()
        self.setPostition()
        self.setRotate()
        self.setColor()
        self.setIntensity()
        self.setDisplay()
        self.setShadow()
        self.setExposure()       

    #scale aiRadius

class SpotLight(Light):
    """聚光灯,继承自灯光基类"""
    def __init__(self,key):
        Light.__init__(self,key)

        self.createLight()
        self.setPostition()
        self.setRotate()
        self.setColor()
        self.setIntensity()
        self.setDisplay()
        self.setShadow()
        self.setExposure()
        self.setAngle()
        self.setDropoff()
    #重写创建灯光
    def createLight(self):
        print self.key
        self.light = self.obj.createNode("hlight")
        self.light.setName(self.key)
        #设置为聚光灯
        self.coneenable = self.light.parm("coneenable")
        self.coneenable.set(True)

    #设置灯光角度
    def setAngle(self):
        self.angle = self.light.parm("coneangle")
        self.angle.set(data[self.key]["coneAngle"])

    #设置灯光衰减
    def setDropoff(self):
        self.coneroll = self.light.parm("coneroll")
        self.coneroll.set(data[self.key]["dropoff"])

    #scale aiRadius penumbraAngle

class AreaLight(Light):
    """平面光,继承自灯光基类"""
    def __init__(self,key):
        Light.__init__(self,key)

        self.createLight()
        self.setPostition()
        self.setRotate()
        self.setColor()
        self.setIntensity()
        self.setDisplay()
        self.setShadow()
        self.setExposure()
        self.setSize()

    #重写创建灯光
    def createLight(self):
        print self.key
        self.light = self.obj.createNode("hlight")
        self.light.setName(self.key)
        #设置为平面光
        self.lightType = self.light.parm("light_type")
        self.lightType.set(2)  

    #设置灯光角度
    def setAngle(self):
        self.angle = self.light.parm("coneangle")
        self.angle.set(data[self.key]["coneAngle"])

    #设置灯光衰减
    def setDropoff(self):
        self.coneroll = self.light.parm("coneroll")
        self.coneroll.set(data[self.key]["dropoff"])

    #设置大小
    def setSize(self):
        self.areasize1 = self.light.parm("areasize1")
        self.areasize1.set(data[self.key]["scale"][0][0])
        self.areasize2 = self.light.parm("areasize2")
        self.areasize2.set(data[self.key]["scale"][0][1])

    # aiSpread aiRoundness aiSoftEdge

class VolumeLight(Light):
    """体积光,继承自灯光基类"""
    def __init__(self,key):
        Light.__init__(self,key)

        self.createLight()
        self.setPostition()
        self.setRotate()
        self.setColor()
        self.setIntensity()
        self.setDisplay()
        self.setShadow()
        self.setExposure()       

    #重写创建灯光
    def createLight(self):
        print self.key
        self.geo = self.obj.createNode("geo")
        self.geo.setName(self.key + "_sphere")

        self.light = self.obj.createNode("hlight")
        self.lightType = self.light.parm("light_type")
        self.lightType.set(6)

        self.areageometry = self.light.parm("areageometry")
        self.areageometry.set("./" + self.key + "_sphere")

        self.light.setName(self.key)

    # airadius 

def createLight():
    #遍历字典,创建灯光
    for key in data:
        #根据灯光类型创建灯光
        if data[key]["lightType"]=="ambientLight":
            light = AmbientLight(key)
        elif data[key]["lightType"] == "directionalLight":
            light = DirectionalLight(key)
        elif data[key]["lightType"] == "pointLight":
            light = PointLight(key)
        elif data[key]["lightType"] == "spotLight":
            light = SpotLight(key)
        elif data[key]["lightType"] == "areaLight":
            light = AreaLight(key)
        elif data[key]["lightType"] == "volumeLight":
            light = VolumeLight(key)

    obj = hou.node("obj")
    #排列节点
    obj.layoutChildren()

最后在maya与houdini里面加载这些脚本即可,至于怎么加载,可以参考我另外一篇博文maya灯光导入houdini插件开发——技术文档

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章