I want to get just the scene name of the file that is currently open. Not the path or extension.
cmds.file(q=True, sn=True)
I cannot use the above as it returns the full path.
Thanks
the os
module includes utilities for this:
filepath = cmds.file(q=True, sn=True)
filename = os.path.basename(filepath)
raw_name, extension = os.path.splitext(filename)
cmds.file(q=True, sn=True, shn=True)
(I wanted to post this as a comment to 's answer, but I can't as I don't have the required reputation level) 答案的评论,但我不能,因为我没有所需的声誉级别)
's answer is the correct answer (using the Python 'os' module): 的答案是正确的答案(使用 Python 'os' 模块):
filepath = cmds.file(q=True, sn=True)
filename = os.path.basename(filepath)
raw_name, extension = os.path.splitext(filename)
You can make it shorter by combining with 's answer (which gives the file name with extension, but in a single instruction instead of 2 as in 's answer):的答案(它给出带扩展名的文件名,但在单个指令中而不是在的答案中使用 2 )来:
filename = cmds.file(q=True, sn=True, shn=True)
raw_name, extension = os.path.splitext(filename)
import os
# a= "Full File Path of the scene"
a= r"C:\Python36\Lib\site-packages\anytree\node\nodemixin.py"
print(os.path.split(a)[1].split('.')[0])
'nodemixin'
print(os.path.splitext(a)[0].split('\\')[-1])
'nodemixin'
When you use Python in Maya you can use the file
command. When you split it based on /
you will get a list as result, which you then just count from the rear [-1]. That gives you the current Maya scene file name as string.
cmds.file(q=True, sn=True).split('/')[-1]
This works fine:
import maya.cmds as cmds
scene = cmds.file(q=True, sn=True)
scene = scene.split('/')[-1]
print(scene)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.