Why my code not working? Line width switch.

Why my code not working? Line width switch.

amaterasu-qbb
Collaborator Collaborator
408 Views
1 Reply
Message 1 of 2

Why my code not working? Line width switch.

amaterasu-qbb
Collaborator
Collaborator

Hello, I'm making code about switch line width. here's the code.

# -*- coding: utf-8 -*- my code
import maya.cmds as cmds

currentPanel = cmds.getPanel( wf =True )
panelType = cmds.getPanel( to=currentPanel )
if( panelType ==  "modelPanel" ):
	lineWidth = cmds.modelEditor( currentPanel, q=True, lw=True )
	if lineWidth > 1:
		cmds.modelEditor( currentPanel, e=True, lw=4 )
	else:
		cmds.modelEditor( currentPanel, e=True, lw=1 )

To switch anything use if-else right? why it does not work?

image

Line width default 1.0Line width default 1.0Line width thickLine width thick

 

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

Kahylan
Advisor
Advisor

Hi!

 

There are two issues with you code.

 

The first one is rather simple, your if statement is defined wrong

you have it defined as:

if lineWidth > 1:

Which translates to "if lineWidth is bigger than 1"

But from the logic of your program I'm guessing it should be:

if lineWidth <= 1:

"if lineWidth is smaller or equal to 1".
Because your current if statement doesn't catch the default of lw = 1.0.

 

The second issue you have, is that you can only run this script from a shelf button, if that is what you want you're done.
But if you want to run it from the script editor, it will do nothing, because the execute command makes the script editor the panel in Focus.

 

Normally when writing scripts to affect the current viewport, using the "playblast(ae=True)" command is the better option, since it automatically affects the current modelpanel even if some other window is selected. Like this:

import maya.cmds as cmds

currentVP = cmds.playblast(ae=True)
lineWidth = cmds.modelEditor( currentVP, q=True, lw=True )
if lineWidth <= 1.0:
    cmds.modelEditor( currentVP, e=True, lw=4 )
else:
    cmds.modelEditor( currentVP, e=True, lw=1 )

 

I hope this helps!