【MayaPy】file()

簡(jiǎn)介:

功能:打開,導(dǎo)入噪伊,導(dǎo)出囚戚,引用,保存或者重命名文件

示例:

首先導(dǎo)入模塊

import maya.cmds as cmds

保存現(xiàn)有場(chǎng)景格式為scii名字為fred.ma

# save the current scene to an ascii file named "fred.ma"
cmds.file( rename='fred.ma' )
cmds.file( save=True, type='mayaAscii' )
# save the current scene to an ascii file without the ".ma" extension
#
cmds.file( rename='tmp' )
cmds.file( save=True, defaultExtensions=False, type='mayaAscii' )
# open the file fred.ma, using the default load settings. Any references will
# be brought in in the same state they were in when fred.ma was last saved.
#
cmds.file( 'fred.ma', open=True )
# reference the file wilma.ma
#
cmds.file( 'C:/mystuff/wilma.mb', reference=True )
# reference the file barney.mb into a namespace called "rubble".
#
cmds.file( 'C:/maya/projects/default/scenes/barney.ma', reference=True, type='mayaAscii', namespace='rubble' )
# change the namespace containing barney.mb.
#
cmds.file( 'C:/maya/projects/default/scenes/barney.ma', edit=True, namespace='purpleDinosaur' )
# retrieve a string array of all files such as main scene and reference files in the scene
#
cmds.file( query=True, list=True )
# Result: C:/maya/projects/default/scenes/fred.ma C:/mystuff/wilma.mb C:/maya/projects/default/scenes/barney.ma
# Select "betty" and export betty to a separate file called "betty.mb".
# Reference the new betty file into this scene, replacing the
# previous betty object from this scene with the reference to betty.
#
cmds.file( 'c:/mystuff/betty.mb', type='mayaBinary', namespace='rubble', exportAsReference=True )
# Select all the objects associated with file betty.mb
#
cmds.file( 'c:/mystuff/betty.mb', selectAll=True )
# Result: rubble:betty
# Remove the reference file betty.mb. All nodes in betty.mb will
# be removed from the scene
cmds.file( 'c:/mystuff/betty.mb', removeReference=True )
# Query whether the file named "foo.mb" exists on disk
#
cmds.file( 'foo.mb', query=True, exists=True )
# Result: 0 #
# Query whether the reference node "rubble:betty is deferred loaded.
# Note, -referenceNode flag has to come before -q flag.
cmds.file(referenceNode='rubbleRN', query=True, deferReference=True )
# Query the last temp file during file save
#
cmds.file( query=True, lastTempFile=True)

構(gòu)建加載設(shè)置和加載設(shè)置標(biāo)簽

構(gòu)建加載設(shè)置

# Build load settings for "ref.ma"
cmds.file( 'ref.ma', open=True, buildLoadSettings=True )

編輯這些設(shè)置酌摇,以指示某些引用應(yīng)該被帶進(jìn)卸載膝舅。
注意:以下命令主要用于內(nèi)部使用。確定給定引用的數(shù)字ID可能不太容易(本例中為“2”)窑多。

cmds.loadSettings( '2', deferReference=1 )

# Edit those settings, to indicate that some reference should
# be brought in unloaded.
# Note: the following command is primarily intended for internal
# use. It may not be easy to determine the numeric ID of a given
# reference ("2" in this case) .
# cmds.loadSettings( '2', deferReference=1 )

使用已經(jīng)編輯過的設(shè)置打開文件

# Use the edited settings when opening the file
cmds.file('ref.ma', open=True, loadSettings='implicitLoadSettings')
#
#   Example for the '-cleanReference' and '-editCommand' flags
#
# Create a simple reference to a sphere
#
cmds.file( force=True, new=True )
cmds.polySphere()
cmds.file( rename='ref.ma' )
cmds.file( force=True, type='mayaAscii', save=True )
cmds.file( force=True, new=True )
cmds.file( 'ref.ma', reference=True, namespace='ref' )

# Scale the sphere
#
cmds.setAttr( 'ref:pSphere1.s', 5, 5, 5 )
cmds.getAttr( 'ref:pSphere1.s' )
# Result: 5 5 5 #

# The 'cleanReference' and 'editCommand' flags only work on
# unloaded references.
#
cmds.file( unloadReference='refRN' )

# Query the setAttr edits:
#
cmds.reference( referenceNode='refRN', query=True, editCommand=True )
# Result: setAttr ref:pSphere1.s -type "double3" 5 5 5 setAttr ref:lightLinker1.lnk -s 2 #

# Remove all setAttr edits on refRN:
#
cmds.file( cleanReference='refRN', editCommand='setAttr' )
cmds.reference( referenceNode='refRN', query=True, editCommand=True )
# Note that nothing is returned

cmds.file( loadReference='refRN' )

cmds.getAttr( 'ref:pSphere1.s' )
# Result: 1 1 1 #
# Note that scale has returned to 1 1 1

# apply the edit file to a reference
cmds.file("translateSphere.editMA", reference=True, applyTo="refRN")
# Result: maps <main> to refRN's namespace

# apply the edit file to nodes in the main scene
cmds.file("translateSphere.editMA", import=True, applyTo=":")
# Result: maps <main> to the root namespace

# apply the edit file to a reference, but it also has connections between two refs
cmds.file("connectionsBetweenRefs.editMA", reference=True, applyTo="refRN", mapPlaceHolderNamespace=("<otherRef>", "otherRefRN"))
# Result: maps <main> to refRN's namespace and <otherRef> to otherRefRN's namespace

# Change the modified state of the file.
cmds.file(modified=True)

# Set the file options
cmds.file( force=True, save=True, options='v=1;p=17',type='mayaAscii');
# Result:The saved file uses full names for attributes on nodes and flags in command.Also the precision of values in file is 17.

# Load Reference Preview

# Create a nested reference with a child reference under a parent reference.
cmds.file( force=True, new=True )
cmds.polySphere()
cmds.file( rename='child.ma' )
cmds.file( force=True, type='mayaAscii', save=True )
cmds.file( force=True, new=True )
cmds.file( 'child.ma', reference=True, namespace='child_namespace' )
cmds.file( rename='parent.ma' )
cmds.file( force=True, type='mayaAscii', save=True )

# Preview the unloaded child reference under the unloaded parent reference.
cmds.file( force=True, new=True )
cmds.file( 'parent.ma', reference=True, namespace='parent_namespace' )
cmds.file( 'parent.ma', unloadReference=True )
cmds.file( 'parent.ma', loadReferencePreview=True )

#
#              Example for 'mergeNamespacesOnClash'
#

# Create a reference
cmds.file( force=True, new=True )
cmds.namespace( add="bar" )
cmds.namespace( set="bar" )
cmds.polySphere();
cmds.file( rename="ref.ma" )
cmds.file( force=True, type='mayaAscii', save=True )

# Create a scene with some namespaces and objects
cmds.file( force=True, new=True )
cmds.namespace( add="ref:foo:bar" )
cmds.namespace( set="ref:foo:bar" )
cmds.polySphere();

# Merge into root
cmds.file('ref.ma', reference=True, mergeNamespacesOnClash=True, namespace=':');

# Merge into nested namespace
cmds.file('ref.ma', import=True, mergeNamespacesOnClash=True, namespace=':ref:foo');

# Don't merge namespace
cmds.file('ref.ma', reference=True, mergeNamespacesOnClash=False, namespace=':ref:foo');

# Now Edit the new namespace and merge it
cmds.file('ref.ma', edit=True, mergeNamespacesOnClash=True, namespace=':ref:foo');



###############################################/

/ Example for export with relativeNamespace #/

###############################################/

cmds.file(new=True,force=True)
cmds.sphere(name=":A:sphereA")
cmds.sphere(name=":A:B:sphereB")
cmds.sphere(name=":A:B:C:sphereC")
cmds.sphere(name=":D:sphereD")

# Select all the spheres.
#
cmds.select(":A:sphereA", replace=True)
cmds.select(":A:B:sphereB",add=True)
cmds.select(":A:B:C:sphereC",add=True)
cmds.select(":D:sphereD",add=True)

# Export all these spheres with -relativeNamespace flag.
#
cmds.file(rename="exp.ma")
cmds.file(force=True, exportSelected=True, type="mayaAscii", relativeNamespace=":A:B")
# The result in the exported file:
# :A:sphereA
# :C:sphereC
# :D:sphereD
# -sphereB
#
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末仍稀,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子埂息,更是在濱河造成了極大的恐慌技潘,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評(píng)論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件千康,死亡現(xiàn)場(chǎng)離奇詭異享幽,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)拾弃,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門值桩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人豪椿,你說我怎么就攤上這事奔坟。” “怎么了砂碉?”我有些...
    開封第一講書人閱讀 153,116評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵蛀蜜,是天一觀的道長。 經(jīng)常有香客問我增蹭,道長滴某,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,371評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮霎奢,結(jié)果婚禮上户誓,老公的妹妹穿的比我還像新娘。我一直安慰自己幕侠,他們只是感情好帝美,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,384評(píng)論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著晤硕,像睡著了一般悼潭。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上舞箍,一...
    開封第一講書人閱讀 49,111評(píng)論 1 285
  • 那天舰褪,我揣著相機(jī)與錄音,去河邊找鬼疏橄。 笑死占拍,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捎迫。 我是一名探鬼主播晃酒,決...
    沈念sama閱讀 38,416評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼窄绒!你這毒婦竟也來了贝次?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評(píng)論 0 259
  • 序言:老撾萬榮一對(duì)情侶失蹤颗祝,失蹤者是張志新(化名)和其女友劉穎浊闪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體螺戳,經(jīng)...
    沈念sama閱讀 43,558評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡搁宾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,007評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了倔幼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盖腿。...
    茶點(diǎn)故事閱讀 38,117評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖损同,靈堂內(nèi)的尸體忽然破棺而出翩腐,到底是詐尸還是另有隱情,我是刑警寧澤膏燃,帶...
    沈念sama閱讀 33,756評(píng)論 4 324
  • 正文 年R本政府宣布茂卦,位于F島的核電站,受9級(jí)特大地震影響组哩,放射性物質(zhì)發(fā)生泄漏等龙。R本人自食惡果不足惜处渣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,324評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蛛砰。 院中可真熱鬧罐栈,春花似錦、人聲如沸泥畅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽位仁。三九已至柑贞,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間障癌,已是汗流浹背凌外。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評(píng)論 1 262
  • 我被黑心中介騙來泰國打工辩尊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涛浙,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,578評(píng)論 2 355
  • 正文 我出身青樓摄欲,卻偏偏與公主長得像轿亮,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子胸墙,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,877評(píng)論 2 345