How to get the current file name via python?

Hey Frank!

@alexaz and I went over a couple options; it kinda comes down to what you want to do with it.

If you want the fast and easy approach, you can just query the source and ask for the media info. Something like this:

from rv import commands
frame = commands.frameStart()
sources = commands.sourcesAtFrame(frame)
metadata =  commands.sourceMediaInfo(sources[0])

# In the metadata, one key is 'file' which has your full path including sequence name.
file = metadata.get("file") 

I originally misinterpreted your request, but I think the answer has value. If you are looking for the specific file that you are reading from your sequence, you can do something along the lines of this:

import rv

# The difference here is that we traverse the graph to find not only the source, but also the source frame number.
info = rv.extra_commands.sourceMetaInfoAtFrame(rv.commands.frame())
metadata =  rv.commands.sourceMediaInfo(info['node'])
filename = metadata.get("file")
sourceFrame = info['frame']

# Note: this only works how we want it to for file sequences, error check to make sure you are dealing in file sequencese, as well as protecting yourself against missing framees.
files = rv.commands.existingFilesInSequence(filename)
frames = rv.commands.existingFramesInSequence(filename)

# Assmeble the files and frame numbers in something we can index into.
framesToFiles = dict(zip(frames, files))


# The result can be found here:
framesToFiles[sourceFrame]

Hopefully those approaches find value in what you are trying to achieve.

-Kessler

4 Likes