Script to Rename Each Frame to Custom Prefix?

Script to Rename Each Frame to Custom Prefix?

andymc4997
Advocate Advocate
1,559 Views
8 Replies
Message 1 of 9

Script to Rename Each Frame to Custom Prefix?

andymc4997
Advocate
Advocate

Hey Maya programmers, 

 

Per the title, I was wondering if it's possible to output each frame of my scene with a custom prefix in the filename. I assume this would be done via the "Pre render frame MEL:" section in Render Options? I would like this functionality because instead of creating multiple cameras/render layers for my still renders, I prefer to keyframe positions and visibility for various parts. Of course then I have to manually rename each outputted frame. 

 

Hopefully I'm clearly explaining what I'm looking for, so any help would be greatly appreciated. 

 

Thank you!

0 Likes
1,560 Views
8 Replies
Replies (8)
Message 2 of 9

FirespriteNate
Advocate
Advocate

you would have to somehow provide a list for each custom prefix you wanted, so if your render sequence was 20 frames, you'd have to have a list of 20 custom prefix names, right?

 

I'm not super au-fait with rendering, and certainly not with using preRender MEL functions, so there may be a much better way to do this, but here is one very simple approach that at least shows you what you want can be done. 

 

If you create a preRender global MEL proc (called whatever) and generate your list of prefixes, like so:

 

global proc preRender()
{
    global string $gPrefixNames[] = {
        "Homer",
        "Bart",
        "Lisa",
        "Maggie",
        "Marge"
    };
    setAttr defaultRenderGlobals.imageFilePrefix -type "string" $gPrefixNames[0];
}

 

This example assumes we have a render sequence of FIVE frames: 0,1,2,3,4 and the fives names listed represent the prefixes for each frame. (We also set the first one here, as the PreRenderFrame doesn't actuallty seem to run before the first frame is rendered? 🤔 ).

 

Then we create a second MEL proc for preRenderFrame:

 

global proc preRenderFrame()
{
    global string $gPrefixNames[];
    int $frame = `currentTime -q`;
    string $prefix = $gPrefixNames[$frame];
    setAttr defaultRenderGlobals.imageFilePrefix -type "string" $prefix;
}

 

enter both of these functions into the relevant Render Options fields:

FirespriteNate_0-1708449414372.png

and then the results after executing a Render Sequence should be something like:

 

Starting Render Sequence with Maya Software
Rendering masterLayer from frame 1 to frame 5
Rendering Frame: 1 (1/5)  Camera: persp  Layer: masterLayer
	D:/test_proj/images/tmp/Homer.0001.png
Rendering Frame: 2 (2/5)  Camera: persp  Layer: masterLayer
	D:/test_proj/images/tmp/Bart.0002.png
Rendering Frame: 3 (3/5)  Camera: persp  Layer: masterLayer
	D:/test_proj/images/tmp/Lisa.0003.png
Rendering Frame: 4 (4/5)  Camera: persp  Layer: masterLayer
	D:/test_proj/images/tmp/Maggie.0004.png
Rendering Frame: 5 (5/5)  Camera: persp  Layer: masterLayer
	D:/test_proj/images/tmp/Marge.0005.png

Sequence rendering completed

 

Maybe this is some help until you get any better answers 😅

0 Likes
Message 3 of 9

andymc4997
Advocate
Advocate

Hi @FirespriteNate, 

 

Sorry it's been so long since responding to this. Thanks for your reply, but unfortunately, nothing happened after entering your code in their respective fields in Render Settings. 

 

Am I doing something wrong? Do I need to point to external MEL files? 

 

andymc4997_0-1710523852295.png

 

0 Likes
Message 4 of 9

FirespriteNate
Advocate
Advocate

defining a global procedure doesn't actually run it. What you have done there is simply define a MEL function in each case. My suggestion was you define those global procs first in MEL (e.g. in a Script Editor MEL tab) and then you just call the function names (i.e. run that proc) in the render callback.

Basically, the text/code you type into those RenderOptions fields should be only a MEL command (proc) name.

There are a couple of different ways to achieve this, but for simplicity sake, my suggestion would be to save both of those procs into individual text files in your Documents\maya\scripts folder and call them the SAME name as their global proc name.. so if you have:

global proc preRenderCode()
{
    // blah, blah, blah...
}

you save that as preRenderCode.mel (notice how the mel file name is EXACTLY the same as the proc name)

then in your pre render field you just input:

preRenderCode

to run that MEL proc. same for post render and whatever other callbacks you want to supply.

Don't try to actually define a function in those fields.

Hopefully that should fix it.

Message 5 of 9

andymc4997
Advocate
Advocate

Hi @FirespriteNate 

 

Ok so it sort of worked.. lol

A couple issues:

 

The script skips over the first name and starts applying them at the second. Here are how my naming is set in the preRender MEL:

 

andymc4997_0-1710941228231.png

When I ran a 3 frame test render sequence, it gave me this:

 

andymc4997_1-1710941270822.png

The naming should actually be, "Front", "34_Left", and "34_Right". Could this possibly be because my frames don't start from 0? I don't like that Maya does this, so I always start with 1 as my first frame. 

 

It also inserts a "_1" into the first two frames, but not the 3rd? Would you know why this is happening? 


Thanks so much!

0 Likes
Message 6 of 9

FirespriteNate
Advocate
Advocate

Hey Andy, yeah, I guess not starting at frame 0 could have this effect, if so, you could probably just fix that by simply adding a null first entry in the $gPrefixNames list (i.e. just insert "", before "Front" in your array). 

I have no idea where the _1 is coming from though, I don't know how you've set the numbering/suffix options, but it could also be due to the filename "34_Left" already exising from an earlier aborted test or something maybe?

 

0 Likes
Message 7 of 9

andymc4997
Advocate
Advocate

Hi @FirespriteNate, 

 

So I tried entering the null, and it apparently broke the script, as nothing happens now when outputting the frames. 

 

Here's how I edited it: 

 

andymc4997_0-1711461091461.png

 

0 Likes
Message 8 of 9

FirespriteNate
Advocate
Advocate

You've added a new "element zero" to that array, which is an empty value, but at the end of that bit of code, you are still setting the .imageFilePrefix to the first entry in the array ($gPrefixNames[0]), which is now nothing.

You probably want to update this to the correct index, i.e. $gPrefixNames[1] to match the desired frame 1.

0 Likes
Message 9 of 9

andymc4997
Advocate
Advocate
Ahh I see, ok I'll give that a try, thanks!
0 Likes