Not an expert in python but somehow found a way need a little bit of spice up

I am not an expert in maya to know if their is any short cut that can straightaway launch outliner and reveal the object straight away however I know if the outliner is open we can click on Display>Reveal selected, so I though why not make a function that launches the outliner if its not open and revels the selcted object

so I came up with this logic pls dont get angry at me I know this is not the best logic(Pythonically or MayaExpert) but you can tell the better way of doing or a one liner…

import maya.cmds as cmds
import maya.mel as mel

def openReveal():
    itms=cmds.getPanel(vis=True)
    outliners =cmds.getPanel(typ='outlinerPanel')
    print outliners,type(outliners)
    allpanels=cmds.getPanel(vis=True)
    Found=False
    NotFound=False
    for index,each in enumerate(outliners):
        each=str(each)
        if Found== True:
            break
        else:
            print("Found is ",Found)
            cmds.OutlinerWindow()
            #mel.eval('outlinerEditor -e -sc 1  outlinerPanel1;')
        for panl in allpanels:
            panl=str(panl)
            if panl == each:
                #print panl
                RevealSelected(panl)
                print "Found"
                Found=True
                break
            else:
                NotFound=True
                pass

def RevealSelected(panl=None):
    if panl:
        print("panl is ",panl)
        mel.eval('outlinerEditor -e -sc 1 '+panl+ ';')
    

if len(cmds.ls( selection=True ))>0:
    openReveal()
else:
    cmds.OutlinerWindow()

in what case will it not work ?

oh forgot to mention one of the case in whcih a part of it doesnt work i.e. if Outliner is not open then it does launches the outliner but doesnt reveal , above I have commented the #mel.eval(‘outlinerEditor -e -sc 1 outlinerPanel1;’) that should do the job but then I would have to forcibly get outliners[0]'th item…

Been a while since I used python in Maya, but just in terms of code, you can really make it more efficient. Also I’m not entirely sure what your code is meant to achieve.

Does it do this:
>If nothings selected, opens outliner
>If something is selected, it opens the outliner and shows the objects in there?

itms and allpanels makes the same call. Just say itms = allpanels (also i wouldn’t shorten a word by just one letter, makes it annoying for whoever gets it down the line)
Also replace the if Found==True with ‘while Find is True’ and you can really cut down on your code and reorganize it for easier editing, rather than a bunch of if/else statements to affect each other.

Also you have a lot of step forward/step back code segments and repeated code/ideas. From what I understand of your goal, you should just be doing this in this order:

>Check if outliner is open, if it isn’t open it
>Check if anything is selected, if it is, show it in the outliner.

Really you could do that in significantly less steps and much cleaner code.

As for when things will break, if you’re just doing something as simple as revealing, nothing should break, but the purer your ideas, the less likelihood of breaking there is.

you got it right I just minimizing steps not a good direction, to waste time in though I can get good for doing something next time,

apologies I have declared itms but its not being used anywhere , i was testing with this name so i forgot to remove it…

I will give it a try follow the approach of order you mentioned…

I appreciate your input!!!

alright, now a much better code and without break but still with if and for I cant get out these too someday I would take a time out to give while loop a try.


import maya.cmds as cmds
import maya.mel as mel
allpanels=cmds.getPanel(vis=True)
outliners =cmds.getPanel(typ='outlinerPanel')

def openOutliner():
    for each in outliners:
        each=str(each)
        for panl in allpanels:
            panl=str(panl)
            if panl in outliners:
                if panl == each:
                    return panl
            else:
                cmds.OutlinerWindow()
        RevealSelected(outliners[0])

    
    
def RevealSelected(panl=None):
    if panl:
        mel.eval('outlinerEditor -e -sc 1 '+panl+ ';')  
    
panl=openOutliner()
if len(cmds.ls( selection=True ))>0:
    RevealSelected(panl)
else:
    print("Noting to reveal in outliner")

however if the outliner is not open it launches the outliner but doesn’t reveal the selected object even though I am making call to RevealSelected(outliners[0])… but if outliner is open RevealSelected works fine

Your openOutliner function is way more complex than it needs to be.

Just loop through each outlinerPanel and see if it is in the list of visible panels.
If it is return that outliner and call reveal_selected on it.
If none of the existing outlinerPanels are visible (the previous for-loop finishes without returning), call the OutlinerWindow command, and then reveal_selected.
Also outlinerEditor is in the cmds module, so you don’t need to do mel.eval(‘outlinerEditor’)

I noticed something weird with cmds.OutilnerEditor, maybe someone can clear this up for me.

If the outliner is closed and I call cmds.OutlinerWindow() followed by cmds.getPanel(vis=True), the newly created outliner won’t appear in the list of visible panels.
I’ve tried using time.sleep(), but that doesn’t make a difference. If I call cmds.OutlinerWindow() twice before cmds.getPanel(vis=True), then it appears in the list of visible panels.
Anyone know why this is?

Code sample of what I’m talking about:


cmds.OutlinerWindow()
print cmds.getPanel(vis=True) # Does not contain 'outlinerPanel1'
cmds.OutlinerWindow()
print cmds.getPanel(vis=True) # Contains 'outlinerPanel1'

[QUOTE=capper;18057]Your openOutliner function is way more complex than it needs to be.

Just loop through each outlinerPanel and see if it is in the list of visible panels.
If it is return that outliner and call reveal_selected on it.
If none of the existing outlinerPanels are visible (the previous for-loop finishes without returning), call the OutlinerWindow command, and then reveal_selected.
Also outlinerEditor is in the cmds module, so you don’t need to do mel.eval(‘outlinerEditor’)

I noticed something weird with cmds.OutilnerEditor, maybe someone can clear this up for me.

If the outliner is closed and I call cmds.OutlinerWindow() followed by cmds.getPanel(vis=True), the newly created outliner won’t appear in the list of visible panels.
I’ve tried using time.sleep(), but that doesn’t make a difference. If I call cmds.OutlinerWindow() twice before cmds.getPanel(vis=True), then it appears in the list of visible panels.
Anyone know why this is?

Code sample of what I’m talking about:


cmds.OutlinerWindow()
print cmds.getPanel(vis=True) # Does not contain 'outlinerPanel1'
cmds.OutlinerWindow()
print cmds.getPanel(vis=True) # Contains 'outlinerPanel1'

[/QUOTE]I figured out myself, never got update of this new post, I have got 8 lines of code and everything works!!! Its a little thing but it is handy. Now I look forward to build something new…

you dont need to put sleep/or make script wait. Just put the line that reveals object in evalDeffered(~here~) and it will work…
click hereto download script and read instructions…