def save_as(self, path, force=False, verbose=0): """Save this pixmap at the given path. Args: path (str): path to save at force (bool): force overwrite with no confirmation verbose (int): print process data """ from psyhive import qt assert self.width() and self.height() _file = File(path) _fmt = {}.get(_file.extn, _file.extn.upper()) lprint("SAVING", path, _fmt, verbose=verbose) if _file.exists(): if not force: _result = qt.yes_no_cancel('Overwrite existing image?\n\n' + path) if _result == 'No': return os.remove(_file.path) test_path(_file.dir) self.save(abs_path(path, win=os.name == 'nt'), format=_fmt, quality=100) assert _file.exists()
def export_hsl_fbx_from_cur_scene(fbx, force=False): """Export HSL format fbx from the current scene. This uses the MocapTools library. Args: fbx (str): path to export to force (bool): overwrite existing files without confirmation """ cmds.loadPlugin('fbxmaya', quiet=True) install_mocap_tools() from MocapTools.Scripts import PsyopMocapTools _fbx = File(fbx) if _fbx.exists(): _fbx.delete(wording='Overwrite', force=force) _tmp_fbx = File('{}/MocapTools/Anim/Export/{}_SK_Tier1_Male.fbx'.format( KEALEYE_TOOLS_ROOT, File(host.cur_scene()).basename)) print ' - TMP FBX', _tmp_fbx.path _setup = PsyopMocapTools.mocapSetupTools() _tmp_fbx.delete(force=True) assert not _tmp_fbx.exists() _setup.exportAnim(PsyopMocapTools.config.animFBX) assert _tmp_fbx.exists() # Move from tmp dir _fbx.test_dir() shutil.move(_tmp_fbx.path, _fbx.path) print ' - SAVED FBX', nice_size(_fbx.path), _fbx.path
def render(file_, camera=None, layer='defaultRenderLayer', col_mgt=True, force=False, verbose=0): """Render the current scene. Args: file_ (str): path to save rendered image camera (str): camera to render through layer (str): layer to render col_mgt (bool): apply colour management force (bool): replace existing without confirmation verbose (int): print process data """ from maya_psyhive import open_maya as hom cmds.loadPlugin('mtoa', quiet=True) from mtoa.cmds import arnoldRender _cam = camera if not _cam: _cam = hom.get_active_cam() # Prepare output path _file = File(get_path(file_)) _file.test_dir() _file.delete(force=force, wording='Replace') # Prepare arnold cmds.setAttr("defaultArnoldRenderOptions.abortOnError", False) cmds.setAttr("defaultArnoldDriver.colorManagement", int(col_mgt)) cmds.setAttr("defaultArnoldDriver.mergeAOVs", True) _extn = {'jpg': 'jpeg'}.get(_file.extn, _file.extn) cmds.setAttr('defaultArnoldDriver.aiTranslator', _extn, type='string') cmds.setAttr('defaultArnoldDriver.prefix', "{}/{}".format(_file.dir, _file.basename), type='string') cmds.setAttr("defaultRenderGlobals.animation", False) # Execute renders assert not _file.exists() arnoldRender.arnoldRender(640, 640, True, True, _cam, ' -layer ' + layer) if not _file.exists(): _tmp_file = File('{}/{}_1.{}'.format(_file.dir, _file.basename, _file.extn)) print ' - TMP FILE', _tmp_file.path assert _tmp_file.exists() _tmp_file.move_to(_file) assert _file.exists() lprint('RENDERED IMAGE', _file.path, verbose=verbose)
def _save_fbx(file_, force=False): """Save fbx file. Args: file_ (str): fbx path force (bool): replace without confirmation """ _file = File(get_path(file_)) _file.delete(wording='Replace', force=force) for _mel in [ 'FBXExportUpAxis z', 'FBXExportFileVersion -v FBX201800', 'FBXExportSmoothingGroups -v true', 'FBXExportSmoothMesh -v true', 'FBXExportTangents -v true', 'FBXExportSkins -v true', 'FBXExportShapes -v true', 'FBXExportEmbeddedTextures -v false', 'FBXExportApplyConstantKeyReducer -v true', 'FBXExportSplitAnimationIntoTakes -c', 'FBXExport -f "{}"'.format(_file.path), ]: mel.eval(_mel) dprint('Wrote file', _file.nice_size(), _file.path) assert _file.exists()
def create_ref(file_, namespace, class_=None, force=False): """Create a reference. Args: file_ (str): path to reference namespace (str): reference namespace class_ (type): override FileRef class force (bool): force replace any existing ref Returns: (FileRef): reference """ from psyhive import qt from psyhive import host _file = File(abs_path(file_)) if not _file.exists(): raise OSError("File does not exist: " + _file.path) _class = class_ or FileRef _rng = host.t_range() if _file.extn == 'abc': cmds.loadPlugin('AbcImport', quiet=True) elif _file.extn.lower() == 'fbx': cmds.loadPlugin('fbxmaya', quiet=True) # Test for existing cmds.namespace(set=":") if cmds.namespace(exists=namespace): _ref = find_ref(namespace, catch=True) if _ref: if not force: qt.ok_cancel( 'Replace existing {} reference?'.format(namespace)) _ref.remove(force=True) else: del_namespace(namespace, force=force) # Create the reference _cur_refs = set(cmds.ls(type='reference')) _kwargs = { 'reference': True, 'namespace': namespace, 'options': "v=0;p=17", 'ignoreVersion': True } cmds.file(_file.abs_path(), **_kwargs) # Find new reference node _ref = get_single(set(cmds.ls(type='reference')).difference(_cur_refs)) # Fbx ref seems to update timeline (?) if host.t_range() != _rng: host.set_range(*_rng) return _class(_ref)
def test_set_writable(self): _test = File('{}/test.txt'.format(_TEST_DIR)) print "TEST FILE", _test if _test.exists(): _test.set_writable(True) _test.delete(force=True) _test.touch() assert _test.is_writable() print _test.set_writable(False) assert not _test.is_writable() _test.set_writable(True) _test.delete(force=True)
def test_move_to(self): _file_a = File(abs_path( '{}/A/file.text'.format(tempfile.gettempdir()))) _file_b = File(abs_path( '{}/B/file.text'.format(tempfile.gettempdir()))) _file_a.touch() _dir = _file_a.parent() print _dir _file_a.parent().move_to(_file_b.parent()) assert _file_b.exists() _file_b.parent().delete(force=True) assert not _file_b.parent().exists()
def test_aistandin(self): from maya_psyhive.tank_support import ts_aistandin set_dev_mode(False) _abc = (_DEV_PROJ.path + '/sequences/dev/dev9999/' 'animation/output/animcache/test_archer/v004/alembic/' 'dev9999_test_archer_v004.abc') _standin = File(_DEV_PROJ.path + '/assets/3D/character/archer/' 'shade/output/shadegeo/shade_main/v092/aistandin/' 'archer_shade_main_v092.ma') # Test build standin output (for Publish Tool) _standin.delete(force=True) assert not _standin.exists() tank_support.build_aistandin_output(output=_standin.path) assert _standin.exists() # Test apply abc to standin output (for Asset Manager) _shade = ref.obtain_ref(_standin.path, namespace='ais_test') tank_support.apply_abc_to_shade_aistandin(namespace=_shade.namespace, abc=_abc) assert _shade.get_node('AISShape').plug('dso').get_val() == _abc # Test build standin from shade (for clash holiday) _shade_path = ( _DEV_PROJ.path + '/assets/3D/character/archer/shade/' 'output/shadegeo/shade_main/v092/maya/archer_shade_main_v092.mb') _shade = ref.obtain_ref(namespace='archer_SHD', file_=_shade_path) _standin = tank_support.build_aistandin_from_shade(archive=_abc, shade=_shade, deferred=False) assert _standin.shp.plug('dso').get_val() == _abc # Test read sg range _abc = (_DEV_PROJ.path + '/sequences/dev/dev0000/' 'animation/output/animcache/aadvark_archer1/v039/alembic/' 'dev0000_aadvark_archer1_v039.abc') assert ts_aistandin._get_abc_range_from_sg(_abc) == (1005, 1015)
def _find_ref_issues(ref_): """Find any issues with the given reference. Args: ref_ (FileRef): reference to check Returns: (str list): issues with reference """ _file = File(host.cur_scene()) _issues = [] # Check namespace _issues += _find_ref_namespace_issues(ref_) if not ref_.namespace: return _issues # Check top node _top_node_issues, _junk = _find_ref_top_node_issues(ref_) _issues += _top_node_issues if _junk: return _issues # Check ref file path _ref_file = File(ref_.path) print ' - FILE', _ref_file.path _local_file = File('{}/{}'.format(_file.dir, _ref_file.filename)) if ingest.is_vendor_file(_ref_file): _vendor_file = ingest.VendorFile(_ref_file) print ' - VENDOR FILE' if _vendor_file.step != 'rig': _issues.append("Reference {} is not a rig".format(ref_.namespace)) elif ingest.is_psy_asset(_ref_file): _psy_file = ingest.PsyAsset(_ref_file) print ' - PSYOP FILE' if _psy_file.step != 'rig': _issues.append("Psyop reference {} is not a rig".format( ref_.namespace)) elif not _local_file.exists(): print ' - OFF-PIPELINE FILE' _issues.append("Reference {} has an off-pipline file {} which isn't " "provided in the current directory {}".format( ref_.namespace, _ref_file.filename, _file.dir)) return _issues
def cache_write(self, tag, data, bkp=False, verbose=0): """Write data to the given cache. Args: tag (str): tag to store data to data (any): data to store bkp (bool): save timestamped backup file on save (if data changed) verbose (int): print process data Returns: (str): path to cache file """ from psyhive.utils import File, write_yaml _file = File(self.cache_fmt.format(tag)) lprint('WRITE CACHE FILE', _file.path, verbose=verbose) if bkp and _file.exists(): _data = self.cache_read(tag) if _data == data: lprint(' - DATA UNCHANGED, NO BKP REQUIRED') else: lprint(' - STORE BKP', _data) _bkp = '{}/_bkp_{}_{}.{}'.format( _file.dir, _file.basename, time.strftime('%y%m%d_%H%M%S'), _file.extn) lprint(' - BKP', _bkp) shutil.copy(_file.path, _bkp) if _file.extn == 'yml': write_yaml(file_=_file, data=data) else: obj_write(file_=_file.path, obj=data) return _file.path
def _build_bad_scene(force=True): _root = pipe.TMP+'/psyhive/remote_scene_check' # Built test refs _cube_mb = File('{}/cube.mb'.format(_root)) if not _cube_mb.exists(): print 'CREATING', _cube_mb cmds.polyCube() host.save_as(_cube_mb, export_selection=True) _deep_cube_mb = File('{}/dir/deep_cube.mb'.format(_root)) if not _deep_cube_mb.exists(): print 'CREATING', _deep_cube_mb cmds.polyCube() host.save_as(_deep_cube_mb, export_selection=True) _2_cube_mb = File('{}/2cube.mb'.format(_root)) if not _2_cube_mb.exists(): print 'CREATING', _cube_mb cmds.select([cmds.polyCube()[0] for _ in range(2)]) host.save_as(_2_cube_mb, export_selection=True) _asset_cube_mb = File('{}/dir/cube_rig_v001.mb'.format(_root)) if not _asset_cube_mb.exists(): print 'CREATING', _asset_cube_mb cmds.polyCube() host.save_as(_asset_cube_mb, export_selection=True) _psy_asset_cube_mb = File('{}/dir/cube_rig_main_v001.mb'.format(_root)) if not _psy_asset_cube_mb.exists(): print 'CREATING', _psy_asset_cube_mb cmds.polyCube() host.save_as(_psy_asset_cube_mb, export_selection=True) # Build scene host.new_scene(force=force) _ref = ref.create_ref(_cube_mb, namespace='bad_namespace') _ref.find_top_node().add_to_grp('CHAR') # Good ref _ref = ref.create_ref(_cube_mb, namespace='cube_1') _ref.find_top_node().add_to_grp('CHAR') # Good asset ref _ref = ref.create_ref(_asset_cube_mb, namespace='cube_2') _ref.find_top_node().add_to_grp('CHAR') # Good psy asset ref _ref = ref.create_ref(_psy_asset_cube_mb, namespace='cube_3') _ref.find_top_node().add_to_grp('CHAR') # Missing ref _ref = ref.create_ref(_deep_cube_mb, namespace='deepRef_7') _ref.find_top_node().add_to_grp('CHAR') # Multi top node _ref = ref.create_ref(_2_cube_mb, namespace='multiTop') assert not _ref.find_top_node(catch=True) # Without namespace hom.CMDS.polyCube().add_to_dlayer('test_LYR') # In bad group _ref = ref.create_ref(_cube_mb, namespace='badParent') _ref.find_top_node().add_to_grp('RANDOM') # In junk _ref = ref.create_ref(_cube_mb, namespace='junkRef_1') _ref.find_top_node().add_to_grp('JUNK') # No namespace cmds.file(_cube_mb.path, reference=True, renamingPrefix='blah') host.save_scene('{}/test2009_animation_v003.mb'.format(_root), force=True)