Skip to content

Instantly share code, notes, and snippets.

@jamesWalker55
Created September 3, 2023 19:06
Show Gist options
  • Save jamesWalker55/1f02c7940dafca990caf2472c8aa41c8 to your computer and use it in GitHub Desktop.
Save jamesWalker55/1f02c7940dafca990caf2472c8aa41c8 to your computer and use it in GitHub Desktop.
Substance Painter script to drag-and-drop images to import to project.
import datetime
import substance_painter.resource as res
import substance_painter.ui
from PySide2 import QtWidgets
def now():
return datetime.datetime.now().timestamp()
def decrementing_now():
return 2**31 - now()
def import_images(paths: list[str]):
usage = res.Usage.TEXTURE
unique_id = round(decrementing_now() * 100)
unique_id = str(unique_id)[-5:]
base_name = f"1_{unique_id}"
for i, p in enumerate(paths):
name = f"{base_name}_{i:02d}"
res.import_project_resource(p, usage, name=name)
class ImageImporter(QtWidgets.QLabel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setAcceptDrops(True)
def dragEnterEvent(self, e):
mimeData = e.mimeData()
if not mimeData.hasUrls():
e.ignore()
return
for url in mimeData.urls():
url: str = url.url()
if not url.startswith("file:///"):
continue
e.accept()
return
e.ignore()
def dropEvent(self, e):
mimeData = e.mimeData()
if not mimeData.hasUrls():
return
paths = []
for url in mimeData.urls():
url: str = url.url()
if not url.startswith("file:///"):
continue
path = url[len("file:///") :]
paths.append(path)
import_images(paths)
plugin_widgets = []
def start_plugin():
"""This method is called when the plugin is started."""
# Create a simple text widget
widget = ImageImporter()
widget.setText("Drag images here to import...")
widget.setWindowTitle("Image Importer")
# Add this widget as a dock to the interface
substance_painter.ui.add_dock_widget(widget)
# Store added widget for proper cleanup when stopping the plugin
plugin_widgets.append(widget)
def close_plugin():
"""This method is called when the plugin is stopped."""
# We need to remove all added widgets from the UI.
for widget in plugin_widgets:
substance_painter.ui.delete_ui_element(widget)
plugin_widgets.clear()
if __name__ == "__main__":
start_plugin()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment