0
import maya.cmds as cmds

def aaa():
    blah... blah...

aaa()

saved this code as aaa.py and placed it right folder and launched Maya and run the code by

import aaa
reload(aaa)

And it executes twice when the first run. How can I prevent that?

1 Answers1

0

And it executes twice when the first run.

Because calling import aaa, will execute all the code in aaa, and thus it will call the aaa() function. then calling reload(aaa) will re-import aaa, so it will run all of the code in it again.

How can I prevent that?

Just remove the reload(aaa), I really don't see why you have it there in the first place, it's very seldom needed.


I don't also understand why you have you're code organized like that in the first place. You should almost certainly have them be like this:

import maya.cmds as cmds

def aaa():
    blah... blah...

from aaa import aaa

aaa()
ruohola
  • 21,987
  • 6
  • 62
  • 97
  • Because without reload(), it will execute code only the first time. From the second run, you need reload(), right? – user8972552 Jul 26 '19 at 13:40
  • No, you don't understand. Of course your code will run twice if you place a function call in a module which you first `import` and then `reload`. – ruohola Jul 26 '19 at 21:13