Maya M2022 Python 2 custom UI Asset Browser failing to navigate paths from text files correctly?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Ok so I have been working on a custom UI to help people navigate maya files on disc I am using M2022 and using Pyside2 I have created a UI with qtwidgets that consists of 4 tabs, Work, Model Check, Publish, Export.
I have added a large collection of features that where all working well on the Work Tab.
The issues for me started when I wanted to duplicate the contents across to a 2nd tab as the publish tab functions much the same as the work tab just pointing at a new directory.
I decided to set this up so it reads the asset_dir and publish_asset_dir paths from a saved .txt document on my Network drive. So by selecting the Project preference file in a drop down it would then update the paths for Work and Publish tab based on the content of the .txt file
here is the contents of the text file
Project = Metal Work Path = N:\SAFE\Asset_Browser\work\Asset No. of Work Lists = 6 Work list Titles = Type, Category, Asset, LOD, User, Scene Publish Path = N:\SAFE\Asset_Browser\publish\Asset No. of Publish Lists = 7 Publish list Titles = Type2, Category2, Asset2, LOD2, User2, Scene2 Project Pref Loc = N:\SAFEH\TOOLS\Maya\assetBrowser\PROJECTS
So on selecting the project to Metal in the drop down its ment to read the text file and then set the Work path for assets_dir and the publish path for publish_assets_dir
so that each of the tabs display the content in those directories at launch and on changing dropdown.
Here are the bits of script i think are causing the problem as well as some context.
def populate_project_dropdown(combo_box): project_pref_files = glob.glob(r"N:\SAFE\TOOLS\Maya\assetBrowser\PROJECTS\*_project.preference.txt") project_names = [os.path.splitext(os.path.basename(file))[0].split("_")[0] for file in project_pref_files] project_paths = [file for file in project_pref_files] combo_box.clear() combo_box.addItems(project_names) return dict(zip(project_names, project_paths)) # New function to handle changing project preferences def change_project_preferences(project_name, asset_browser): preferences = read_project_preferences(project_name) if preferences: asset_dir = preferences.get("Work Path", "") publish_asset_dir = preferences.get("Publish Path", "") if asset_dir and publish_asset_dir: asset_browser.asset_dir = asset_dir asset_browser.publish_asset_dir = publish_asset_dir asset_browser.initUI() # Update the UI based on new preferences else: print("Asset directory or publish asset directory is not set in preferences.") else: print("Project preferences not found.") class AssetBrowser(QtWidgets.QWidget): def __init__(self, asset_dir, publish_asset_dir, parent=None): super(AssetBrowser, self).__init__(parent) self.asset_dir = asset_dir self.publish_asset_dir = publish_asset_dir self.levels = 6 self.list_titles = ["Type", "Category", "Asset", "LOD", "User", "Scene"] self.file_views = [] self.file_models = [] self.publish_dir = "" # Add a new attribute for publish directory *MAYBE REMOVE self.thumbnail_label = None self.file_info_text = None self.selected_file_path = None self.last_selected_folder_path = None self.layout = None self.init_preferences() # Load preferences on initialization self.initUI() self.file_models = [] # Keep track of created QFileSystemModel instances self.publish_tab = None self.work_tab = None self.project_combo_box.currentIndexChanged.connect(self.changeProjectPreferences) # Fix this line self.changeProjectPreferences() def changeProjectPreferences self.publish_tab = QtWidgets.QWidget() self.tab_widget.addTab(self.publish_tab, "Publish") # Call change_project_preferences to set the initial project preferences change_project_preferences(self.project_combo_box.currentText(), self) # Add content to the tabs self.addWorkTabContent(work_tab, asset_dir) self.addPublishTabContent(publish_tab, publish_asset_dir) # Add content to other tabs if needed # Populate the first view upon launch self.populateDirectory(self.asset_dir, self.file_views[0]) #work tab def addWorkTabContent(self, work_tab, asset_dir): # Add content to the "Work" tab only if it's not already added if work_tab.layout() is None: work_layout = QtWidgets.QVBoxLayout(work_tab) # Add your content here print("Work tab content added") print("Asset Directory:", asset_dir) def populateDirectory(self, directory, view, scale_factor=1): view.model().setRootPath(directory) view.setRootIndex(view.model().index(directory)) #publish tab def addPublishTabContent(self, publish_tab, publish_asset_dir): # Add content to the "Publish" tab only if it's not already added if publish_tab.layout() is None: publish_layout = QtWidgets.QVBoxLayout(publish_tab) # Add your content here print("Publish tab content added") print("Publish Asset Directory:", publish_asset_dir) def populateDirectory(self, directory, view, scale_factor=1): view.model().setRootPath(directory) view.setRootIndex(view.model().index(directory)) # Main entry point -------------------------------------------------------- if __name__ == '__main__': app = QtWidgets.QApplication.instance() or QtWidgets.QApplication(sys.argv) try: # Check Asset Browser status from preferences status = read_preferences('Asset Browser Status') if status == 'Open': # If Asset Browser was open, activate the existing window existing_app = QtWidgets.QApplication.instance() for widget in existing_app.topLevelWidgets(): if isinstance(widget, AssetBrowser): widget.activateWindow() widget.raise_() break else: # If Asset Browser was closed, set the status to Open and show the window # Populate project dropdown list populate_project_dropdown(asset_browser.project_combo_box) # Read asset directories from preferences file asset_dir = read_preferences('Asset Directory') publish_asset_dir = read_preferences('Publish Asset Directory') # Check if the asset directories are retrieved properly if asset_dir is None or publish_asset_dir is None: raise ValueError("Asset directory or publish asset directory is not set in preferences.") # Create the AssetBrowser instance with both asset_dir and publish_asset_dir arguments asset_browser = AssetBrowser(asset_dir, publish_asset_dir) asset_browser.show() write_preferences('Asset Browser Status', 'Open') # Update the status after showing the window
I got it to a point where it kinda reads the path but its now not even launching. im really worried and confused and if im honest im abit new to all this. please help fix my code. im struggling badly and if someone can get this running properly im gonna pony up some coffees for this one! @musicamante
thank you so much for your help, im struggling so badly and cant afford to not get this working. I appreciate your feedback that I presented this wrong the first time so hope this is better? please help me.
Full script here
https://github.com/safeburton/MayaAssetBrowser/blob/5e30b7c968e707d9b01efc9d04166af453dd6d4f/AssetBr...