Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

When using python (iconTextButton) to import a path, is there a way to get rid of the suffix?

When using python (iconTextButton) to import a path, is there a way to get rid of the suffix?

niuxihuanming
Participant Participant
691 Views
7 Replies
Message 1 of 8

When using python (iconTextButton) to import a path, is there a way to get rid of the suffix?

niuxihuanming
Participant
Participant

When python uses (iconTextButton) to import the path, is there any way to remove the suffix? My script is a script that reads the cache. After clicking the folder icon to import the path, you need to delete the suffix before reading it. Is there any way to automatically delete the file suffix after importing the path?d68e8de206ec381a97addf2f04ba715.png

def Cacha_path(pp=1):
    global browsePath  
    singleFilter = "Maya Files (*.abc *.fur *.png)"       
    path = cmds.fileDialog2(fileFilter=singleFilter,dialogStyle=2)
    browsePath = str(path[0])
    if pp == 1:
        cmds.textField("readCachepath", e=True, text=browsePath)                                                                                                                                                                                                                           
    return browsePath             
                
def read_yeti_cache():
    r_cache_path = cmds.textField("readCachepath", q=True, text=True)
        
    r_cache_path = r_cache_path.replace('\\', '/')

    r_shufix = cmds.optionMenu(rshufix, query=True, value=True)
    sel = cmds.ls(sl=1, dag=1, s=1, type='pgYetiMaya')
    for node in sel:
        cmds.setAttr(node + '.fileMode', 1)
        cmds.setAttr(node + '.cacheFileName', "{}{}".format(r_cache_path, r_shufix), type='string')
                
cmds.window( title='maya_tool')
cmds.columnLayout() 

cmds.rowLayout(nc=3)
cmds.textField("readCachepath", editable=True, w=300, h=30)
cmds.iconTextButton(st='iconOnly', i1='SP_FileDialogNewFolder.png', c="Cacha_path(pp=1)",w=50, h=30)

cmds.setParent('..')
cmds.rowLayout(nc=3)
rshufix = cmds.optionMenu("rs_hufix", h=30, w = 200, label = "suffix")
cmds.menuItem( label='.%03d.fur' )
cmds.menuItem( label='.%03d.abc' )


cmds.button(l='Read cache', c="read_yeti_cache()", bgc=(0.4, 0.2, 0.3), width=200, h=30)
cmds.showWindow()
0 Likes
Accepted solutions (1)
692 Views
7 Replies
Replies (7)
Message 2 of 8

Kahylan
Advisor
Advisor
Accepted solution

Hi!

 

You can use String formatting to do so. Just add these two lines after the Line where you define browsePath (Line 5 of the code you posted)

 

suffix = browsePath.split(".")[-1]
browsePath = browsePath[:(-1*(len(suffix)+1))]

 

The code looks for fullstops in your string and then deletes the last fullstop and everything after it from the string

 

I hope it helps!

0 Likes
Message 3 of 8

niuxihuanming
Participant
Participant

Hello, at present, the script can only import the path when maya is first opened and used, but it will be invalid if it is exported again. My file is (Shape.004.fur). What can I do to delete the content after two points?
72429c03fa519363521325b97f69a2c.png

0 Likes
Message 4 of 8

Kahylan
Advisor
Advisor

This code should remove all fullstops and what is behind them in the file name of your path:

 

fileName = browsePath.split("/")[-1]
noSuffix = fileName.split(".")[0]
suffixLen = len(fileName)-len(noSuffix)
browsePath = browsePath[:(-1*suffixLen)]

 

 

I'm not sure about the issue in your video, I wasn't able to reproduce it.

I hope it helps!

0 Likes
Message 5 of 8

niuxihuanming
Participant
Participant

Hello, now the script can be used, but there is a serious bug. It is normal to open maya for the first time. After that, it will prompt an error:Error:( NameError: file <maya console> line 5: name 'browsePath' is not defined #), How should this be solved? The picture is the effect I want to achievea920da8c195587771164de35d9b0b9d.png

0 Likes
Message 6 of 8

Kahylan
Advisor
Advisor

could you please post the full code you have now again, so I can see if it is implemented right?

 

Also it would be nice if you could attach one of your fur files. I don't have Yeti so I can't create one myself.

Message 7 of 8

niuxihuanming
Participant
Participant

 

def Cacha_path(pp=1):
    global browsePath  
    singleFilter = "Maya Files (*.abc *.fur *.png)"       
    path = cmds.fileDialog2(fileFilter=singleFilter,dialogStyle=2)
    fileName = browsePath.split("/")[-1]
    noSuffix = fileName.split(".")[0]
    suffixLen = len(fileName)-len(noSuffix)
    browsePath = browsePath[:(-1*suffixLen)]
    if pp == 1:
        cmds.textField("readCachepath", e=True, text=browsePath)                                                                                                                                                                                                                           
    return browsePath             
                
def read_yeti_cache():
    r_cache_path = cmds.textField("readCachepath", q=True, text=True)
        
    r_cache_path = r_cache_path.replace('\\', '/')

    r_shufix = cmds.optionMenu(rshufix, query=True, value=True)
    sel = cmds.ls(sl=1, dag=1, s=1, type='pgYetiMaya')
    for node in sel:
        cmds.setAttr(node + '.fileMode', 1)
        cmds.setAttr(node + '.cacheFileName', "{}{}".format(r_cache_path, r_shufix), type='string')
                
cmds.window( title='maya_tool')
cmds.columnLayout() 

cmds.rowLayout(nc=3)
cmds.textField("readCachepath", editable=True, w=300, h=30)
cmds.iconTextButton(st='iconOnly', i1='SP_FileDialogNewFolder.png', c="Cacha_path(pp=1)",w=50, h=30)

cmds.setParent('..')
cmds.rowLayout(nc=3)
rshufix = cmds.optionMenu("rs_hufix", h=30, w = 200, label = "suffix")
cmds.menuItem( label='.%03d.fur' )
cmds.menuItem( label='.%03d.abc' )


cmds.button(l='Read cache', c="read_yeti_cache()", bgc=(0.4, 0.2, 0.3), width=200, h=30)
cmds.showWindow()

Hello, this function does not need to install Yeti, just make sure that the file name can enter the input field correctly after clicking the folder icon to select the file. The problem now is that the path doesn't go into the textField after the file is selected

0 Likes
Message 8 of 8

Kahylan
Advisor
Advisor

You made a small mistake when you implemented the stringformatting. You took out this line:

browsePath = path[0]

This resulted in the stringformatting referencing the variable before assignment.

I didn't really like this line anyway since the variable "path" was just assigned to be read out and then never used again. This how I would do this:

def Cacha_path(pp=1):
    singleFilter = "Maya Files (*.abc *.fur *.png)"       
    browsePath = cmds.fileDialog2(fileFilter=singleFilter,dialogStyle=2)[0]
    fileName = browsePath.split("/")[-1]
    noSuffix = fileName.split(".")[0]
    suffixLen = len(fileName)-len(noSuffix)
    browsePath = browsePath[:(-1*suffixLen)]
    if pp == 1:
        cmds.textField("readCachepath", e=True, text=browsePath)                                                                                                                                                                                                                           
    return browsePath             
                
def read_yeti_cache():
    r_cache_path = cmds.textField("readCachepath", q=True, text=True)
        
    r_cache_path = r_cache_path.replace('\\', '/')

    r_shufix = cmds.optionMenu(rshufix, query=True, value=True)
    sel = cmds.ls(sl=1, dag=1, s=1, type='pgYetiMaya')
    for node in sel:
        cmds.setAttr(node + '.fileMode', 1)
        cmds.setAttr(node + '.cacheFileName', "{}{}".format(r_cache_path, r_shufix), type='string')
                
cmds.window( title='maya_tool')
cmds.columnLayout() 

cmds.rowLayout(nc=3)
cmds.textField("readCachepath", editable=True, w=300, h=30)
cmds.iconTextButton(st='iconOnly', i1='SP_FileDialogNewFolder.png', c="Cacha_path(pp=1)",w=50, h=30)

cmds.setParent('..')
cmds.rowLayout(nc=3)
rshufix = cmds.optionMenu("rs_hufix", h=30, w = 200, label = "suffix")
cmds.menuItem( label='.%03d.fur' )
cmds.menuItem( label='.%03d.abc' )


cmds.button(l='Read cache', c="read_yeti_cache()", bgc=(0.4, 0.2, 0.3), width=200, h=30)
cmds.showWindow()

 

I hope it helps!