[PyMEL] Treeview oddity: Bool as stringified integer



Check out this code:

from pymel.core import *

def buttonpress(item, isPressed):
	print "isPressed value is %s and its type is %s" % (isPressed, type(isPressed))

window = window(h=300, w=300)
layout = columnLayout( rowSpacing=5)

control = treeView( parent = layout, 
	w=300, h=300, 
	numberOfButtons = 2, 
	pressCommand=(1, buttonpress) )
showWindow( window )

treeView( control, e=True, addItem = ("layer 1", ""))
treeView( control, e=True, buttonStyle=("layer 1", 1, "2StateButton"))

The isPressed argument i get from the pressCommand-argument is logically a bool value and indicates, wether that button is pressed or released. While all other UI-arguments deliver the integer-values 0 or 1, this one pressCommand actually gives me the bool as a string “0” or “1”:

isPressed value is 1 and its type is <type 'unicode'>

This is more than bizarre, right? I mean this is obviously a bug (in mel, mayapython or pymel)…? Because the documentation states:

Second argument specifies the callback function to be executed the callback function should take as parameters: - a string for the clicked button’s item - an int for the clicked button’s state

This is quite unhandy (not to say “un-pythonesque”), as you can’t go like:

if isPressed:

“1” and “0” are not empty strings, so this is true for both. Has anyone experienced this before?


I believe it is because the buttons in treeView can actually have 3 states, so they won’t work as bools, and instead of just using ints Maya falls back on its favorite notion of just making everything a string.

Also treeView is damned weird.


Yea, that makes sense. These little things sure make me go on wild bug hunts far too often.

Also treeView is damned weird.

True. But that applies to a lot of commands in maya.