Porting out Pyside UI code to separate modules

Deleted the other thread, accidentally posted in the wrong forum.

Hello all,

I’m wanting to port out PySide code(UI Code only) so that I have separate modules, but can’t seem to get to where I need to be on it.
My main desire is to just have the main_ui.py be the central script that grabs everything from, but I can’t seem to figure out how to inherit the Widget information from the other code.
Essentially, there is QWidget code in librariesThatIWantToReach.py and I want main_ui.py to read that code.

I currently have this folder structure:
MainPackageFolder
-init.py
–libs(folder)
----init.py
----librariesThatIWantToReach.py
–ui(folder)
----init.py
----main_ui.py

I’m using the main_ui.py as the main hub for everything, as being my main PySide QWidget instance.
This is my main_ui.py code(trimmed down to the necessities)



import libs.awRenamer as AWR
import libs.awMirrorObj as AMO

def showui():
    main_window = maya.OpenMayaUI.MQtUtil.mainWindow()
    return wrapInstance(long(main_window), QWidget)

class AWTools_UI(QDialog):
    """
    Words
    """
    def __init__(self, parent=showui()):
        super(AWTools_UI, self).__init__(parent)

    def start(self):
        self.setWindowTitle('Alex Rigging Tools')
        self.setObjectName('AlexRiggingTools')
        self.setMinimumSize(400, 750)
        self.setMaximumSize(400, 750)
        self.setWindowFlags(QtCore.Qt.Tool)

        self._layout()
        self._connections()



And in the other code(libraries that I want to reach), I just have the main functions like


def searchReplace(searchFor, replaceWith, currentSelection):
    for selected in currentSelection:
        name = selected.replace(searchFor, replaceWith)
        pmc.rename(selected, name)

But like I said above, I want the UI code to be in there as well and I want the QWidget instance from main_ui.py to pull in any information from an instance in librariesThatIWantToReach.py.
I’ve tried a bunch of stuff, but can’t seem to figure something out and haven’t found any examples doing what I’d like to do.
Hopefully this made sense. Thanks.

Edit: I think I came up with a better way of explaining:

I have the UI code for each tool segment in the module that it’s supposed to be in. I want to pull from that code and assign it to an object like a QHBoxLayout or a QWidget(not sure of what would work yet) that I can then assign to a position in the UI in the main_ui.py code.

I’m still not clear on the problem or the desired behavior. I will offer this though:

ui code should live in one place and only deal with ui. If you have a QMainWindow subclass, any method in there that doesn’t affect the ui needs to go. Import your business logic and call functions from those modules.

A good rule is never import PySide outside of your ui tools. I guess my only exception might be for QMessageBox dialog, but that is because im lazy :slight_smile:

Okay. I’ll update again this evening after work. Thanks.

So, I’m basically just creating a giant toolset of all the tools that I’ve created over the years and it’s going to be more than the regular 20-30 lines of PySide that a standard tool needs.

So the reason that I want to have the PySide in separate areas is that I didn’t want to have one main_ui.py file where I have thousands of lines of UI code - basically for the sake of keeping it moderately neat and not just a wall of UI code.
But the other reason that I wanted to port it out is that I wanted to keep all the functions that get values of UI-related fields outside of the UI code(again, for cleanliness of the UI code), but it doesn’t seem that’s feasible.

I have already done what you’re saying of Import your business logic and call functions -
For instance, the UI.py file


import libs.awRenamer as AWR
import libs.awMirrorObj as AMO

....... 

    def _connections(self):
        self.rename_searchReplaceButton.clicked.connect(lambda: self.searchAndReplace())
        self.rename_prefixButton.clicked.connect(lambda: self.addPrefix())
        self.rename_suffixButton.clicked.connect(lambda: self.addSuffix())
        self.rename_renameButton.clicked.connect(lambda: self.renameItems())

    def renameItems(self):
        strRename  = self.getFieldContents(self.rename_renameField)
        startNum = self.getFieldContents(self.rename_startField)
        paddingNum = self.getFieldContents(self.rename_paddingField)

        AWR.rename(strRename, startNum, paddingNum, self.getSelection())

    def getSelection(self):
        return pmc.ls(sl=True)
.... and so on

And then in the modules that I’m importing (this is awRenamer, for instance), I have these kind of functions:


def searchReplace(searchFor, replaceWith, currentSelection):
    for selected in currentSelection:
        name = selected.replace(searchFor, replaceWith)
        pmc.rename(selected, name)

def prefix(strPrefix, currentSelection):
    for selected in currentSelection:
        print(type(selected))
        print(type(strPrefix))
        name = strPrefix + selected
        pmc.rename(selected, name)

def suffix(strSuffix, currentSelection):
    for selected in currentSelection:
        name = selected + strSuffix
        pmc.rename(selected, name)

So in summation, I basically just thought it might be a better way to keep the code separate and clean, but it appears that it’s not feasible or advised. Ok! :slight_smile: Thanks!

are you getting import errors? Why are you unable to accomplish what you want?

it is totally possible and very much advised to keep GUI code separate from Maya code.

glancing at your sample code, you still have pymel calls in your GUI code…

[QUOTE=rgkovach123;27697]it is totally possible and very much advised to keep GUI code separate from Maya code.

glancing at your sample code, you still have pymel calls in your GUI code…[/QUOTE]

Hi, I know it’s possible to keep the GUI code separate from the Maya code - that part I had no trouble with. I was wanting to separate out UI code to separate segments because the UI code is going to get VERY big. I’m putting somewhere around 50 tools that I’ve built into this one UI and didn’t want to have one HUGE ui file that I would have to rummage through in an IDE. But, as TheMaxx says, it’s just ill-advised to split UI code out.
And the PyMel code is just because I hadn’t moved that function over yet from the UI code. I started with the UI as a building block and am moving code out. I’m also going to have other general libraries that do stuff like the getCurrentSelection() in their own file that gets imported into the space so it’s accessible by every function.

No import errors, I was just trying to illustrate what my original post was for, since you said you were unclear on the desired behavior.
Basically, I wanted my code to behave the exact same as if it were one file, but have the UI ported to different files that I could keep track of easier(because I’m putting dozens of tools into one layout space and the UI file alone would get huge) - but you advised against that so I’m not going to! :slight_smile:

I think i see. I have a bunch of custom windows, dialogs and widgets that I use and they are all separate files. For example a Rollout widget. I use it everywhere in all sort of windows, but that is a widget in its own file that handles its own logic. Then just use it like a qwidget.

If your ui file would be massive, i would imagine the ui itself would be massive as well. If its just a collection of windows and dialogs, then there now reason those can’t be in their own files (and they should be).