def save_file(log, fname=None): if fname is None: fname = "temp" file_path = fname if HOST == STANDALONE: from tentaculo.api import standalone standalone.message_function("save_file", [file_path]) elif HOST == MAYA or HOST == MAYA2: mc.file(rename=file_path) file_path = mc.file(save=True, type="mayaAscii") elif HOST == NUKE: nuke.scriptSaveAs(file_path, overwrite=1) elif HOST == HOUDINI: hou.hipFile.setName(file_path) hou.hipFile.save() elif HOST == MAX: MaxPlus.FileManager.Save(file_path) elif HOST == C4D: doc = c4d.documents.GetActiveDocument() if doc is not None: c4d.documents.SaveDocument(doc, str(file_path), c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT) elif HOST == BLENDER: bpy.ops.wm.save_mainfile(filepath=file_path) elif HOST == KATANA: KatanaFile.Save(file_path) return file_path
def file_name(log): file_name = None if HOST == STANDALONE: from tentaculo.api import standalone file_name = standalone.message_function("file_name")[0] elif HOST == MAYA or HOST == MAYA2: file_name = os.path.normpath(mc.file(query=True, sceneName=True)) elif HOST == NUKE: file_name = os.path.normpath(nuke.root().name()) elif HOST == HOUDINI: file_name = os.path.normpath(hou.hipFile.name()) elif HOST == MAX: file_name = MaxPlus.FileManager.GetFileNameAndPath() elif HOST == C4D: doc = c4d.documents.GetActiveDocument() if doc is not None: file_name = os.path.join(doc.GetDocumentPath(), doc.GetDocumentName()) elif HOST == BLENDER: if bpy.data.is_saved: file_name = bpy.data.filepath elif HOST == KATANA: file_name = NodegraphAPI.NodegraphGlobals.GetProjectAssetID() return file_name
def save_query(log): cancel = False if HOST == STANDALONE: from tentaculo.api import standalone cancel = standalone.message_function("save_query")[0] elif HOST == MAYA or HOST == MAYA2: file_name = mc.file(query=True, sceneName=True) need_save = mc.file(query=True, modified=True) if len(file_name) > 0 and need_save: ret = mc.confirmDialog(title='Closing file', message='Save current file?', messageAlign='center', button=['Yes', 'No', 'Cancel'], defaultButton='Yes', cancelButton='Cancel', dismissString='Cancel') if ret == "Yes": mc.file(save=True, type="mayaAscii") elif ret == "Cancel": cancel = True elif HOST == NUKE: cancel = not nuke.scriptClose() elif HOST == HOUDINI: need_save = hou.hipFile.hasUnsavedChanges() if need_save: hou.hipFile.clear() cancel = hou.hipFile.hasUnsavedChanges() elif HOST == MAX: cancel = not MaxPlus.FileManager.CheckForSave() elif HOST == C4D: doc = c4d.documents.GetActiveDocument() if doc is not None and doc.GetChanged(): cancel = not c4d.documents.SaveDocument( doc, doc.GetDocumentPath() + "/" + doc.GetDocumentName(), c4d.SAVEDOCUMENTFLAGS_DIALOGSALLOWED, c4d.FORMAT_C4DEXPORT) elif HOST == BLENDER: if bpy.data.is_dirty: bpy.ops.wm.save_mainfile(check_existing=True) cancel = bpy.data.is_dirty elif HOST == KATANA: if KatanaFile.IsFileDirty(): mb = UI4.App.Application.QtGui.QMessageBox(app_window()) mb.setText("Closing file") mb.setInformativeText("Save current file?") mb.setStandardButtons( UI4.App.Application.QtGui.QMessageBox.Yes | UI4.App.Application.QtGui.QMessageBox.No | UI4.App.Application.QtGui.QMessageBox.Cancel) ret = mb.exec_() if ret == UI4.App.Application.QtGui.QMessageBox.Yes: KatanaFile.Save( NodegraphAPI.NodegraphGlobals.GetProjectAssetID()) cancel = ret == UI4.App.Application.QtGui.QMessageBox.Cancel return not cancel
def close_file(): if HOST == STANDALONE: from tentaculo.api import standalone standalone.message_function("close_file") elif HOST == MAYA or HOST == MAYA2: mc.file(new=True, force=True) elif HOST == NUKE: nuke.scriptClear() elif HOST == HOUDINI: hou.hipFile.clear(suppress_save_prompt=True) elif HOST == MAX: MaxPlus.FileManager.Reset(noPrompt=True) elif HOST == C4D: c4d.documents.CloseAllDocuments() elif HOST == BLENDER: bpy.ops.wm.read_homefile() elif HOST == KATANA: KatanaFile.New() return True
def create_file(log, filepath, origin_path=None): dir = os.path.dirname(filepath) if not os.path.exists(dir): raise Exception('{0} does not exist'.format(dir)) if origin_path is None: origin_path = filepath if HOST == STANDALONE: from tentaculo.api import standalone standalone.message_function("create_file", [filepath]) elif HOST == MAYA or HOST == MAYA2: check_workspace_maya(log, origin_path) mc.file(new=True, force=True) mc.file(rename=filepath) mc.file(save=True, type="mayaAscii") elif HOST == NUKE: nuke.scriptClear() nuke.scriptSaveAs(filepath, overwrite=1) elif HOST == HOUDINI: hou.hipFile.clear(suppress_save_prompt=True) hou.hipFile.save(filepath) elif HOST == MAX: MaxPlus.FileManager.Reset(noPrompt=True) MaxPlus.FileManager.Save(filepath) elif HOST == C4D: c4d.documents.CloseAllDocuments() newDoc = c4d.documents.BaseDocument() c4d.documents.InsertBaseDocument(newDoc) c4d.documents.SaveDocument(newDoc, str(filepath), c4d.SAVEDOCUMENTFLAGS_0, c4d.FORMAT_C4DEXPORT) c4d.documents.LoadFile(str(filepath)) elif HOST == BLENDER: bpy.ops.wm.read_homefile() bpy.ops.wm.save_mainfile(filepath=filepath, check_existing=False) elif HOST == KATANA: KatanaFile.New() KatanaFile.Save(filepath) return filepath
def open_file(log, filepath, origin_path=None): if not os.path.exists(filepath): raise Exception('{0} does not exist'.format(filepath)) if origin_path is None: origin_path = filepath if HOST == STANDALONE: from tentaculo.api import standalone standalone.message_function("open_file", [filepath]) elif HOST == MAYA or HOST == MAYA2: check_workspace_maya(log, origin_path) mc.file(new=True, force=True) mc.file(filepath, open=True, force=True) log.debug('Maya openned %s', filepath) elif HOST == NUKE: nuke.scriptClear() nuke.Root().setModified(False) nuke.scriptOpen(filepath) log.debug("Nuke opened %s", filepath) elif HOST == HOUDINI: hou.hipFile.load(filepath.replace( '\\', '/')) #, suppress_save_prompt = True) log.debug("Houdini opened %s", filepath) elif HOST == MAX: MaxPlus.FileManager.Open(filepath) log.debug("Max opened %s", filepath) elif HOST == C4D: c4d.documents.LoadFile(str(filepath.replace('\\', '/'))) log.debug("Cinema 4D opened %s", filepath) elif HOST == BLENDER: bpy.ops.wm.open_mainfile(filepath=filepath) log.debug("Blender opened %s", filepath) elif HOST == KATANA: KatanaFile.Load(filepath) return filepath
def import_file(log, fname=None, as_reference=False): if fname is None or not os.path.exists(fname): log.error("Invalid file path given for import: %s", fname) return False if HOST == STANDALONE: from tentaculo.api import standalone standalone.message_function("import_file", [fname, as_reference]) elif HOST == MAYA or HOST == MAYA2: log.debug("Import: Maya %s", fname) if as_reference: file_path = mc.file(fname, reference=True, mergeNamespacesOnClash=True) mc.file(file_path, selectAll=True) else: before = mc.ls() mc.file(fname, i=True, mergeNamespacesOnClash=True) after = mc.ls() selection = [x for x in after if x not in before] mc.select(selection) elif HOST == NUKE: log.debug("Import: Nuke %s", fname) if as_reference: nuke.nodes.Read(file=fname) elif HOST == HOUDINI: log.debug("Import: Houdini %s", fname) if as_reference: pass else: hou.hipFile.merge(fname) elif HOST == MAX: log.debug("Import: 3ds Max %s", fname) if as_reference: pass else: MaxPlus.FileManager.Merge(fname, True, True) elif HOST == C4D: log.debug("Import: C4D %s", fname) doc = c4d.documents.GetActiveDocument() if doc is not None: if as_reference: pass else: c4d.documents.MergeDocument(doc, fname) elif HOST == BLENDER: log.debug("Import: Blender %s", fname) with bpy.data.libraries.load(filepath=fname, link=as_reference) as (data_from, data_to): #data_to.objects = data_from.objects for attr in dir(data_to): setattr(data_to, attr, getattr(data_from, attr)) for obj in data_to.objects: if obj is not None: bpy.context.scene.objects.link(obj) obj.select = True elif HOST == KATANA: if as_reference: pass else: KatanaFile.Import(fname) return True
def take(self): if capp.HOST == capp.MAYA or capp.HOST == capp.MAYA2: view = omui.M3dView.active3dView() view.setColorMask(1, 1, 1, 0) width = view.portWidth() height = view.portHeight() image = om.MImage() if view.getRendererName() == view.kViewport2Renderer: image.create(view.portWidth(), view.portHeight(), 4, om.MImage.kFloat) view.readColorBuffer(image) image.convertPixelFormat(om.MImage.kByte) else: view.readColorBuffer(image) ptr = ctypes.cast(image.pixels().__long__(), ctypes.POINTER(ctypes.c_char)) ptrAsStr = ctypes.string_at(ptr, width * height * 4) image = QImage(ptrAsStr, width, height, QImage.Format_RGB32) image = image.mirrored(horizontal=False, vertical=True) self.img = QPixmap.fromImage(image) self.set(self.save(self.img)) elif capp.HOST == capp.NUKE: viewer = nuke.activeViewer() viewer_input = nuke.ViewerWindow.activeInput(viewer) viewer_node = nuke.activeViewer().node() if viewer_input is not None and viewer_node is not None: inputNode = nuke.Node.input(viewer_node, viewer_input) out_path = os.path.join(utils.tempdir(), "cerebro_screen.png").replace( '\\', '/') node_write = nuke.nodes.Write(file=out_path, name="cerebro_screen_node", file_type="png") node_write.setInput(0, inputNode) cur_frame = int(nuke.knob("frame")) nuke.execute(node_write.name(), cur_frame, cur_frame) self.set(out_path) # Cleanup for n in [node_write]: nuke.delete(n) elif capp.HOST == capp.HOUDINI: cur_desktop = hou.ui.curDesktop() frame = hou.frame() desktop = cur_desktop.name() panetab = cur_desktop.paneTabOfType( hou.paneTabType.SceneViewer).name() persp = cur_desktop.paneTabOfType( hou.paneTabType.SceneViewer).curViewport().name() camera_path = desktop + '.' + panetab + ".world." + persp out_path = os.path.join(utils.tempdir(), "cerebro_screen.jpg").replace('\\', '/') hou.hscript("viewwrite -r 800 600 -f {0} {1} {2} '{3}'".format( frame, frame, camera_path, out_path)) self.set(out_path) elif capp.HOST == capp.MAX: view = MaxPlus.ViewportManager.GetActiveViewport() bm = MaxPlus.Factory.CreateBitmap() storage = MaxPlus.Factory.CreateStorage(7) info = storage.GetBitmapInfo() bm.SetStorage(storage) bm.DeleteStorage() res = view.GetDIB(info, bm) if res: #bm.Display() out_path = os.path.join(utils.tempdir(), "cerebro_screen.jpg") info.SetName(out_path) bm.OpenOutput(info) bm.Write(info) bm.Close(info) self.set(out_path) elif capp.HOST == capp.C4D: xres, yres = 800, 600 doc = c4d.documents.GetActiveDocument() rd = c4d.documents.RenderData() rd[c4d.RDATA_XRES] = xres rd[c4d.RDATA_YRES] = yres rd[c4d.RDATA_RENDERENGINE] = c4d.RDATA_RENDERENGINE_PREVIEWHARDWARE bmp = c4d.bitmaps.BaseBitmap() if bmp.Init(xres, yres) == c4d.IMAGERESULT_OK: res = c4d.documents.RenderDocument(doc, rd.GetData(), bmp, c4d.RENDERFLAGS_EXTERNAL) if res == c4d.RENDERRESULT_OK: out_path = os.path.join(utils.tempdir(), "cerebro_screen.png") res = bmp.Save(out_path, c4d.FILTER_PNG) if res == c4d.IMAGERESULT_OK: self.set(out_path) elif capp.HOST == capp.BLENDER: out_path = os.path.join(utils.tempdir(), "cerebro_screen.png") for window in bpy.context.window_manager.windows: screen = window.screen for area in screen.areas: if area.type == 'VIEW_3D': for space in area.spaces: if space.type == 'VIEW_3D': for region in area.regions: if region.type == 'WINDOW': L_altBpyCtx = { # defining alternative context (allowing modifications without altering real one) 'area': area # our 3D View (first found) , 'blend_data': bpy.context. blend_data # just to suppress PyContext warning, doesn't seem to have any effect #, 'edit_text' : bpy.context.edit_text # just to suppress PyContext warning, doesn't seem to have any effect , 'region': None # just to suppress PyContext warning, doesn't seem to have any effect , 'scene': window.scene if utils.PY37 else screen.scene, 'space': space, 'screen': window.screen, 'window': window # current window, could also copy context } bpy.ops.screen.screenshot( L_altBpyCtx, filepath=out_path, check_existing=False, full=False) break #XXX: limit to the window of the 3D View break #XXX: limit to the corresponding space (3D View) break #XXX: limit to the first 3D View (area) self.set(out_path) elif capp.HOST == capp.STANDALONE: out_path = os.path.join(utils.tempdir(), "standalone_screen.png") if standalone.message_function("screenshot", [out_path])[0]: self.set(out_path) elif capp.HOST == capp.KATANA: out_path = os.path.join(utils.tempdir(), "cerebro_screen.png") wnd = capp.app_window() pix = QPixmap.grabWindow(wnd.winId()) pix.save(out_path) self.set(out_path) return self.img