def getObj(*args):
#get selection and put it in the widgets["objTFG"]
clearList()
sel = cmds.ls(sl=True, type="transform")
if (sel and (len(sel)==1)):
cmds.textFieldGrp(widgets["objTFG"], e=True, tx=sel[0])
else:
cmds.warning("you must select one object with the \"follow\" attribute")
#------------maybe add attr onto end of obj text, then you don't have to get it later if you needed to ???
#now create a button for each value in the "follow" attr
#channels = cmds.channelBox ('mainChannelBox', query=True, selectedMainAttributes=True)
enumValueStr = cmds.attributeQuery("follow", node=sel[0], listEnum=True)[0]
values = enumValueStr.split(":")
for i in range(0,len(values)):
#pick a random color?
r = random.uniform(0.5,1)
g = random.uniform(0.5,1)
b = random.uniform(0.5,1)
color = (r, g, b)
#here create the button
cmds.button(l=values[i], w=125, p=widgets["bottomRCLO"], bgc=color, h=50, c=partial(switchMatchSpace, i))
python类channelBox()的实例源码
def getAttr(*args):
"""grabs the selected channel from the selected obj and puts the enum values into the list"""
#--------here could require a channel of a specific name, then you could do it automagically (check for "follow", "spaces", "space", "ss", etc)
obj = cmds.textFieldGrp(widgets["objTFG"], q=True, tx=True)
cmds.select(obj, r=True)
channels = cmds.channelBox ('mainChannelBox', query=True, selectedMainAttributes=True)
print channels
if (channels and (len(channels)==1)):
if (cmds.attributeQuery(channels[0], node=obj, enum=True)):
enumValue = cmds.attributeQuery(channels[0], node=obj, listEnum=True)
values = enumValue[0].split(":")
for value in values:
cmds.textScrollList(widgets["spacesTSL"], e=True, append=value)
#----------create a button for each one???
#----------or have them be double clicked???
else:
cmds.warning("select only the enum space switch channel")
def channelbox_command_expression(box, menuItem, key, *args):
with sysCmd.Undo(0):
main_obj_list = cmds.channelBox(box.channelbox, q=1, mainObjectList=1)
main_attr_list = cmds.channelBox(box.channelbox, q=1, selectedMainAttributes=1)
shape_obj_list = cmds.channelBox(box.channelbox, q=1, shapeObjectList=1)
shape_attr_list = cmds.channelBox(box.channelbox, q=1, selectedShapeAttributes=1)
history_obj_list = cmds.channelBox(box.channelbox, q=1, historyObjectList=1)
history_attr_list = cmds.channelBox(box.channelbox, q=1, selectedHistoryAttributes=1)
output_obj_list = cmds.channelBox(box.channelbox, q=1, outputObjectList=1)
output_attr_list = cmds.channelBox(box.channelbox, q=1, selectedOutputAttributes=1)
if main_obj_list and main_attr_list:
mel.eval("expressionEditor \"EE\" " + main_obj_list[0] + " " + main_attr_list[0] + ";")
elif shape_obj_list and shape_attr_list:
mel.eval("expressionEditor \"EE\" " + shape_obj_list[0] + " " + shape_attr_list[0] + ";")
elif history_obj_list and history_attr_list:
mel.eval("expressionEditor \"EE\" " + history_obj_list[0] + " " + history_attr_list[0] + ";")
elif output_obj_list and output_attr_list:
mel.eval("expressionEditor \"EE\" " + output_obj_list[0] + " " + output_attr_list[0] + ";")
def channelbox_command_setSpeed(box, menuItem, key, *args):
# Sets the incremental size for attributes when modifying with middle mouse,
# eg 'medium' appends +1/-1, 'fast' is +10/-10
with sysCmd.Undo(0):
state = 1 if key == "speedSlow" else 2 if key == "speedMedium" else 3
if state == 1:
cmds.channelBox(box.channelbox, e=1, speed=0.1)
elif state == 2:
cmds.channelBox(box.channelbox, e=1, speed=1)
else:
cmds.channelBox(box.channelbox, e=1, speed=10)
box.saved_states["speedState"][0] = state
if box.saved_states["showIcons"][0]:
channelbox_command_Symbol_update(box, "speedState")
sysCmd.channelbox_save_state(box)
def channelbox_command_setManip(box, menuItem, key, *args):
# Set manipulator display/update type based on selected attr in channel box
with sysCmd.Undo(0):
state = 1 if key == "noManips" else 2 if key == "invisibleManips" else 3
if state == 1:
cmds.channelBox(box.channelbox, e=1, useManips="none")
elif state == 2:
cmds.channelBox(box.channelbox, e=1, useManips="invisible")
else:
cmds.channelBox(box.channelbox, e=1, useManips="standard")
box.saved_states["manipsState"][0] = state
if box.saved_states["showIcons"][0]:
channelbox_command_Symbol_update(box, "manipsState")
sysCmd.channelbox_save_state(box)
def channelbox_command_precision(box, menuItem, key, *args):
# floating point value displayed in channel box, eg. value of 5 will
# display 5 decimal places
with sysCmd.Undo(0):
old_precision = box.saved_states[key][0]
new_precision = mel.eval("precisionPrompt (\"\", " + str(old_precision) + ", 15);")
if new_precision > 0: # Change widths of the fields depending on the precision
if new_precision <= 3:
new_width = 65
elif new_precision <= 6:
new_width = 95
elif new_precision <= 9:
new_width = 115
elif new_precision <= 12:
new_width = 130
else:
new_width = 155
cmds.channelBox(box.channelbox, e=1, pre=new_precision, fieldWidth=new_width)
box.saved_states[key][0] = new_precision
box.saved_states["fieldWidth"][0] = new_width
sysCmd.channelbox_save_state(box)
def channelBox_SelectedPlugs(box):
result = []
def loop(which, _result):
obj_list = cmds.channelBox(box.channelbox, q=1, mainObjectList=which[0], shapeObjectList=which[1],
historyObjectList=which[2], outputObjectList=which[3])
attr_list = cmds.channelBox(box.channelbox, q=1, selectedMainAttributes=which[0],
selectedShapeAttributes=which[1], selectedHistoryAttributes=which[2],
selectedOutputAttributes=which[3])
if obj_list and attr_list:
for obj in obj_list:
for attr in attr_list:
_result.append(obj + "." + attr)
return _result
result = loop([1, 0, 0, 0], result)
result = loop([0, 1, 0, 0], result)
result = loop([0, 0, 1, 0], result)
result = loop([0, 0, 0, 1], result)
return result
# -----------------------------------------
def getSelectedChannels():
'''
Return channels that are selected in the channelbox
'''
if not mc.ls(sl=True):
return
gChannelBoxName = mm.eval('$temp=$gChannelBoxName')
sma = mc.channelBox(gChannelBoxName, query=True, sma=True)
ssa = mc.channelBox(gChannelBoxName, query=True, ssa=True)
sha = mc.channelBox(gChannelBoxName, query=True, sha=True)
channels = list()
if sma:
channels.extend(sma)
if ssa:
channels.extend(ssa)
if sha:
channels.extend(sha)
return channels
def _populateSelectionField(self, channel, field, *args):
selectedChannels = None
if channel:
selectedChannels = getSelectedChannels()
if not selectedChannels:
raise RuntimeError('Please select an attribute in the channelBox.')
if len(selectedChannels) > 1:
raise RuntimeError('Please select only one attribute.')
sel = mc.ls(sl=True)
if not sel:
raise RuntimeError('Please select a node.')
if len(sel) > 1:
raise RuntimeError('Please select only one node.')
selection = sel[0]
if selectedChannels:
selection = selection+'.'+selectedChannels[0]
mc.textFieldButtonGrp(field, edit=True, text=selection)
def _populateSelectionList(self, channel, control, *args):
selectedChannels = None
if channel:
selectedChannels = getSelectedChannels()
if not selectedChannels:
raise RuntimeError('Please select an attribute in the channelBox.')
if len(selectedChannels) > 1:
raise RuntimeError('Please select only one attribute.')
sel = mc.ls(sl=True)
if not sel:
raise RuntimeError('Please select a node.')
if len(sel) > 1:
raise RuntimeError('Please select only one node.')
selection = sel[0]
if selectedChannels:
selection = selection+'.'+selectedChannels[0]
mc.textScrollList(control, edit=True, append=[selection])
def setChannelBoxAtTop(channelBox, value):
"""
:param channelBox: mainChannelBox
:type channelBox: str
:param value:
:type value: bool
:example::
setChannelBoxAtTop("mainChannelBox",True)
"""
cmds.channelBox(channelBox, edit=True, containerAtTop=value)
def setChannelShowType(channelBox, value):
"""
:param channelBox: mainChannelBox
:type channelBox: str
:param value:
:type value: str
:example::
setChannelShowType("mainChannelBox", "all")
"""
cmds.optionVar(stringValue=("cbShowType", value))
cmds.channelBox(channelBox, edit=True, update=True)
def read_selected_channels(*args):
"""Return a list of selected channels in the Channel Box"""
channelbox = mel.eval("global string $gChannelBoxName; "
"$temp=$gChannelBoxName;")
return cmds.channelBox(channelbox,
query=True,
selectedMainAttributes=True) or []
def getChannels(*args):
cBox = mel.eval('$temp=$gChannelBoxName')
cAttrs = cmds.channelBox(cBox, q=True, selectedMainAttributes=True, ssa=True, sha=True, soa=True)
return cAttrs
def transfer_attrs(*args):
"""
transfers attrs and connections from second obj to first object selected
"""
tgt, src = get_source_and_targets()
if not tgt or len(src) > 1:
cmds.warning("Select only one target then one source obj to transfer the attributes and connections!")
return ()
attrs = cmds.channelBox('mainChannelBox', q=True, selectedMainAttributes=True)
if not attrs:
cmds.warning("You have to select at least one attr on last object selected to transfer!")
return ()
for attr in attrs:
attrType, hasMin, attrMin, hasMax, attrMax, value, inConnection, outConnection, locked = get_channel_attributes(
src[0], attr)
if not attrType == "enum":
# create attribute
if not cmds.attributeQuery(attr, node=tgt, exists=True):
if hasMin and not hasMax:
cmds.addAttr(tgt, ln=attr, at=attrType, min=attrMin[0], dv=value, k=True)
elif hasMax and not hasMin:
cmds.addAttr(tgt, ln=attr, at=attrType, max=attrMax[0], dv=value, k=True)
elif hasMin and hasMax:
cmds.addAttr(tgt, ln=attr, at=attrType, min=attrMin[0], max=attrMax[0], dv=value, k=True)
else:
cmds.addAttr(tgt, ln=attr, at=attrType, dv=value, k=True)
else:
cmds.warning("The attribute: {0} already exists. Skipping creation!".format(attr))
# lock
if locked:
cmds.setAttr("{0}.{1}".format(tgt, attr), l=True)
else:
cmds.warning("I don't do enums at the moment!")
# connect tgt attr to connection, forced
if inConnection:
cmds.connectAttr(inConnection[0], "{0}.{1}".format(tgt, attr))
if outConnection:
for conn in outConnection:
cmds.connectAttr("{0}.{1}".format(tgt, attr), conn, force=True)
def get_selected_channel(tfbg, *args):
"""
gets the selected channel of the selected objects
tfbg = the key of the widget for the ui (from widgets dict). string
"""
obj = ""
channel = ""
cBox = mel.eval('$temp=$gChannelBoxName')
sel = cmds.ls(sl=True, l=True)
if sel:
if not len(sel) == 1:
cmds.warning("You have to select ONE node!")
else:
obj = sel[0]
else:
cmds.warning("You have to select ONE node!")
if sel:
channels = cmds.channelBox(cBox, q=True, sma=True, ssa=True, sha=True, soa=True)
if channels:
if not len(channels) == 1:
cmds.warning("You have to select ONE channel!")
else:
channel = channels[0]
else:
cmds.warning("You have to select ONE channel!")
if obj and channel:
full = "%s.%s" % (obj, channel)
cmds.textFieldButtonGrp(widgets[tfbg], e=True, tx=full)
def channelbox_menu_selected_channels(box):
obj_list = cmds.channelBox(box.channelbox, q=1, mainObjectList=1)
attr_list = cmds.channelBox(box.channelbox, q=1, selectedMainAttributes=1)
if obj_list and attr_list and len(obj_list) > 0 and len(attr_list) > 0:
return 1
obj_list = cmds.channelBox(box.channelbox, q=1, shapeObjectList=1)
attr_list = cmds.channelBox(box.channelbox, q=1, selectedShapeAttributes=1)
if obj_list and attr_list and len(obj_list) > 0 and len(attr_list) > 0:
return 1
obj_list = cmds.channelBox(box.channelbox, q=1, historyObjectList=1)
attr_list = cmds.channelBox(box.channelbox, q=1, selectedHistoryAttributes=1)
if obj_list and attr_list and len(obj_list) > 0 and len(attr_list) > 0:
return 1
obj_list = cmds.channelBox(box.channelbox, q=1, outputObjectList=1)
attr_list = cmds.channelBox(box.channelbox, q=1, selectedOutputAttributes=1)
if obj_list and attr_list and len(obj_list) > 0 and len(attr_list) > 0:
return 1
return 0
def channelbox_command_keyItem(box, menuItem, key, *args):
with sysCmd.Undo():
cmds.channelBox(box.channelbox, e=1,
exe=("if( `getAttr -k \"#P.#A\"`||`getAttr -channelBox \"#P.#A\"` )setKeyframe \"#P.#A\";", 1))
def channelbox_command_keyAll(box, menuItem, key, *args):
with sysCmd.Undo():
cmds.channelBox(box.channelbox, e=1, exe=("if( `getAttr -k \"#P.#A\"` ) setKeyframe \"#P.#A\";", 0))
def channelbox_command_breakdown(box, menuItem, key, *args):
with sysCmd.Undo():
cmds.channelBox(box.channelbox, e=1, exe=("setKeyframe -breakdown true \"#P.#A\";", 1))
def channelbox_command_breakdownAll(box, menuItem, key, *args):
with sysCmd.Undo():
cmds.channelBox(box.channelbox, e=1, exe=("setKeyframe -breakdown true \"#P.#A\";", 0))
def channelbox_command_muteAll(box, menuItem, key, *args):
with sysCmd.Undo():
cmds.channelBox(box.channelbox, e=1, exe=("mute \"#P.#A\";", 0))
def channelbox_command_unmute(box, menuItem, key, *args):
with sysCmd.Undo():
cmds.channelBox(box.channelbox, e=1, exe=("mute -disable -force \"#P.#A\";", 1))
def channelbox_command_unmuteAll(box, menuItem, key, *args):
with sysCmd.Undo():
cmds.channelBox(box.channelbox, e=1, exe=("mute -disable -force \"#P.#A\";", 0))
def channelbox_command_syncGraph(box, menuItem, key, *args):
with sysCmd.Undo(0):
state = channelBox_Checkbox_Update(box, key, menuItem)
if not state and box.menu_jobs[key] > 0:
# if user disabled the option, and ScriptJob is running, kill the ScriptJob
cmds.scriptJob(k=box.menu_jobs[key])
box.menu_jobs[key] = -1
if state:
mel.eval("GraphEditor;") # open graph editor
cmds.channelBox(box.channelbox, e=1, exe=(channelbox_command_animCurve(box, menuItem, key), 0))
if box.menu_jobs[key] < 0: # if ScriptJob is not running, start it
box.menu_jobs[key] = cmds.scriptJob(
event=("ChannelBoxLabelSelected", partial(channelbox_command_syncGraph_scriptJob, box)),
parent=box.channelbox)
def channelbox_command_cut(box, menuItem, key, *args):
with sysCmd.Undo():
cmd = ""
okay = 0
def loop(which, _cmd, _okay):
obj_list = cmds.channelBox(box.channelbox, q=1, mainObjectList=which[0], shapeObjectList=which[1],
historyObjectList=which[2], outputObjectList=which[3])
attr_list = cmds.channelBox(box.channelbox, q=1, selectedMainAttributes=which[0],
selectedShapeAttributes=which[1], selectedHistoryAttributes=which[2],
selectedOutputAttributes=which[3])
if obj_list and attr_list:
_cmd += "cutKey -t \":\" -f \":"
for channel in attr_list:
_cmd += "\" -at \"" + channel
for obj in obj_list:
_cmd += "\" " + obj
_cmd += ";"
_okay = 1
return _cmd, _okay
cmd, okay = loop([1, 0, 0, 0], cmd, okay)
cmd, okay = loop([0, 1, 0, 0], cmd, okay)
cmd, okay = loop([0, 0, 1, 0], cmd, okay)
cmd, okay = loop([0, 0, 0, 1], cmd, okay)
if okay == 1:
print cmd
print "// Result: " + str(mel.eval(cmd)) + " //"
def channelbox_command_paste(box, menuItem, key, *args):
with sysCmd.Undo():
cmd = ""
okay = 0
current_time = cmds.currentTime(q=1)
def loop(which, _cmd, _okay):
obj_list = cmds.channelBox(box.channelbox, q=1, mainObjectList=which[0], shapeObjectList=which[1],
historyObjectList=which[2], outputObjectList=which[3])
attr_list = cmds.channelBox(box.channelbox, q=1, selectedMainAttributes=which[0],
selectedShapeAttributes=which[1], selectedHistoryAttributes=which[2],
selectedOutputAttributes=which[3])
if obj_list and attr_list:
_cmd += "pasteKey -connect true -time " + str(current_time) + " "
for channel in attr_list:
_cmd += "-at \"" + channel + "\" "
for obj in obj_list:
_cmd += obj
_cmd += ";"
_okay = 1
return _cmd, _okay
cmd, okay = loop([1, 0, 0, 0], cmd, okay)
cmd, okay = loop([0, 1, 0, 0], cmd, okay)
cmd, okay = loop([0, 0, 1, 0], cmd, okay)
cmd, okay = loop([0, 0, 0, 1], cmd, okay)
if okay == 1:
print cmd
print "// Result: " + str(mel.eval(cmd)) + " //"
def channelbox_command_delete(box, menuItem, key, *args):
with sysCmd.Undo():
cmd = ""
okay = 0
def loop(which, _cmd, _okay):
obj_list = cmds.channelBox(box.channelbox, q=1, mainObjectList=which[0], shapeObjectList=which[1],
historyObjectList=which[2], outputObjectList=which[3])
attr_list = cmds.channelBox(box.channelbox, q=1, selectedMainAttributes=which[0],
selectedShapeAttributes=which[1], selectedHistoryAttributes=which[2],
selectedOutputAttributes=which[3])
if obj_list and attr_list:
_cmd += "cutKey -cl -t \":\" -f \":"
for channel in attr_list:
_cmd += "\" -at \"" + channel
for obj in obj_list:
_cmd += "\" " + obj
_cmd += ";"
_okay = 1
return _cmd, _okay
cmd, okay = loop([1, 0, 0, 0], cmd, okay)
cmd, okay = loop([0, 1, 0, 0], cmd, okay)
cmd, okay = loop([0, 0, 1, 0], cmd, okay)
cmd, okay = loop([0, 0, 0, 1], cmd, okay)
if okay == 1:
print cmd
print "// Result: " + str(mel.eval(cmd)) + " //"
# --
def channelbox_command_unkeyable(box, menuItem, key, *args):
cmds.channelBox(box.channelbox, e=1, exe=("setAttr -keyable false -channelBox false \"#P.#A\";", 1))
def channelbox_command_lockUnkeyable(box, menuItem, key, *args):
cmds.channelBox(box.channelbox, e=1, exe=("setAttr -lock true -keyable false -channelBox false \"#P.#A\";", 1))