how to auto update a testScrollList?

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 10 October 2012   #1
how to auto update a testScrollList?

I'm super new to scripting and had a friend help making my first iteration of this script, which is a floating window that lists all my cameras in the scene. It let me select the camera from the list and also has a button to look through the selected. simple enough. But it never refreshed so when I added a new camera or renamed one I had to relaunch the script. It wasn't an issue at first, I just added a button on the bottom that relaunched the script. It worked for me.

but I was adding more features to it today I added a button that creates a new camera, and I made it so the delete button will work on the list. the problem is that it doesn't show the changes in the list without a relaunch. I tried the refresh() command but it doesn't seem to work, or I don't know where to put it.

Can anyone help me out? I want the list to update when I add, delete, or rename a camera

here is the full script I have so far. I just added the checkbox with the intent later to turn on/off ortho cameras.


#####
# This script makes a GUI with a list of
# your Cameras that allows you to select 
# the camera from the GUI and you can 
# also look through the selected camera
#####


import maya.cmds as mc

camListBox = "camList"

def selectChanged():
    #query selection in list
    #then do select command with selected item in list
    selectCam = mc.textScrollList(camListBox, q=True, selectItem=True)
    mc.select(selectCam[0])

def delSelected():
    #query selection in list
    #then do select command with selected item in list
    #then delete it
    selectCam = mc.textScrollList(camListBox, q=True, selectItem=True)
    mc.delete(selectCam[0])
   
    
def newCam():
    mc.camera()

def camLook():
    selectLook = mc.textScrollList(camListBox, q=True, selectItem=True)
    mc.lookThru(selectLook[0]) # looks through the camera I specify

def initGui():
    #here we will add all cameras in scene to list box
    allCams = mc.listCameras(p=True) # p=True is grabbing the perspective cameras only.
    
    for aCam in allCams:
        mc.textScrollList(camListBox, edit=True, append=aCam)

    if len(mc.textScrollList(camListBox, q=True, allItems=True)):
        mc.textScrollList(camListBox, edit=True, selectIndexedItem=1)
        
def makeWindow(winName):
    
    if mc.window(winName, exists=True):
        mc.deleteUI(winName)
    
    mc.window( winName, width=150 )
    mc.columnLayout( adjustableColumn=True )
    mc.checkBox( label='Show Ortho Cameras' )
    mc.textScrollList(camListBox, deleteKeyCommand="delSelected()", allowMultiSelection=False, selectCommand="selectChanged()")
    mc.button( label='View', command="camLook()" )
    mc.button( label='New', command="newCam()" )
    mc.button( label='Refresh', command="makeWindow(\"CamWindow\")" )
    mc.showWindow()
    
    #fill gui with cameras
    initGui()


makeWindow("CamWindow")
 
Old 10 October 2012   #2
Try this:

#####
# This script makes a GUI with a list of
# your Cameras that allows you to select 
# the camera from the GUI and you can 
# also look through the selected camera
#####


import maya.cmds as mc

camListBox = "camList"

def selectChanged():
	#query selection in list
	#then do select command with selected item in list
	selectCam = mc.textScrollList(camListBox, q=True, selectItem=True)
	mc.select(selectCam[0])

def delSelected():
	#query selection in list
	#then do select command with selected item in list
	#then delete it
	selectCam = mc.textScrollList(camListBox, q=True, selectItem=True)
	mc.delete(selectCam[0])
   
	
def newCam():
	mc.camera()

def camLook():
	selectLook = mc.textScrollList(camListBox, q=True, selectItem=True)
	mc.lookThru(selectLook[0]) # looks through the camera I specify

def initGui():
	#here we will add all cameras in scene to list box
	allCams = mc.listCameras(p=True) # p=True is grabbing the perspective cameras only.
	
	for aCam in allCams:
		mc.textScrollList(camListBox, edit=True, append=aCam)

	if len(mc.textScrollList(camListBox, q=True, allItems=True)):
		mc.textScrollList(camListBox, edit=True, selectIndexedItem=1)
		
def refreshGui():
	# remove all existing items from the textScrollList, then repopulate it with initGui()
	mc.textScrollList(camListBox, edit=True, removeAll=True)
	initGui();
		
def makeWindow(winName):
	
	if mc.window(winName, exists=True):
		mc.deleteUI(winName)
	
	mc.window( winName, width=150 )
	mc.columnLayout( adjustableColumn=True )
	mc.checkBox( label='Show Ortho Cameras' )
	mc.textScrollList(camListBox, deleteKeyCommand="delSelected()", allowMultiSelection=False, selectCommand="selectChanged()")
	mc.button( label='View', command="camLook()" )
	mc.button( label='New', command="newCam()" )
	mc.button( label='Refresh', command="refreshGui()" )
	mc.showWindow()
	
	#fill gui with cameras
	initGui()


makeWindow("CamWindow")


I added a new function refreshGui() which clears out the list of items in the textScrollList and calls initGui() again to repopulate the list. There are more advanced approaches you can use to skip the refresh button altogether and have the list auto update as cameras are created too.

For example:

The scriptJob command lets you create a function that will be invoked by Maya whenever a certain event or condition occurs in Maya. One of these events is DagObjectCreated. This would fire anytime a DAG object is created in the scene. In that function callback, you could filter it for only cameras and then add it to the list dynamically.
 
Old 10 October 2012   #3
Thanks for the better refresh button. I'll take a look at the DagObjectCreated event and see what I can do to implement it. I would definitely like to remove the refresh button and have the list auto update. It's a small script but I think I'm learning a thing or two from it.
 
Old 10 October 2012   #4
Btw, for your Include Ortho Cameras checkbox, that currently looks unimplemented, there should be a -cc/changeCommand flag that you can attach another function to in order to fire an event as the value is toggled. In that function you can check the value of your checkbox and update the list accordingly.

That's probably a good exercise to begin with before getting into the scriptJob.
 
Old 10 October 2012   #5
Thanks. I was just about to look into the ortho checkbox or scriptJob function today. I'll look into the checkbox first.
 
Old 10 October 2012   #6
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 12:19 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.