In Python, importing a script is the same as running it.
To list a series of scripts, you can simply list the directory in which they lie, and then run them by importing them. You can programmatically import a module using __import__("hand script.py"). An alternative to importing is execfile which is similar, but doesn’t require that you expose a directory via sys.path first.
import os
for script in os.listdir("."):
__import__(script)
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/e5cc06d4-59ae-4f2c-9bd2-8096f93c32c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Hey marcus,
The idea is to do that but using a widget that lists scripts that you can add or remove in a UI.
What is the diference between __import__ and just import? Both excute the script right?
But importing doesn't work if the module have classes and methods. You should import the module, create a class object and then theClassobject.myMethod()
Am I right?
Anyway,back to the main question that was about adding scripts un the Ul
--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/a84761fe-4ffb-461a-8bba-eec43d22fc5c%40googlegroups.com.
I can’t tell if you are asking about how to visually append items to a list, as in your example, or whether you are asking about how to build a plug-in architecture?
I’ll assume you mean the latter as it is the more difficult aspect.
Importing multiple times as Justin said is a good point about many of the gotchas you might experience while working on this. It’s possible you might find similar problems with reloading a module and using execfile as well; for example imports taking place within your script are typically cached like any other import, which means you’ll need to somehow either keep track of those imports and make sure they happen once per script, or count on the fact that it may happen.
Depending on the nature of your scripts, a safe method of running each script in isolation, without worrying too much about side-effects or paths, might be to subprocess each invokation.
import os
import sys
import subprocess
for script in os.listdir("."):
subprocess.call([sys.executable, script])
At this point, no amount of nested imports, side-effects or messing about with PYTHONPATH can throw your execution mechanism off guard!
The next step might be to append each result of os.listdir to your GUI.
# Using QListWidget with QListWidgetItem
for script in os.listdir("."):
item = QtGui.QListWidgetItem(script)
self.mylistwigdet.addItem(item)
Then, once hitting “run”, you would do as above; loop through each item and run each script.
for i in xrange(widget.count()):
item = widget.item(i)
script = item.text()
subprocess.call([sys.executable, script])
That should do it!
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3tNrpGH3POxtv0_PhbtsNfkWM8KVn6WRZe46frJfsbZA%40mail.gmail.com.
I can’t tell if you are asking about how to visually append items to a list, as in your example, or whether you are asking about how to build a plug-in architecture?
I’ll assume you mean the latter as it is the more difficult aspect.
Importing multiple times as Justin said is a good point about many of the gotchas you might experience while working on this. It’s possible you might find similar problems with reloading a module and using
execfileas well; for example imports taking place within your script are typically cached like any other import, which means you’ll need to somehow either keep track of those imports and make sure they happen once per script, or count on the fact that it may happen.
Depending on the nature of your scripts, a safe method of running each script in isolation, without worrying too much about side-effects or paths, might be to subprocess each invokation.import os import sys import subprocessfor script in os.listdir("."): subprocess.call([sys.executable, script])At this point, no amount of nested imports, side-effects or messing about with PYTHONPATH can throw your execution mechanism off guard!
The next step might be to append each result of
os.listdirto your GUI.# Using QListWidget with QListWidgetItemfor script in os.listdir("."): item = QtGui.QListWidgetItem(script) self.mylistwigdet.addItem(item)Then, once hitting “run”, you would do as above; loop through each item and run each script.
for i in xrange(widget.count()): item = widget.item(i) script = item.text() subprocess.call([sys.executable, script])
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmODKAMSKzW%3DUQ21DmC6TsUQnuzT2%2BBD0UN4774kuT5pWkA%40mail.gmail.com.
Yes, the approach only works if the scripts have no relation and if they don’t need access to a currently running environment, like Maya.
If you have control of how the scripts it is meant to run are developed, a common interface should do the trick.
Here’s an example of how this works in Pyblish.
import pyblish.api
# find all scripts, and return their common interface;
# a class called "Plugin" in this case
for plugin in pyblish.api.discover():
plugin_ = plugin()
instantiated.process()
It takes all scripts it finds on paths registered by someone else, and expects those scripts to contain a subclass of a particular superclass provided by the framework; the Plugin class.
You could do something similar or maybe even identical to this, as this is also affecting the internal state of Maya and also deals with side-effects, using execfile as opposed to import.
Sorry, looks like I pressed send before I finished typing up the code. Here it is again, but properly formatted.
import pyblish.api
# find all scripts, and return their common interface;
# a class called "Plugin" in this case
for Plugin in pyblish.api.discover():
plugin = Plugin()
plugin.process()