def isrUI(*args):
if cmds.window("irnWin", exists=True):
cmds.deleteUI("irnWin")
widgets["win"] = cmds.window("irnWin", t="zbw_insertRandomNoise", w=225, h=100)
widgets["CLO"] = cmds.columnLayout()
cmds.text(
"select the controls you want to add random\n motion to. This will add a group above \nand some attrs on the controls",
al="left")
cmds.separator(h=10)
widgets["offsetCBG"] = cmds.checkBoxGrp(l="Offset randomize?", v1=True, cw=[(1, 125), (2, 50)],
cal=[(1, "left"), (2, "left")],
cc=partial(toggleOnOff, "offsetCBG", "offsetIFG"))
widgets["offsetIFG"] = cmds.intFieldGrp(l="Offset Min/Max:", numberOfFields=2, cw=[(1, 125), (2, 50), (3, 50)],
cal=[(1, "left"), (2, "left"), (3, "left")], v1=-200, v2=200)
widgets["speedCBG"] = cmds.checkBoxGrp(l="Speed randomize?", v1=True, cw=[(1, 125), (2, 50)],
cal=[(1, "left"), (2, "left")],
cc=partial(toggleOnOff, "speedCBG", "speedFFG"))
widgets["speedFFG"] = cmds.floatFieldGrp(l="Speed Min/Max:", numberOfFields=2, cw=[(1, 125), (2, 50), (3, 50)],
cal=[(1, "left"), (2, "left"), (3, "left")], v1=-2, v2=2, pre=2,
cc=partial(limitFloatField, "speedFFG", -10, 10))
widgets["ampCBG"] = cmds.checkBoxGrp(l="Amplitude randomize?:", v1=True, cw=[(1, 125), (2, 50)],
cal=[(1, "left"), (2, "left")], cc=partial(toggleOnOff, "ampCBG", "ampFFG"))
widgets["ampFFG"] = cmds.floatFieldGrp(l="Amplitude Min/Max", numberOfFields=2, cw=[(1, 125), (2, 50), (3, 50)],
cal=[(1, "left"), (2, "left"), (3, "left")], v1=.5, v2=1.5, pre=2,
cc=partial(limitFloatField, "ampFFG", -10, 10))
widgets["noiseCBG"] = cmds.checkBoxGrp(l="Noise randomize?:", v1=True, cw=[(1, 125), (2, 50)],
cal=[(1, "left"), (2, "left")],
cc=partial(toggleOnOff, "noiseCBG", "noiseFFG"))
widgets["noiseFFG"] = cmds.floatFieldGrp(l="Noise Min/Max", numberOfFields=2, cw=[(1, 125), (2, 50), (3, 50)],
cal=[(1, "left"), (2, "left"), (3, "left")], v1=.1, v2=.3, pre=2,
cc=partial(limitFloatField, "noiseFFG", 0, 1))
widgets["freqCBG"] = cmds.checkBoxGrp(l="Noise Freq randomize?:", v1=True, cw=[(1, 125), (2, 50)],
cal=[(1, "left"), (2, "left")], cc=partial(toggleOnOff, "freqCBG", "freqFFG"))
widgets["freqFFG"] = cmds.floatFieldGrp(l="Noise Freq Min/Max", numberOfFields=2, cw=[(1, 125), (2, 50), (3, 50)],
cal=[(1, "left"), (2, "left"), (3, "left")], v1=0, v2=.25, pre=2,
cc=partial(limitFloatField, "freqFFG", 0, 1))
cmds.separator(h=5)
cmds.separator(h=10)
widgets["but"] = cmds.button(l="add to selected control", w=225, h=40, bgc=(.5, .7, .5), c=irnDo)
cmds.window(widgets["win"], e=True, wh=(5, 5), rtf=True)
cmds.showWindow(widgets["win"])
python类group()的实例源码
def rebuild_curves(*args):
"""
rebuilds selected curves to specs in window
"""
sel = cmds.ls(sl=True, exactType = "transform")
check = False
newCrvs = []
if sel:
for x in sel:
check = rig.isType(x, "nurbsCurve")
if check:
crv = x
parent = ""
parList = cmds.listRelatives(crv, parent = True)
if parList:
parent = parList[0]
num = calculate_pts(crv)
newCrv = cmds.rebuildCurve(crv, rebuildType = 0, spans = num, keepRange = 0, replaceOriginal=False, name = "{0}_RB".format(crv))[0]
newCrvs.append(newCrv)
if cmds.objExists("crvRebuildOriginals_GRP"):
if (parent and parent != "crvRebuildOriginals_GRP"):
cmds.parent(newCrv, parent)
if parent != "crvRebuildOriginals_GRP":
cmds.parent(crv, "crvRebuildOriginals_GRP")
cmds.setAttr("{0}.v".format(crv), 0)
else:
cmds.group(empty = True, name = "crvRebuildOriginals_GRP")
if (parent and parent != "crvRebuildOriginals_GRP"):
cmds.parent(newCrv, parent)
if parent != "crvRebuildOriginals_GRP":
cmds.parent(crv, "crvRebuildOriginals_GRP")
cmds.setAttr("{0}.v".format(crv), 0)
else:
cmds.warning("{0} is not a nurbsCurve object. Skipping!".format(x))
cmds.select(newCrvs, r=True)
def extendPoly(*args):
"""does the polyextension by grabbing the curve, offsetting it and then lofting. Then converts the nurbs surface to polys"""
#make sure a curve is selected
selection = cmds.ls(sl=True)
if selection:
sel = selection[0]
shape = cmds.listRelatives(sel, s=True)[0]
type = cmds.objectType(shape)
name = cmds.textFieldGrp("name", q=True, tx=True)
hisGrp = cmds.checkBox("history", q=True, v=True)
hisPoly = cmds.checkBox("polyHistory", q=True, v=True)
if type== "nurbsCurve":
#offset the curb
distance = cmds.floatFieldGrp("curbFFG", q=True, v1=True)
# bump = cmds.checkBox("bumpCB", q=True, v=True)
pos = cmds.checkBox("curbCB", q=True, v=True)
if pos == 0:
dist = distance * -1
else:
dist = distance
U = cmds.intFieldGrp("UDivIFG", q=True, v1=True)
V = cmds.intFieldGrp("VDivIFG", q=True, v1=True)
origCrv = cmds.rename(sel, "%s_inner_CRV"%name)
outCurve = cmds.offsetCurve(origCrv, d=dist, n="%s_outer_CRV"%name)
midCurve = cmds.offsetCurve(origCrv, d=dist/2, n="%s_mid_CRV"%name)
# if bump:
# cmds.xform(midCurve, ws=True, r=True, t=(0,5,0))
cmds.select(cl=True)
lofted = cmds.loft(origCrv, midCurve, outCurve)[0]
loft = cmds.rename(lofted, "%s_lofted"%name)
polygon = cmds.nurbsToPoly(loft, pt=1, ch=hisPoly, f=2, un=U, vn=V)[0]
poly = cmds.rename(polygon, "%s_poly"%name)
curbGrp = cmds.group(empty=True)
grp = cmds.rename(curbGrp, "%s_History_GRP"%name)
# cmds.rename(poly, "polyCurb")
cmds.parent(loft, outCurve, midCurve, origCrv, grp)
cmds.setAttr("%s.v"%grp, 0)
if not hisGrp:
cmds.delete(grp)
else:
cmds.warning("That's not a curve! You need to select a curve!")
else:
cmds.warning("You haven't selected anything!")
def fkChain(ctrlType="circle", color="red", axis="x", *args):
"""
puts a correctly oriented control onto each joint of selected chain. Will name the controls after the joint names and parent them according to the joint order
Select the top joint of a chain and call fkChain(ARGS)
Arguments: ctrlType ("sphere", "circle", "cube", etc), color ("red", "darkRed",etc. See zbw_rig.createControl for full list), axis ("x", "y", "x")
"""
#get the selected joint's chain of joints
sel = cmds.ls(sl=True, type="joint")
#initialize lists
ctrlList = []
groupList = []
#for now just do one chain
if len(sel) != 1:
cmds.error("please select only the top level joint of one chain")
else:
#get the hierarchy of just joints
allChain = cmds.select(sel[0], hi=True)
chain = cmds.ls(sl=True, type="joint")
chainSize = len(chain)
for jnt in chain:
#get the rot order
rotOrder = cmds.getAttr("%s.rotateOrder"%jnt)
#control name
ctrlName = jnt + "_CTRL"
#create control
ctrl = createControl(ctrlName, ctrlType, axis, color)
#snap that control to the joint (group orient)
groupOrient(jnt, ctrl, "GRP")
group = ctrl + "_GRP"
#orient constrain the joint to the control
cmds.orientConstraint(ctrl, jnt)
#set rotation order for the control and group
cmds.setAttr("%s.rotateOrder"%ctrl, rotOrder)
cmds.setAttr("%s.rotateOrder"%group, rotOrder)
#add the controls and groups to lists to keep their order
ctrlList.append(ctrl)
groupList.append(group)
#parent up the groups and controls correctly
for i in range(chainSize-1, 0, -1):
cmds.parent(groupList[i], ctrlList[i-1])
def align_to_curve(crv=None, obj=None, param=None, *args):
"""
places the obj on the curve aligned to . . .
Args:
obj (string): object to align
crv: (string): curve TRANSFORM to align to
param (float): parameter along curve to position and orient to
*args:
Returns:
void
"""
#TODO - check on non-orig geo, check the matrix plugin is loaded
if not obj and crv and param:
cmds.warning("zbw_rig.align_to_curve: Didnt' get all the correct params! (obj, crv, param)")
return()
if not isType(crv, "nurbsCurve"):
cmds.warning("zbw_rig.align_to_curve: crv param wasn't a curve!")
return()
crvShp = cmds.listRelatives(crv, s=True)[0]
tempObj = cmds.group(empty=True, name="tempCrvNull")
poci = cmds.shadingNode("pointOnCurveInfo", asUtility=True, name="tempPOCI")
cmds.connectAttr("{0}.worldSpace[0]".format(crvShp), "{0}.inputCurve".format(poci))
cmds.setAttr("{0}.parameter".format(poci), param)
cmds.connectAttr("{0}.position".format(poci), "{0}.translate".format(tempObj))
sideVal = cmds.getAttr("{0}.normalizedNormal".format(poci))[0]
side = om.MVector(sideVal[0], sideVal[1], sideVal[2])
frontVal = cmds.getAttr("{0}.normalizedTangent".format(poci))[0]
front = om.MVector(frontVal[0], frontVal[1], frontVal[2])
up = side ^ front
mat4 = cmds.shadingNode("fourByFourMatrix", asUtility=True, name="temp4x4")
decomp = cmds.shadingNode("decomposeMatrix", asUtility=True, name="tempDM")
yrow = [side[0], side[1], side[2], 0]
xrow = [front[0], front[1], front[2], 0]
zrow = [up[0], up[1], up[2], 0]
for col in range(3):
cmds.setAttr("{0}.in0{1}".format(mat4, col), xrow[col])
cmds.setAttr("{0}.in1{1}".format(mat4, col), yrow[col])
cmds.setAttr("{0}.in2{1}".format(mat4, col), zrow[col])
cmds.setAttr("{0}.in33".format(mat4), 1)
cmds.connectAttr("{0}.output".format(mat4), "{0}.inputMatrix".format(decomp))
cmds.connectAttr("{0}.outputRotate".format(decomp), "{0}.rotate".format(tempObj))
snapTo(tempObj, obj)
cmds.delete(tempObj, poci, decomp, mat4)
def exportControl(curves, name):
'''Export a control curve
'''
if not isinstance(curves, (list, tuple)):
curves = [curves]
grp = mc.group(em=True, name=name)
for each in curves:
ml_parentShape.parentShape(each, grp)
mc.delete(grp, constructionHistory=True)
tempFile = mc.internalVar(userTmpDir=True)
tempFile+='tempControlExport.ma'
mc.select(grp)
mc.file(tempFile, force=True, typ='mayaAscii', exportSelected=True)
with open(tempFile, 'r') as f:
contents = f.read()
ctrlLines = ['//ML Control Curve: '+name]
record = False
for line in contents.splitlines():
if line.startswith('select'):
break
if line.strip().startswith('rename'): #skip the uuid commands
continue
if line.startswith('createNode transform'):
record = True
ctrlLines.append('string $ml_tempCtrlName = `createNode transform -n "'+name+'_#"`;')
elif line.startswith('createNode nurbsCurve'):
ctrlLines.append('createNode nurbsCurve -p $ml_tempCtrlName;')
elif record:
ctrlLines.append(line)
with open(controlFilePath(name), 'w') as f:
f.write('\n'.join(ctrlLines))
return grp
def reset_pivot(*args):
sel = mc.ls(sl=True)
if not sel:
om.MGlobal.displayWarning('Nothing selected.')
return
if len(sel) > 1:
om.MGlobal.displayWarning('Only works on one node at a time.')
return
node = sel[0]
driver = None
driver_value = None
driver_default = None
if is_pivot_connected(node):
driver = pivot_driver_attr(node)
if driver:
dNode,dAttr = driver.split('.',1)
driver_value = mc.getAttr(driver)
driver_default = mc.attributeQuery(dAttr, node=dNode, listDefault=True)[0]
if driver_default == driver_value:
return
else:
om.MGlobal.displayWarning('Pivot attribute is connected, unable to edit.')
return
if not driver:
pivotPosition = mc.getAttr(node+'.rotatePivot')[0]
if pivotPosition == (0.0,0.0,0.0):
return
tempPosition = mc.group(em=True)
tempPivot = mc.group(em=True)
tempPivot = mc.parent(tempPivot, node)[0]
if driver:
mc.setAttr(driver, driver_default)
newRP = mc.getAttr(node+'.rotatePivot')[0]
mc.setAttr(driver, driver_value)
mc.setAttr(tempPivot+'.translate', *newRP)
else:
mc.setAttr(tempPivot+'.translate', 0,0,0)
mc.setAttr(tempPivot+'.rotate', 0,0,0)
utl.matchBake(source=[tempPivot], destination=[tempPosition], bakeOnOnes=True, maintainOffset=False, preserveTangentWeight=False, rotate=False)
if driver:
mc.setAttr(driver, driver_default)
else:
mc.setAttr(node+'.rotatePivot', 0,0,0)
mc.refresh()
utl.matchBake(source=[tempPosition], destination=[node], bakeOnOnes=True, maintainOffset=False, preserveTangentWeight=False, rotate=False)
mc.delete(tempPosition,tempPivot)
mc.select(node)