Jump to content

custom save python script - a very beginner


tagosaku

Recommended Posts

Hi, I am a very beginner of coding and looking for a custom save shell by python. This time, especially I'd like to ask for how to replace ".comment." part.

Example file is "fx_dest_vocity_v051.04.comment.hip" and a procedure is :
1 - click shelf dock
2 - show a popUpWindow and type a comment
3 - click the popUpWindow, close it, and a script replaces "comment" part and new file name is saved, for example, "fx_dest_vocity_v051.04.vel2xfaster.hip"

my best guess is;
fileName = hou.hipFile.basename()       ## for instance, file name is "fx_dest_vocity_v051.04.comment.hip"
hou.ui.displayMessage("type comments", variable )  ## save comment
hou.ui.displayMessage.close
result = re.search( "([0-9]{2})\.???????\.hip", fileName )  ## get  comment part
newFileName = fileName.replace( result.group(1), variable )
hou.hipFile.save(newFileName)   ## new file name "fx_dest_vocity_v051.04.vel2xfaster.hip"

I know it's so many broken lines, so if someone helps to rewrite it, it would be great, thanks!

Edited by tagosaku
Link to comment
Share on other sites

I think your "mock script" will work as designed, with minor edits. Always use raw literal before regexes in Python: r'^(.+)\.(.+?)\.hip$'. Otherwise you have to escape each regex backslash with another backslash.

 

3 hours ago, tagosaku said:

if someone helps to rewrite it, it would be great

Ok, here is how I would do it myself. Notes: 1) it doesn't required for an addition to be the word "comment" here; 2) for simplicity, it does not perform any error check: assume proper use or add extra if statements.

import os.path as op
import hou

# Fetch tuple with index of pressed button and input text.
choice, text = hou.ui.readInput('Type comments message',
                                help='Type comments description.',
                                title='Commented HIP',
                                buttons=('OK', 'Cancel'),
                                default_choice=1,
                                close_choice=1)

# If OK was pressed with some non-empty text.
if choice == 0 and text:
    path = hou.hipFile.path()
    dir, name = op.split(path)

    # Discard extension.
    name = op.splitext(name)[0]

    # Discard one '.anytext' occurence at the end.
    name = name.rsplit('.', 1)[0]

    # Make new name with input text.
    new_name = '%s.%s.hip' % (name, text)

    # Finally, create an absolute path to save file to.
    new_path = op.normpath(op.join(dir, new_name))

    # Save the file.
    #hou.hipFile.save(new_path)
    print(new_path)  # TEMP: print to console instead.

 

Link to comment
Share on other sites

F1, thanks for your quick reply. Whole codes are changed and I can understand 70%. That's ok, and I tried your code and working well. I would like to ask for two things,

When typing comment and hitting 'enter key', it executes as cancel, but prefer to execute as 'ok'. Then I changed to

default_choice=1

Is that correct approach?

Second questions, Actually I want to sometimes add comment words, not always. For instance, sometimes name
"fx_dest_vocity_v051.04.comment.hip" or 
"fx_dest_vocity_v051.04.hip"
Then I tried to change your code, but messed up. (sometime it loses last two digit numbers in a condition...)
Could you add the feature that I can save a file with blank text?

Link to comment
Share on other sites

1. Choice nums relate to the buttons tuple: ('OK', 'Cancel'). 0 is 'OK' and 1 is 'Cancel' here.

2. You can accept cases where text not entered and omit name changing part if there is no text entered:

if choice == 0:
    path = hou.hipFile.path()

    # Replace existing comment.
    if text:
        dir, name = op.split(path)
        name = op.splitext(name)[0]
        name = name.rsplit('.', 1)[0]
        name = '%s.%s.hip' % (name, text)
        path = op.join(dir, name)
        path = op.normpath(path)

    # hou.hipFile.save(path)
    print(path)

Personally, I would just normally save with Ctrl+S.

3. By the way, if you use the code as a shelf tool, you have different ways of invoking it: click, Ctrl+click, etc. Try printing kwargs dict available inside shelf tool script and inspect contents. You can check if the shelf was clicked with modifier button to bypass dialog window.

Edited by f1480187
  • Thanks 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...