1

i'm working on a "python script" in "MAYA", when i use "pm.fileDialog2" and when i click the exit button, the loop script is still running, i want when i open the file and click the close button "for loop" stops. Thank you for the help

this is the pyside and pymel script in "MAYA"

#file: run_script
_runScript(scripts=['file_a.py', 'file_b.py'])
def _runScript(scripts=[]):
    if len(scripts):
        for script in scripts:
            exec(open(script).read(), globals())

#file: file_a
open_file = pm.fileDialog2(cap='Open File',ff='Maya Files (*.ma *.mb)', dir='C:/', ds=1, fm=4 )
if open_file:
    print('Open File')
else:
    print('Not Open')

#file: file_b
print('this is file_b')
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Wongselent
  • 31
  • 3

1 Answers1

0

I'm not sure I'm understanding the use case, so apologies if this is not what you're looking for.

Its usually not a good idea to use exec() because it may leave unforseen traces in your global namespace -- in your example the open_file variable is going to exist after you run because exec() will run as if you had just run the code in-line. The more common -- and safer -- pattern is to do the work inside of functions and then import those functions to do work.

If you want to open a file and do something with it in a single function, the usual pattern would be to have a file which defined the function you want to run:

# in "do_something.py"
import maya.cmds as cmds
def process(filename):
   if filename:
        cmds.file(open=filename)
        print "doing things in ...", filename
        #real work here
   else:
        print "no file provided"
        return

And then import that file and use the function to do the work. That way you have the control flow without any extra tricks.

 # in main file or the listener
 import do_something
 import maya.cmds

 filename =  pm.fileDialog2(cap='Open File',ff='Maya Files (*.ma *.mb)', dir='C:/', ds=1, fm=4 )

 do_something.process(filename)

To import the file, you'll need to put it somewhere on the python path; basics are here

theodox
  • 12,028
  • 3
  • 23
  • 36
  • [image tool](https://ibb.co/6mFTg7P) Thank you for helping me, sir, the script that I made is as shown below. – Wongselent May 16 '19 at 16:47