# cg_nukeIOPlugin # Initial code generated by XSI SDK Wizard # Executed Wed Mar 26 14:33:39 UTC+1100 2008 by 3D import win32com.client from win32com.client import constants import math xsi = Application null = None false = 0 true = 1 changeLogStr = "v1.1 - 31 March 2008 - Added chan import code\r\n" changeLogStr += "v1.0 - 26 March 2008 - initial release\r\n" changeLogStr += "Some code from Gmeyer's XSI to Nuke Chan exporter script \r\n" changeLogStr += "enjoy!\r\nchrisg\r\n" class cfg: # generic global variable container pass def log(msg, inSeverity = 8 ): xsi.LogMessage(msg, inSeverity) def findObj(inObjName): oColl = XSIFactory.CreateObject( "XSI.Collection" ) oColl.Items = inObjName if oColl.count > 0: return oColl(0) else: return false def makeExportPset(): if findObj("nukeExport"): oCP = findObj("nukeExport") else: cPass = xsi.GetCurrentPass() cFrameIn = cPass.FrameStart.Value cFrameOut = cPass.FrameEnd.Value cFrameWidth = cPass.ImageWidth.Value cFrameHeight = cPass.ImageHeight.Value if xsi.Selection.Count == 0: cCam = cPass.Camera.Value else: cCam = xsi.Selection(0) oCP = xsi.ActiveSceneRoot.AddProperty("CustomProperty",false, "nukeExport") oCP.AddParameter2("ExportFile",constants.siString,"",null,null,null,null,constants.siClassifUnknown,constants.siPersistable) oCP.AddParameter2("objectToExport",constants.siString,cCam,null,null,0,100,constants.siClassifUnknown,constants.siPersistable) oCP.AddParameter2("frameIn",constants.siInt2,cFrameIn,null,null,null,null,constants.siClassifUnknown,constants.siPersistable) oCP.AddParameter2("frameOut",constants.siInt2,cFrameOut,null,null,null,null,constants.siClassifUnknown,constants.siPersistable) oCP.AddParameter2("changeLog",constants.siString,changeLogStr,null,null,null,null,constants.siClassifUnknown,constants.siPersistable) oLayout = oCP.PPGLayout oLayout.Clear() oLayout.AddTab("General") oFilePick = oLayout.AddItem("ExportFile", "FileName", constants.siControlFilePath) oFilePick.SetAttribute(constants.siUIFileFilter, "Chan Files (*.chan)|*.chan|All Files (*.*)|*.*||") oLayout.AddItem("objectToExport", "Export this object") oLayout.AddGroup("Frame Range") oLayout.AddRow() rtn = oLayout.AddItem("frameIn", "Frame In") rtn.SetAttribute(constants.siUINoSlider, true) rtn = oLayout.AddItem("frameOut", "Frame Out") rtn.SetAttribute(constants.siUINoSlider, true) oLayout.EndRow() oLayout.EndGroup() oLayout.AddStaticText("Check the script log for the correct aperture settings\nfor your nuke camera.") oLayout.AddTab("Changelog") oLayout.AddString("changeLog", "Change Log", true) return oCP def Export_NukeCam(Camera, File, Start, End): log("----- Nuke .chan export -----") oCam = findObj(Camera) if oCam.Type == "camera": aperture = xsi.GetValue(oCam.FullName + ".camera.projplaneheight") * 25.4 haperture = xsi.GetValue(oCam.FullName + ".camera.projplanewidth") * 25.4 log("Set your nuke cam to the following aperture (mm):") log("%f x %f" % (haperture, aperture)) focal = xsi.GetValue(oCam.FullName + ".camera.projplanedist") fovVal = 2 * math.atan( ( 0.5 * (aperture) ) / focal ) fovVal = (fovVal /(4 * math.atan(1))*180) else: fovVal = 0 # open file f = open(File, 'w') oPbar = XSIUIToolkit.progressbar oPbar.Maximum = (End+1) oPbar.Caption = "Exporting Stuff..." oPbar.Visible = true oPbar.CancelEnabled = false for i in range(Start, End+1): oTrans = oCam.Kinematics.Global.GetTransform2(i) Posx = oTrans.PosX Posy = oTrans.PosY Posz = oTrans.PosZ Rotx = oTrans.RotX Roty = oTrans.RotY Rotz = oTrans.RotZ Fr = "%i\t" % (i) Pos = "%f \t %f \t %f \t" % (Posx, Posy, Posz) Rot = "%f \t %f \t %f\t" % (Rotx, Roty, Rotz) Fov = "%f" % (fovVal) # Information to write if oCam.Type == "camera": Info = Fr + Pos + Rot + Fov else: Info = Fr + Pos + Rot # Write Info to file and enter a new line f.write (Info + "\n") oPbar.Increment() oPbar.Visible = false # close file f.close # Messages log( "The chan file is located at: " + File) log("----- done -----") def XSILoadPlugin( in_reg ): in_reg.Author = "chrisg" in_reg.Name = "cg_nukeIOPlugin" in_reg.Email = "chrisg@chris-g.net" in_reg.URL = "http://www.chris-g.net" in_reg.Major = 1 in_reg.Minor = 0 in_reg.RegisterCommand("cg_nukeExport","cg_nukeExport") in_reg.RegisterCommand("cg_nukeImport","cg_nukeImport") in_reg.RegisterMenu(constants.siMenuTbAnimateToolsExportID,"cg_nukeExport_Menu",false,false) in_reg.RegisterMenu(constants.siMenuMainFileExportID,"cg_nukeExport_Menu",false,false) in_reg.RegisterMenu(constants.siMenuMainFileImportID,"cg_nukeImport_Menu",false,false) #RegistrationInsertionPoint - do not remove this line return true def XSIUnloadPlugin( in_reg ): strPluginName = in_reg.Name return true def importChan(inObj, inChanFile): try: f = open(inChanFile, 'r') except: log("Can't open channel file! Borking.") return # get the line count count = 0 for line in open(inChanFile, 'r'): count = count + 1 fileList = f.readlines() oPbar = XSIUIToolkit.progressbar oPbar.Maximum = count oPbar.Caption = "Importing .chan file..." oPbar.Visible = true oPbar.CancelEnabled = false Application.SetValue("preferences.scripting.cmdlog", false, "") isCamera = false for thisLine in fileList: arr = thisLine.split("\t") if len(arr) < 7: log("Malformed .chan file? Not enough columns. Borking.") return fr = int(arr[0]) posx = float(arr[1]) posy = float(arr[2]) posz = float(arr[3]) rotx = float(arr[4]) roty = float(arr[5]) rotz = float(arr[6]) if len(arr) == 8: try: fov = float(arr[7]) except: fov = 0 else: fov = 0 if fov != 0: if inObj.Type == "camera": isCamera = true xsi.SaveKey(inObj.fullName + ".camera.fov", fr, fov) xsi.SaveKey(inObj.fullName + ".kine.global.posx", fr, posx) xsi.SaveKey(inObj.fullName + ".kine.global.posy", fr, posy) xsi.SaveKey(inObj.fullName + ".kine.global.posz", fr, posz) xsi.SaveKey(inObj.fullName + ".kine.global.rotx", fr, rotx) xsi.SaveKey(inObj.fullName + ".kine.global.roty", fr, roty) xsi.SaveKey(inObj.fullName + ".kine.global.rotz", fr, rotz) #xsi.LogMessage("Frame: %s / posx: %s" % (fr, posx)) oPbar.Increment() f.close() oPbar.Visible = false Application.SetValue("preferences.scripting.cmdlog", true, "") if isCamera == true: log("Don't forget to apply your Nuke camera back settings!") xsi.InspectObj(inObj) def makeImportPset(): if findObj("nukeImport"): oCP = findObj("nukeImport") else: oCP = xsi.ActiveSceneRoot.AddProperty("CustomProperty",false, "nukeImport") oCP.AddParameter2("ImportFile",constants.siString,"",null,null,null,null,constants.siClassifUnknown,constants.siPersistable) oCP.AddParameter2("changeLog",constants.siString,changeLogStr,null,null,null,null,constants.siClassifUnknown,constants.siPersistable) oLayout = oCP.PPGLayout oLayout.Clear() oLayout.AddTab("General") oFilePick = oLayout.AddItem("ImportFile", "FileName", constants.siControlFilePath) oFilePick.SetAttribute(constants.siUIFileFilter, "Chan Files (*.chan)|*.chan|All Files (*.*)|*.*||") oFilePick.SetAttribute(constants.siUIFileMustExist, true) oFilePick.SetAttribute(constants.siUIOpenFile, true) oLayout.AddStaticText("If importing a camera chan file, check your camera back settings.") oLayout.AddTab("Changelog") oLayout.AddString("changeLog", "Change Log", true) return oCP def cg_nukeImport_Init( in_ctxt ): oCmd = in_ctxt.Source oCmd.Description = "Imports cameras or objects from nuke .chan format" oCmd.ReturnValue = true return true def cg_nukeImport_Execute( ): try: opts = makeImportPset() xsi.inspectobj( opts , "", "Nuke .chan Import", constants.siModal ) except: log("User cancelled") return cfg.ImportFile = opts.ImportFile.Value importChan(xsi.Selection(0), cfg.ImportFile) return true def cg_nukeImport_Menu_Init( in_ctxt ): oMenu = in_ctxt.Source oMenu.AddCommandItem("Nuke Import","cg_nukeImport") return true def cg_nukeExport_Init( in_ctxt ): oCmd = in_ctxt.Source oCmd.Description = "Exports cameras or objects to nuke .chan format" oCmd.ReturnValue = true return true def cg_nukeExport_Execute( ): try: opts = makeExportPset() xsi.inspectobj( opts , "", "Nuke .chan Export", constants.siModal ) except: log("User cancelled") return cfg.ExportFile = opts.ExportFile.Value cfg.frameIn = opts.frameIn.Value cfg.frameOut = opts.frameOut.Value cfg.frameCount = (cfg.frameOut - cfg.frameIn) + 1 cfg.objectToExport = opts.objectToExport.Value sceneItemCount = 0 if cfg.ExportFile != "": Export_NukeCam(cfg.objectToExport, cfg.ExportFile, cfg.frameIn , cfg.frameOut ) else: log("No chan file specified") return true def cg_nukeExport_Menu_Init( in_ctxt ): oMenu = in_ctxt.Source oMenu.AddCommandItem("Nuke Export","cg_nukeExport") return true