I need your help to create a AutoSave toggle UI, either python(cmds) or mel would be helpful.

I need your help to create a AutoSave toggle UI, either python(cmds) or mel would be helpful.

amaterasu-qbb
Collaborator Collaborator
860 Views
5 Replies
Message 1 of 6

I need your help to create a AutoSave toggle UI, either python(cmds) or mel would be helpful.

amaterasu-qbb
Collaborator
Collaborator

Hi! everyone.

I just created the UI using only python cmds. I am new to creating UI with commands, so please let me know if there is anything I should implement. The image above is the concept of the UI, and the image below is the actual UI I created.

it's a concept picture.it's a concept picture.what I made.what I made.

I am unhappy with the UI.

  • The UI does not line up nicely.
  • I would like to implement the ability to make the button icon for textFieldButtonGrp a folder icon and open the user's folder when clicked.

Also, how do I connect autosave commands to the UI? Do I have to use def?

 

# autosaveChangeUI
import maya.cmds as cmds

window = cmds.window( t = 'autosaveChangeUI' )
cmds.columnLayout( adj = True )

cmds.frameLayout( label='AutoSave' )

cmds.checkBox( label = 'Enable' )
cmds.checkBox( label = 'Prompt before save' )
cmds.checkBox( label = 'Limit autosaves' )

cmds.intSliderGrp( f = True, l = 'Number of autosaves:', min = 0, max = 10, fmn = 0, fmx = 9999, v = 10 )
cmds.intSliderGrp( f = True, l = 'Interval (minutes):', min = 0, max = 10, fmn = 0, fmx = 60, v = 10 )

cmds.radioButtonGrp( l = 'Autosave destination:', la3 = [ 'Project', 'Named folder', 'Env Var' ], nrb = 3 )

cmds.textFieldButtonGrp( l = 'Label', bl = 'Button' )

cmds.showWindow( window )

 

Thanks.

Replies (5)
Message 2 of 6

1925250542
Participant
Participant

Autosave you can make a read/write, write module. My thinking is that you do a write out for each step, then each time you open it you do a read/write of the previously written data, and then set the control with the previously written data. You can use a dictionary to quickly create the property

0 Likes
Message 3 of 6

amaterasu-qbb
Collaborator
Collaborator

Hi! Thank you for your help. @1925250542 .

 

Autosave you can make a read/write, write module... OK. module = Files containing a program.

My thinking is that you do a write out for each step, then each time you open it you do a read/write of the previously written data, and then set the control with the previously written data... Excuse me, I am not sure how to do this.

You can use a dictionary to quickly create the property... Excuse me, I am not sure what the dictionary means and how to do this.

 


@1925250542 wrote:

Autosave you can make a read/write, write module. My thinking is that you do a write out for each step, then each time you open it you do a read/write of the previously written data, and then set the control with the previously written data. You can use a dictionary to quickly create the property


I modified the UI as follows and it looks better. The difference is that cmds.checkBox was changed to cmds.checkBoxGrp.

 

v02_version / ui fixedv02_version / ui fixed

import maya.cmds as cmds

window = cmds.window( t = 'autosaveChangeUI' )
cmds.columnLayout( adj = True, )

cmds.frameLayout( l = 'AutoSave')

cmds.checkBoxGrp( l = 'Enable' )
cmds.checkBoxGrp( l = 'Prompt before save' )
cmds.checkBoxGrp( l = 'Limit autosaves' )

cmds.intSliderGrp( f = True, l = 'Number of autosaves:', min = 0, max = 10, fmn = 0, fmx = 9999, v = 10 )
cmds.intSliderGrp( f = True, l = 'Interval (minutes):', min = 0, max = 10, fmn = 0, fmx = 60, v = 10 )

cmds.radioButtonGrp( l = 'Autosave destination:', la3 = [ 'Project', 'Named folder', 'Env Var' ], nrb = 3 )

cmds.textFieldButtonGrp( l = 'Label', bl = 'Button' )

cmds.showWindow( window )

 

My next wish is to change the appearance of the button in cmds.textFieldButtonGrp( l = 'Label', bl = 'Button' ) to a file icon. If not possible, I will accept the current button appearance.

I also need to create a checkbox to enable and a checkbox to disable to make it work the same as AutoSave in Files/Projects in Preferences.
My intention is to do this as far as I can on my own, but I would be grateful for your help.

 

when checkbox is not checked.when checkbox is not checked.

 

Thanks.

Message 4 of 6

1925250542
Participant
Participant

I modified it for you, it's my own store read/write scheme, I store the first checkBoxGrp into the x variable for you, and then every time you open it, it initializes the read/write with the data you wrote out before. My comment is in Chinese, sorry, you can translate it!

import maya.cmds as cmds
import os
import pickle

def UI():
	window = cmds.window( t = 'autosaveChangeUI' )
	cmds.columnLayout( adj = True, )

	cmds.frameLayout( l = 'AutoSave')

	x = cmds.checkBoxGrp( l = 'Enable' ,cc=lambda *args: modify_file_attr(key_var,cmds.checkBoxGrp(x, query=True, v=True)))
	cmds.checkBoxGrp( l = 'Prompt before save' )
	cmds.checkBoxGrp( l = 'Limit autosaves' )

	cmds.intSliderGrp( f = True, l = 'Number of autosaves:', min = 0, max = 10, fmn = 0, fmx = 9999, v = 10 )
	cmds.intSliderGrp( f = True, l = 'Interval (minutes):', min = 0, max = 10, fmn = 0, fmx = 60, v = 10 )

	cmds.radioButtonGrp( l = 'Autosave destination:', la3 = [ 'Project', 'Named folder', 'Env Var' ], nrb = 3 )

	cmds.textFieldButtonGrp( l = 'Label', bl = 'Button' )

	init_Attr(x)
	cmds.showWindow( window )



def init_Attr(x): #初始化参数
    """ 初始化参数 """
    

    cmds.button(x,edit=True,en=not(reading_history_file()['your key_var']))  


data_storage_folder_path = #Data storage path

DATAFILENAME = 'HistoricalDataFile.json'

def Detect_and_create_history_folder():
    """ 创建历史文件夹 判断脚本下有没有历史文件夹,如果没有会自动创建"""
    
    if not os.path.exists(data_storage_folder_path):
        os.makedirs(data_storage_folder_path)
        print('检测到你没有创建数据文件夹,将会自动帮你创建一个数据文件夹')



def Detect_and_create_history_file():
    """ 创建历史文件 判断有没有历史文件,如果没有会自动创建并写入初始数据信息"""

    if not os.path.exists(data_storage_folder_path+'/'+ DATAFILENAME):
        with open(data_storage_folder_path+'/'+ DATAFILENAME, 'w') as f:
            f.write(' ')
        print('检测到你没有创建数据文件夹,将会自动帮你创建一个数据文件')
        write_default_attr()


def write_default_attr():
    """ 重新写入文件信息 """

    default_attr = {'Your control name': "Your control's default value",
                    }
    # 默认属性

    with open(data_storage_folder_path+'/'+ DATAFILENAME, 'wb')as file:
        pickle.dump(default_attr,file)

def reading_history_file(): # 读取文件信息
    """ 读取文件信息 """

    with open(data_storage_folder_path+'/'+ DATAFILENAME, 'rb') as file:
        default_attr = pickle.load(file)
    return default_attr

def modify_file_attr(key_var,modify_var):
    """ 修改文件信息 """
    
    reading_history_file()
    default_attr = reading_history_file()

    default_attr[key_var] = modify_var

    with open(data_storage_folder_path+'/'+ DATAFILENAME, 'wb')as file:
        pickle.dump(default_attr,file)


UI()
Message 5 of 6

amaterasu-qbb
Collaborator
Collaborator

Hi! @1925250542 ,

 

You helped me. You took the time to help me and write this code. I will always be grateful.

I translated the Chinese with Google Translate and copied and pasted it. This text was also translated by Deepl and this is all I could do. 🤣🤣🤣
There were so many def that it was far beyond my ability. I would like to salute your programming.

Here is the translated version.

 

import maya.cmds as cmds
import os
import pickle

def UI():
    window = cmds.window( t = 'autosaveChangeUI' )
    cmds.columnLayout( adj = True, )
    cmds.frameLayout( l = 'AutoSave')
    
    x = cmds.checkBoxGrp( l = 'Enable' ,cc=lambda *args: modify_file_attr(key_var,cmds.checkBoxGrp(x, query=True, v=True)))
    cmds.checkBoxGrp( l = 'Prompt before save' )
    cmds.checkBoxGrp( l = 'Limit autosaves' )
    
    cmds.intSliderGrp( f = True, l = 'Number of autosaves:', min = 0, max = 10, fmn = 0, fmx = 9999, v = 10 )
    cmds.intSliderGrp( f = True, l = 'Interval (minutes):', min = 0, max = 10, fmn = 0, fmx = 60, v = 10 )

    cmds.radioButtonGrp( l = 'Autosave destination:', la3 = [ 'Project', 'Named folder', 'Env Var' ], nrb = 3 )

    cmds.textFieldButtonGrp( l = 'Label', bl = 'Button' )

    init_Attr(x)
    cmds.showWindow( window )



def init_Attr(x): #Initialization parameters
    """ Initialization parameters """
    

    cmds.button(x,edit=True,en=not(reading_history_file()['your key_var']))  


data_storage_folder_path = #Data storage path

DATAFILENAME = 'HistoricalDataFile.json'

def Detect_and_create_history_folder():
    """ Create a history folder. Determine whether there is a history folder under the script. If not, it will be created automatically."""
    
    if not os.path.exists(data_storage_folder_path):
        os.makedirs(data_storage_folder_path)
        print('It is detected that you have not created a data folder and will automatically create a data folder for you.')



def Detect_and_create_history_file():
    """ Create a history file to determine whether there is a history file. If not, it will automatically create and write the initial data information."""

    if not os.path.exists(data_storage_folder_path+'/'+ DATAFILENAME):
        with open(data_storage_folder_path+'/'+ DATAFILENAME, 'w') as f:
            f.write(' ')
        print('It is detected that you have not created a data folder and will automatically create a data file for you.')
        write_default_attr()


def write_default_attr():
    """ Rewrite file information """

    default_attr = {'Your control name': "Your control's default value",
                    }
    # Default properties

    with open(data_storage_folder_path+'/'+ DATAFILENAME, 'wb')as file:
        pickle.dump(default_attr,file)

def reading_history_file(): # Read file information
    """ Read file information """

    with open(data_storage_folder_path+'/'+ DATAFILENAME, 'rb') as file:
        default_attr = pickle.load(file)
    return default_attr

def modify_file_attr(key_var,modify_var):
    """ Modify file information """
    
    reading_history_file()
    default_attr = reading_history_file()

    default_attr[key_var] = modify_var

    with open(data_storage_folder_path+'/'+ DATAFILENAME, 'wb')as file:
        pickle.dump(default_attr,file)


UI()

 

The problem is that in my environment, Maya Creative on Windows 11, I cannot run it and get the following error message.

 

# Error: invalid syntax

 

Thanks.

0 Likes
Message 6 of 6

1925250542
Participant
Participant
Yes, because I'm copying this directly from my code, you'll need to modify it to use it, you'll need to go and de-write the variables all over again