Problem accessing scrollField in Python

Hi everyone,

I’m still working on a simple sanity check for Maya, and I have a noob question. Everything was going great until I added a menu in Maya to launch my script, and I put my UI creation in a function.
Here is my simplified script to explain my problem :slight_smile:

import pymel.core as pm

###################### GLOBAL VARIABLES #####################

projectVersion = 20180600

###################### CHECK FUNCTIONS ############################

def checkMaya():
version = pm.about(api=True)
if version != projectVersion:
log.insertText(“ERROR* file is not created with MAYA 2018 service pack 6\n”)
else:
log.insertText("OK maya version is " + str(projectVersion) + “\n”)

########################### UI ###########################

def baguetteUI():

with pm.window('win', title="Sanity Check", width=400, height=700, sizeable=0):
    with pm.frameLayout(lv=False):

        pm.text(label="SCENE MANAGMENT", fn="boldLabelFont", ebg=True, bgc=(0.2,0.2,0.2), h=20)

        with pm.rowLayout(numberOfColumns=2, cw=(1,350)):
            versionCheckbox = pm.checkBox(label="Maya version Check", value=1)
            pm.button(label="Check", command = pm.Callback( checkMaya ) )

        with pm.autoLayout():
            log = pm.scrollField(ed=False)  

baguetteUI()

So now that my UI is in a function I can’t access “log”, which is the scrollfield instance ( I have the same problem with the checkboxes actually ). I have no idea how to deal with this. I tried to return a string from my functions,also tried to switch to a class, but I always face the same problem at some point when I want to update the ScrollField. Any suggestion?

Thanks !!!

UI elements created in a function will be local to the function unless the result of the creation function is assigned to a variable outside of the function. In a class based Ui this is typically assigned to an attribute defined in the __init__() constructor like self.ui_element

import pymel.core as pm

PROJECT_VERSION=20180600 # all caps is the common convention for constants

class BaguetteUI():

def __init__(self):
    self.scrollfield_log=None
    self.checkbox_version=None
    self.create_window()

def create_window(self):

    with pm.window('win', title="Sanity Check", width=400, height=700, sizeable=0):
        with pm.frameLayout(lv=False):
            pm.text(label="SCENE MANAGMENT", fn="boldLabelFont", ebg=True, bgc=(0.2, 0.2, 0.2), h=20)

            with pm.rowLayout(numberOfColumns=2, cw=(1, 350)):
                self.checkbox_version= pm.checkBox(label="Maya version Check", value=1)
                pm.button(label="Check", command=pm.Callback(self.checkMaya))

            with pm.autoLayout():
                self.scrollfield_log= pm.scrollField(ed=False)

def checkMaya(self):
    version = pm.about(api=True)
    if version != PROJECT_VERSION:
        self.log.insertText("ERROR * file is not createwith MAYA 2018 service pack 6\n")
    else:
        self.log.insertText("OK maya version is {}\n".format(PROJECT_VERSION))

UI=BaguetteUI()

On a related note, i’ve got an old blog post which talks about the mechanics of this kind of stuff in detail here:

2 Likes