If nothing selected

If nothing selected

Anonymous
Not applicable
4,059 Views
4 Replies
Message 1 of 5

If nothing selected

Anonymous
Not applicable

I'm writing a python script and I want to check if there's nothing selected. Si I got this:

 

        if target == None:
            cmds.error("Please select an object")

 

But it doesn't work. Even if nothing is selected, the script continues beyond my if statement. Any idea?

0 Likes
Accepted solutions (1)
4,060 Views
4 Replies
Replies (4)
Message 2 of 5

mspeer
Consultant
Consultant

Hi!

It seems you accidentally did not add the complete script.

No one can say why

if target == None:

is not True for a scene without any selection, without any further data.

 

If the statement is True then it will stop after the error message (tested in  Maya 2019.1)

0 Likes
Message 3 of 5

Anonymous
Not applicable

Here is the entire script.

 

# Fires a curve from the camera to the selected object. By Robert Rioux. Inspired by NavKit by Laurent Taillefer. :-)

import maya.cmds as cmds
target = cmds.ls(sl=True)


class rayProject(object):
   def __init__(self,*args):
       self.rayProjectWin = 'rayProject'
       if cmds.window(self.rayProjectWin, exists=True):
           cmds.deleteUI(self.rayProjectWin)

       self.buildWin()

   def buildWin(self,*args):
       self.rayProject = cmds.window(self.rayProjectWin, t="Ray Project", tlb = True, w=100, h=30, bgc= [0.4,0.4,0.4])


       theRowLayout = cmds.rowColumnLayout(nc=1,columnWidth=(30,1), h=30)
       cmds.button(label="Fire!",  width = 80, command = self.fire)       

       cmds.setParent( theRowLayout )

       # Display the window
       cmds.showWindow(self.rayProjectWin)   



# Project the curve


   def fire(*args):

       target = cmds.ls(sl=True)

       print target

       if target == None:
           #cmds.error("Please select a locator")
           print target
           print "Check if statment"

       else:    
           print "else"    
           transform = maya.cmds.xform( target, q=True, ws=True, t=True )


           pan = cmds.getPanel(wf=True)
           cam = cmds.modelPanel(pan, q=True, camera=True)


           transformCam = maya.cmds.xform( cam, q=True, ws=True, t=True )

           cmds.curve (p=[(transformCam[0],transformCam[1],transformCam[2]),(transform[0],transform[1],transform[2])], d=1) 

           newCurve =  cmds.ls(sl=True)
           cmds.xform (r=True, piv=(transformCam[0],transformCam[1],transformCam[2]))
           cmds.xform (s=(10,10,10))
           cmds.select (target)


rayProject()   
0 Likes
Message 4 of 5

mspeer
Consultant
Consultant
Accepted solution

Hi!

 

This is what i missed:

target = cmds.ls(sl=True)

Please use this for the if statement:

if not target:

 

Message 5 of 5

Anonymous
Not applicable

Great! Thank you. I will give it a try tomorrow. 

0 Likes