Run different command depending on whether object or comp is selected

Run different command depending on whether object or comp is selected

The-Digital-Shaman
Advocate Advocate
235 Views
1 Reply
Message 1 of 2

Run different command depending on whether object or comp is selected

The-Digital-Shaman
Advocate
Advocate

Hi there,

 

Conditionals and expressions are a bit ahead of me in understanding MEL and Python.

 

I have an idea which would help me reduce number of shortcuts, if I would just put more than a single script under a single button/hotkey by combining them and run depending on mode I am in, like component/object mode.

 

For example. Moving pivot to the selected component pivot position and matching pivot to the last selected object, could be under the same hotkey.

These in my case are two completely different scripts, but with conditional whether object or component is selected, they could be combined, no? 

 

Could someone point me at a good code example which implemented such thing?

 

Many thanks,

DS

0 Likes
236 Views
1 Reply
Reply (1)
Message 2 of 2

Kahylan
Advisor
Advisor

Hi!

 

Well, yes and no...

 

The problem with this is, that you also can have objects and components selected at the same time. And you would have to know what you want to do in that case.

Here is an example of checking wether just a single component is in your selection and running the component script if there is.

import maya.cmds as mc

def executePivotScript():
    sel = mc.ls(sl= True, fl = True)
    component = False
    for s in sel:
        if "." in s:
            component = True
            break
    
    if component == True:
        componentModeScript()
    else:
        objectModeScript()

def objectModeScript():
    """
    Put your object mode script in this function
    """

def componentModeScript():
    """
    Put your component mode script in this function
    """
    
executePivotScript()

Ofcourse you could also use filterExpand() to search for a specific componenttype or check if there is a full object selected, depending on your needs.

 

I hope it helps

0 Likes