Jump to content

hou.hipFile.save but stay in the same session.


younglegend

Recommended Posts

Hello! I came across this super useful script by F1 few weeks ago which allows me to save hip files with comments. But i'd like to know if there's a way to save a scene while staying in current session. (i.e a new file save but stay on the current hip file) :mellow:

 

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:
    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)

 

Edited by kishenpj
Link to comment
Share on other sites

import os.path as op
import hou

activeSession = hou.hipFile.name()
print activeSession

# 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:
    # save tmp file
    tmp = ('').join(hou.hipFile.name().split('.')[0]) + '_tmp.hip'
    hou.hipFile.save(tmp)
    
    # set path name to the current session name
    path = activeFilename

    # 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)

    # save off the newly commented version
    hou.hipFile.save(path)
    # load the tmp file
    hou.hipFile.load(tmp)
    # rename it to the original session name
    hou.hipFile.setName(activeSession)
    
    print(path)


    It's clunky - but it does what you want it to
    

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

3 hours ago, garf said:

import os.path as op
import hou

activeSession = hou.hipFile.name()
print activeSession

# 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:
    # save tmp file
    tmp = ('').join(hou.hipFile.name().split('.')[0]) + '_tmp.hip'
    hou.hipFile.save(tmp)
    
    # set path name to the current session name
    path = activeFilename

    # 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)

    # save off the newly commented version
    hou.hipFile.save(path)
    # load the tmp file
    hou.hipFile.load(tmp)
    # rename it to the original session name
    hou.hipFile.setName(activeSession)
    
    print(path)


    It's clunky - but it does what you want it to
    

Might be a problem if the file size is big but this is a good workaround! ....and the only choice i guess. :unsure:

Link to comment
Share on other sites

Well, this one works! :lol:

import os.path as op
import hou

activeSession = hou.hipFile.name()

# 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 = op.normpath(op.join(dir, new_name))

    # Save the file.
    hou.hipFile.save(file_name=new, save_to_recent_files=False)
    hou.hipFile.setName(path)
    print "File saved to - " + (new)

 

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...