Pre and post render python scripting

Hello guys, this is my first post around here, even though I’ve been following for a couple months now.

I’ve been encountering a issue in python scripting in general and that is the reload of the script in pre render operations.

I have a script that is called renderOperations.py inside the maya scripts folder and in the Post Translate Python Script I have:

import renderOperations
reload(renderOperations)
renderOperations.main()

The weird thing is that when I first render the scene, it doesn’t run the operations it should run, only at the second attempt. I discovered around the web that the reload function seems to be a bit buggy, because some people have the same issue.

Any idea what might cause this? The operations on the script are simple: mesh smooth before render, remove it after.

Thanks in advance

While reload can be weird, we’d probably need to see what exactly is happening in renderOperations to be able to say if it is the cause or not.

this is the renderOperations.py code

import maya.cmds as mc
import maya.mel as mel
import os, sys

def main():

    mc.prepareRender(edit=True, preRender=preRenderOp)
    mc.prepareRender(edit=True, postRender=postRenderOp)

def preRenderOp():

    meshes = mc.ls(type='mesh')
    
    for mesh in meshes:
        a = mc.polySmooth(mesh, sdt=2, dv=2)
        
def postRenderOp():

    meshes = mc.ls(type='mesh')

    for mesh in meshes:
        
        connections = mc.listConnections(mesh)
        for connection in connections:
            if 'polySmooth' in connection:
                mc.polySmooth(connection, edit=True, sdt=0, dv=0)         
                
    mc.select(meshes)
    mel.eval("DeleteHistory;")
    mc.select(clear=True)

The weird thing is that the preRenderOp doesn’t run at the first try, but the postRenderOp does…