コード例 #1
0
ファイル: generic.py プロジェクト: PearseT/Maya_scripts
def main(templateFile=None,
         inheritTemplateRotations=False,
         controlShapes=None,
         fbx=False,
         scale=1,
         radius=1):

    template(filepath=templateFile, scale=scale)

    root.main(name="assembly",
              fbx=fbx,
              radius=radius,
              body=False,
              pelvis=False)
    mc.parent("cog_jnt", "cog_ctrl")
    mc.connectAttr("assembly.joints", "cog_jnt.v")
    common.selectable("assembly.editJoints", "cog_jnt")
    common.sets("root", "cog_jnt", None, None)
    mc.delete("template")

    if fbx:
        mc.createNode("joint", n="root_fbx", p="skeleton_fbx")
        mc.setAttr("root_fbx.radius", radius * 0.25)
        c = mc.pointConstraint("cog_ctrl", "root_fbx", sk=["y"])[0]
        mc.parent(c, "constraints_fbx")
        c = mc.orientConstraint("cog_ctrl", "root_fbx", sk=["x", "z"])[0]
        mc.parent(c, "constraints_fbx")
        c = mc.scaleConstraint("cog_ctrl", "root_fbx")[0]
        mc.parent(c, "constraints_fbx")
        mc.createNode("joint", n="cog_fbx", p="root_fbx")
        mc.setAttr("cog_fbx.radius", radius * 0.25)
        c = mc.parentConstraint("cog_jnt", "cog_fbx")[0]
        mc.parent(c, "constraints_fbx")
        c = mc.scaleConstraint("cog_jnt", "cog_fbx")[0]
        mc.parent(c, "constraints_fbx")

    if controlShapes:
        if os.path.isfile(THIS_DIR + "/" + controlShapes):
            common.loadControlShapes(THIS_DIR + "/" + controlShapes)
        elif os.path.isfile(controlShapes):
            common.loadControlShapes(controlShapes)

    mc.select(cl=True)
    mc.dgdirty(a=True)
コード例 #2
0
ファイル: arm.py プロジェクト: PearseT/Maya_scripts
def main(name="arm",
         positions=None,
         inheritTemplateRotations=False,
         radius=1,
         ikShape="cube"):

    # create template joints if needed
    tmp = None
    if not positions:
        positions = template(name=name)
        tmp = positions[:]  # copy

    # positions/orientations from template transforms/joints
    rotations = [None] * 4
    for i in range(4):
        if type(positions[i]) == str or type(positions[i]) == unicode:
            if inheritTemplateRotations:
                rotations[i] = mc.xform(positions[i], q=True, ro=True, ws=True)
            positions[i] = mc.xform(positions[i], q=True, rp=True, ws=True)

    grp = mc.createNode("transform", n=name)

    #
    # ik joints
    #

    ik = [None] * 4
    for i in range(1, 4):
        ik[i] = mc.createNode("joint", n=name + "_ik" + str(i + 1))
        mc.setAttr(ik[i] + ".t", positions[i][0], positions[i][1],
                   positions[i][2])
        if inheritTemplateRotations:
            mc.setAttr(ik[i] + ".r", rotations[i][0], rotations[i][1],
                       rotations[i][2])

    if not inheritTemplateRotations:
        if positions[1][0] >= -0.001:
            mc.delete(
                mc.aimConstraint(ik[2],
                                 ik[1],
                                 aim=(1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[3]))
            mc.delete(
                mc.aimConstraint(ik[3],
                                 ik[2],
                                 aim=(1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[1]))
            mc.delete(
                mc.aimConstraint(ik[2],
                                 ik[3],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[1]))
        else:
            mc.delete(
                mc.aimConstraint(ik[2],
                                 ik[1],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[3]))
            mc.delete(
                mc.aimConstraint(ik[3],
                                 ik[2],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[1]))
            mc.delete(
                mc.aimConstraint(ik[2],
                                 ik[3],
                                 aim=(1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[1]))

    for i in range(1, 4):
        r = mc.getAttr(ik[i] + ".r")[0]
        mc.setAttr(ik[i] + ".jo", r[0], r[1], r[2])
        mc.setAttr(ik[i] + ".r", 0, 0, 0)
        mc.setAttr(ik[i] + ".radius", radius * 0.5)

    # first ik joint
    ik[0] = mc.createNode("joint", n=name + "_ik1")
    mc.setAttr(ik[0] + ".t", positions[0][0], positions[0][1], positions[0][2])
    if inheritTemplateRotations:
        mc.setAttr(ik[0] + ".r", rotations[0][0], rotations[0][1],
                   rotations[0][2])
    else:
        n = mc.createNode("transform")
        mc.delete(mc.pointConstraint(ik[0], n))
        mc.setAttr(n + ".tz", mc.getAttr(n + ".tz") - 1)
        if positions[1][0] >= -0.001:
            mc.delete(
                mc.aimConstraint(ik[1],
                                 ik[0],
                                 aim=(1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=n))
        else:
            mc.delete(
                mc.aimConstraint(ik[1],
                                 ik[0],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=n))
        mc.delete(n)
    r = mc.getAttr(ik[0] + ".r")[0]
    mc.setAttr(ik[0] + ".jo", r[0], r[1], r[2])
    mc.setAttr(ik[0] + ".r", 0, 0, 0)

    #
    # ik controls
    #

    ik_ctrl_grp = [None] * 3
    ik_ctrl = [None] * 3
    ik_ctrl_grp[1], ik_ctrl[1] = common.control(
        name=name + "_ik2",
        parent=grp,
        position=ik[3],
        normal=(1, 0, 0),
        color=6,
        radius=radius,
        shape=ikShape,
        lockAttr=["s", "v"],
        hideAttr=["sx", "sy", "sz", "v"])
    mc.addAttr(ik_ctrl[1], ln="joints", at="bool", dv=True, k=True)
    mc.addAttr(ik_ctrl[1], ln="editJoints", at="bool", k=True)
    mc.addAttr(ik_ctrl[1], ln="fkControls", at="bool", dv=True, k=True)
    mc.addAttr(ik_ctrl[1], ln="ikControls", at="bool", dv=True, k=True)
    mc.addAttr(ik_ctrl[1], ln="stretch", at="bool", k=True)

    ik_ctrl_grp[2], ik_ctrl[2] = common.control(name=name+"_pv", parent=grp,
           position=ik[2], rotation=ik[2], normal=(0,0,1),
           color=6, radius=radius*0.25, shape=ikShape,
           lockAttr=["r","s"], \
           hideAttr=["rx","ry","rz","sx","sy","sz","v"])
    if positions[0][0] <= -0.001:
        mc.move(0, 0, -3 * radius, ik_ctrl_grp[2], os=True, r=True, wd=True)
    else:
        direction = -1 if inheritTemplateRotations else 1
        mc.move(0,
                0,
                direction * 3 * radius,
                ik_ctrl_grp[2],
                os=True,
                r=True,
                wd=True)
    mc.connectAttr(ik_ctrl[1] + ".ikControls", ik_ctrl[2] + ".v")

    #
    # ik stretch joints
    #

    ik2 = [None] * 4
    for i in range(4):
        ik2[i] = mc.duplicate(ik[i])[0]
        ik2[i] = mc.rename(ik2[i], name + "_ik" + str(i + 1) + "_str")
        if not i: ik2[i] = mc.parent(ik2[i], grp)[0]
        else:
            ik2[i] = mc.parent(ik2[i], ik2[i - 1])[0]
            if i < 3:
                c = mc.parentConstraint(ik[i], ik2[i])[0]
                mc.rename(c, name + "_ik" + str(i + 1) + "_str_parcon")
            else:
                c = mc.pointConstraint(ik[i], ik2[i])[0]
                mc.rename(c, name + "_ik" + str(i + 1) + "_str_pntcon")
            if i == 2: mc.connectAttr(ik[2] + ".r", ik2[2] + ".r")
        mc.setAttr(ik2[i] + ".jo", 0, 0, 0)

    mc.delete(mc.orientConstraint(ik[3], ik2[3]))
    c = mc.orientConstraint(ik_ctrl[1], ik2[3], mo=True)[0]
    mc.rename(c, ik2[3].split("|")[-1] + "_oricon")

    #
    # fk joints and controls
    #

    fk_ctrl = [None] * 4
    jnt = [None] * 4
    for i in range(4):
        fk_ctrl[i] = mc.duplicate(ik[i])[0]
        if i:
            fk_ctrl[i] = mc.rename(fk_ctrl[i],
                                   name + "_fk" + str(i + 1) + "_ctrl")
            mc.connectAttr(ik2[i] + ".t", fk_ctrl[i] + ".t")
            mc.connectAttr(ik2[i] + ".r", fk_ctrl[i] + ".jo")
            mc.setAttr(fk_ctrl[i] + ".r", 0, 0, 0)
            if inheritTemplateRotations:
                common.control(addShapeTo=fk_ctrl[i],
                               normal=(1, 0, 0),
                               color=13,
                               radius=radius * 0.7)
            else:
                common.control(addShapeTo=fk_ctrl[i],
                               normal=(1, 0, 0),
                               color=13,
                               radius=radius * 0.7)
            mc.connectAttr(
                ik_ctrl[1] + ".fkControls",
                mc.listRelatives(fk_ctrl[i], pa=True, s=True)[0] + ".v")
        else:
            # the first fk control becomes ik one
            fk_ctrl[i] = mc.rename(fk_ctrl[i], name + "_ik1_ctrl")
            common.control(addShapeTo=fk_ctrl[i],
                           normal=(1, 0, 0),
                           color=6,
                           radius=radius)
            mc.connectAttr(
                ik_ctrl[1] + ".ikControls",
                mc.listRelatives(fk_ctrl[i], pa=True, s=True)[0] + ".v")
        if i == 1: fk_ctrl[i] = mc.parent(fk_ctrl[i], grp)[0]
        elif i > 1: fk_ctrl[i] = mc.parent(fk_ctrl[i], fk_ctrl[i - 1])[0]
        mc.setAttr(fk_ctrl[i] + ".drawStyle", 2)

        # fk joints
        jnt[i] = mc.duplicate(ik[i])[0]
        if i: jnt[i] = mc.parent(jnt[i], fk_ctrl[i])[0]
        else: jnt[i] = mc.parent(jnt[i], ik[0])[0]
        jnt[i] = mc.rename(jnt[i], name + "_jnt" + str(i + 1))
        for a in ["t", "r", "jo"]:
            mc.setAttr(jnt[i] + "." + a, 0, 0, 0)
        mc.connectAttr(ik_ctrl[1] + ".joints", jnt[i] + ".v")
        if i: mc.setAttr(jnt[i] + ".drawStyle", 0)
        mc.setAttr(jnt[i] + ".radius", radius * 0.5)

    # the first ik joint becomes fk control
    common.control(addShapeTo=ik[0],
                   normal=(1, 0, 0),
                   color=13,
                   radius=radius * 0.7)
    mc.connectAttr(ik_ctrl[1] + ".fkControls",
                   mc.listRelatives(ik[0], pa=True, s=True)[0] + ".v")
    ik[0] = mc.rename(ik[0], name + "_fk1_ctrl")
    mc.setAttr(ik[0] + ".drawStyle", 2)

    fk1_ctrl_grp = mc.createNode("joint", n=name + "_fk1_ctrl_grp", p=grp)
    mc.delete(mc.parentConstraint(ik[0], fk1_ctrl_grp))
    r = mc.getAttr(ik[0] + ".jo")[0]
    mc.setAttr(fk1_ctrl_grp + ".jo", r[0], r[1], r[2])
    mc.connectAttr(fk_ctrl[0] + ".r", fk1_ctrl_grp + ".r")
    mc.setAttr(fk1_ctrl_grp + ".drawStyle", 2)
    ik[0] = mc.parent(ik[0], fk1_ctrl_grp)[0]
    mc.setAttr(ik[0] + ".jo", 0, 0, 0)

    c = mc.orientConstraint(fk_ctrl[0], ik2[0], mo=True)[0]
    mc.rename(c, name + "_ik" + str(i + 1) + "_str_oricon")

    fk2_ctrl_grp = mc.createNode("joint", n=name + "_fk2_ctrl_grp")
    mc.setAttr(fk2_ctrl_grp + ".drawStyle", 2)

    #
    # ik handles
    #

    for i in range(2, 4):
        ik[i] = mc.parent(ik[i], ik[i - 1])[0]
    mc.select(ik[1:])
    mc.SetPreferredAngle()

    ikh = common.ikHandle(name, ik[1], ik[3], parent=ik_ctrl[1])[0]
    pvc = mc.poleVectorConstraint(ik_ctrl[2], ikh)[0]
    mc.rename(pvc, name + "_pvcon")

    #
    # stretch math
    #

    n = mc.createNode("transform", n="str_grp", p=fk_ctrl[0])
    mc.delete(mc.pointConstraint(ik[1], n))
    db1 = mc.createNode("distanceBetween")
    mc.connectAttr(n + ".worldMatrix", db1 + ".inMatrix1")
    mc.connectAttr(ik_ctrl[1] + ".worldMatrix", db1 + ".inMatrix2")
    db2 = mc.createNode("distanceBetween")
    n2 = mc.duplicate(n)[0]
    mc.connectAttr(n2 + ".worldMatrix", db2 + ".inMatrix1")
    mc.connectAttr(ik_ctrl_grp[1] + ".worldMatrix", db2 + ".inMatrix2")
    md1 = mc.createNode("multiplyDivide")
    mc.setAttr(md1 + ".operation", 2)
    mc.connectAttr(db1 + ".distance", md1 + ".input1X")
    mc.connectAttr(db2 + ".distance", md1 + ".input2X")
    md2 = mc.createNode("multiplyDivide")
    mc.connectAttr(md1 + ".outputX", md2 + ".input1X")
    mc.connectAttr(ik_ctrl[1] + ".stretch", md2 + ".input2X")
    c = mc.createNode("condition")
    mc.setAttr(c + ".secondTerm", 1)
    mc.setAttr(c + ".operation", 3)
    mc.connectAttr(md1 + ".outputX", c + ".colorIfTrueR")
    mc.connectAttr(md2 + ".outputX", c + ".firstTerm")

    mc.connectAttr(c + ".outColorR", ik[1] + ".sx")
    mc.connectAttr(c + ".outColorR", ik[2] + ".sx")

    # offset the stretch group to enable full extension
    # of the leg before it starts stretching
    db3 = mc.createNode("distanceBetween")
    mc.connectAttr(fk_ctrl[1] + ".worldMatrix", db3 + ".inMatrix1")
    mc.connectAttr(fk_ctrl[2] + ".worldMatrix", db3 + ".inMatrix2")
    d = mc.getAttr(db3 + ".distance")
    mc.connectAttr(ik_ctrl[1] + ".worldMatrix", db3 + ".inMatrix1", f=True)
    d += mc.getAttr(db3 + ".distance") - mc.getAttr(db1 + ".distance")
    mc.delete(db3)
    p = mc.xform(n2, q=True, rp=True, ws=True)
    p2 = mc.xform(ik_ctrl[1], q=True, rp=True, ws=True)
    p[0] -= p2[0]
    p[1] -= p2[1]
    p[2] -= p2[2]
    m = math.sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2])
    p[0] /= m
    p[1] /= m
    p[2] /= m
    p2 = mc.getAttr(n2 + ".t")[0]
    mc.setAttr(n2 + ".t", p2[0] - p[0] * d, p2[1] - p[1] * d, p2[2] - p[2] * d)

    #
    # lock and hide attributes
    #

    for i in range(4):
        for a in ["tx", "ty", "tz", "sx", "sy", "sz", "v", "radius"]:
            mc.setAttr(fk_ctrl[i] + "." + a, l=True, k=False, cb=False)
    mc.setAttr(fk_ctrl[2] + ".rx", l=True, k=False, cb=False)
    mc.setAttr(fk_ctrl[2] + ".rz", l=True, k=False, cb=False)
    for a in ["tx", "ty", "tz", "sx", "sy", "sz", "v", "radius"]:
        mc.setAttr(ik[0] + "." + a, l=True, k=False, cb=False)

    # selection sets
    ik_ctrl[0] = fk_ctrl.pop(0)
    fk_ctrl.insert(0, ik.pop(0))
    common.sets(name, jnt, fk_ctrl, ik_ctrl)

    # selectable joints
    common.selectable(ik_ctrl[1] + ".editJoints", jnt)

    # fk to ik bake ready
    for i in range(4):
        mc.addAttr(ik_ctrl[1], ln="fk" + str(i + 1), at="message", h=True)
        mc.addAttr(fk_ctrl[i], ln="fk", at="message", h=True)
        mc.connectAttr(ik_ctrl[1] + ".fk" + str(i + 1), fk_ctrl[i] + ".fk")
    mc.addAttr(ik_ctrl[1], ln="ik1", at="message", h=True)
    mc.addAttr(ik_ctrl[0], ln="ik", at="message", h=True)
    mc.connectAttr(ik_ctrl[1] + ".ik1", ik_ctrl[0] + ".ik")
    mc.addAttr(ik_ctrl[1], ln="ik2", at="message", h=True)
    mc.addAttr(ik_ctrl[2], ln="ik", at="message", h=True)
    mc.connectAttr(ik_ctrl[1] + ".ik2", ik_ctrl[2] + ".ik")

    # organize
    if tmp: mc.delete(tmp)
    mc.parent(fk_ctrl[1], fk2_ctrl_grp)[0]
    fk2_ctrl_grp = mc.parent(fk2_ctrl_grp, fk_ctrl[0])[0]
    mc.setAttr(fk2_ctrl_grp + ".t", 0, 0, 0)

    mc.hide(ik[0], ik2[0])
    mc.parent(ik[0], ik_ctrl[0])
    mc.parent(ik_ctrl[0], grp)
    mc.select(grp)
    mc.dgdirty(a=True)

    return grp
コード例 #3
0
ファイル: wheel.py プロジェクト: PearseT/Maya_scripts
def main(name="wheel", terrain=None, radius=1, scale=1):

	if terrain == None:
		l = mc.ls(sl=True, o=True) or []
		for n in l:
			if mc.nodeType(n) == "mesh":
				terrain = n
				break
			else:
				l2 = mc.listRelatives(n, pa=True, s=True, ni=True) or []
				for n2 in l2:
					if mc.nodeType(n2) == "mesh":
						terrain = n
						break
			if terrain: break

	grp, ctrl = common.control(name=name+"_placer", color=6, radius=radius*3, shape="square",
								hideAttr=["tx","tz","rx","ry","rz","sx","sy","sz","v"],
								lockAttr=["tx","tz","rx","ry","rz","sx","sy","sz"])
	grp = mc.rename(grp, name+"_grp")

	susp = mc.curve(d=1, p=[(0,-0.5,0),(0,0.5,0)], k=(0,1), n=name+"_susp")
	mc.setAttr(susp+".template", True)
	susp = mc.parent(susp, ctrl)[0]

	grp2, ctrl2 = common.control(name=name, numOffsetGroups=1, color=13, radius=radius*1.25,
					normal=(1,0,0), parent=grp, lockAttr=["tx","tz","rx"],
					hideAttr=["tx","tz","rx","sx","sy","sz"])
	tilt = mc.listRelatives(ctrl2, pa=True, p=True)[0]
	tilt = mc.rename(tilt, name+"_tilt_grp")
	mc.addAttr(ctrl2, ln="joints", at="bool", dv=True, k=True)
	mc.addAttr(ctrl2, ln="editJoints", at="bool", k=True)
	mc.addAttr(ctrl2, ln="placer", at="bool", dv=True, k=True)
	mc.addAttr(ctrl2, ln="wheel", at="bool", dv=True, k=True)
	mc.addAttr(ctrl2, ln="terrain", at="bool", dv=True, k=True)
	mc.addAttr(ctrl2, ln="editTerrain", at="bool", dv=False, k=True)
	mc.addAttr(ctrl2, ln="spin", at="double", k=True)
	mc.addAttr(ctrl2, ln="tilt", at="double", dv=1, k=True)
	mc.addAttr(ctrl2, ln="size", at="double", dv=1, min=0, k=True)
	mc.addAttr(ctrl2, ln="terrainDetection", at="bool", dv=True, k=True)
	mc.addAttr(ctrl2, ln="suspensionOffset", at="double", min=0, k=True)
	mc.addAttr(ctrl2, ln="suspensionLength", at="double", dv=1, min=0, k=True)

	mc.connectAttr(ctrl2+".placer", ctrl+"Shape.v")

	wheel = mc.polyCylinder(h=0.5, ax=(1,0,0), sc=1, ch=False, n=name+"_pxy")[0]
	mc.select(wheel+".e[0:19]", wheel+".e[20:39]")
	mc.polyBevel(wheel+".e[0:39]", o=0.1, ch=False)
	mc.setAttr(wheel+"Shape.overrideEnabled", True)
	mc.setAttr(wheel+"Shape.overrideDisplayType", 2)
	mc.setAttr(wheel+"Shape.overrideShading", 0)
	mc.setAttr(wheel+"Shape.overrideColor", 1)
	mc.setAttr(wheel+".castsShadows", 0)
	mc.setAttr(wheel+".receiveShadows", 0)
	mc.setAttr(wheel+".primaryVisibility", 0)
	mc.setAttr(wheel+".visibleInReflections", 0)
	mc.setAttr(wheel+".visibleInRefractions", 0)
	mc.setAttr(wheel+".doubleSided", 0)
	mc.connectAttr(ctrl2+".wheel", wheel+"Shape.v")
	mc.parent(wheel, ctrl)

	jnt = mc.createNode("joint", n=name+"_jnt", p=ctrl, ss=True)
	mc.connectAttr(wheel+".t", jnt+".t")
	mc.connectAttr(wheel+".r", jnt+".r")
	mc.connectAttr(ctrl2+".joints", jnt+".v")
	mc.setAttr(jnt+".radius", radius*0.5)
	common.selectable(ctrl2+".editJoints", jnt)
	common.sets("wheel", jnt, None, None)

	if terrain == None:
		terrain = _terrain.main(name=name, parent=grp)
		mc.connectAttr(ctrl2+".terrain", terrain+".v")
		r = mc.createNode("reverse", ss=True)
		mc.connectAttr(ctrl2+".editTerrain", r+".inputX")
		mc.connectAttr(r+".outputX", terrain+".template")

	ter_ted = mc.createNode("transform", p=ctrl, n=name+"_terrain_detection", ss=True)
	pc = mc.parentConstraint(ctrl2, wheel)[0]
	mc.pointConstraint(susp, ter_ted)
	gc = mc.geometryConstraint(terrain, ter_ted)[0]
	mc.connectAttr(ctrl2+".terrainDetection", gc+"."+terrain+"W0")
	susp_orig = mc.createNode("transform", p=ctrl, n=name+"_suspension_origin", ss=True)
	mc.pointConstraint(ter_ted, susp_orig)
	mc.geometryConstraint(susp, susp_orig)
	sPntConst = mc.pointConstraint(susp_orig, grp2)[0]

	mc.connectAttr(ctrl2+".suspensionOffset", susp+".ty")
	mc.connectAttr(ctrl2+".suspensionLength", susp+".sy")
	mc.connectAttr(ctrl2+".placer", susp+".v")
	adl = mc.createNode("addDoubleLinear", ss=True)
	mc.connectAttr(ctrl2+".spin", adl+".input1")
	mc.connectAttr(adl+".output", pc+".target[0].targetOffsetRotateX")
	adl = mc.createNode("addDoubleLinear", ss=True)
	mc.connectAttr(ctrl2+".tilt", adl+".input1")
	md = mc.createNode("multiplyDivide", ss=True)
	mc.setAttr(md+".input2Y", -1)
	mc.connectAttr(ctrl2+".size", md+".input1Y")
	mc.connectAttr(md+".outputY", tilt+".rotatePivotY")
	mc.connectAttr(md+".outputY", tilt+".scalePivotY")
	mc.connectAttr(ctrl2+".s", wheel+".s")
	mc.connectAttr(ctrl2+".size", sPntConst+".oy")
	mc.connectAttr(ctrl2+".size", ctrl2+".sx")
	mc.connectAttr(ctrl2+".size", ctrl2+".sy")
	mc.connectAttr(ctrl2+".size", ctrl2+".sz")
	mc.setAttr(ctrl2+".s", l=True)
	mc.move(0, -1, 0, tilt+".scalePivot", tilt+".rotatePivot", r=True)

	mc.expression(s=tilt+".rz = "+ctrl2+".ry * -"+adl+".output * 0.25", o="", ae=True, uc="all", n=name+"_wheel_exp")

	common.sets("wheel", None, [ctrl, ctrl2], None)

	mc.select(grp)
	mc.dgdirty(a=True)

	return grp, ctrl, ctrl2, wheel
コード例 #4
0
ファイル: head.py プロジェクト: PearseT/Maya_scripts
def main(name="head",
         control=None,
         parent=None,
         positions=None,
         radius=1,
         ikControlOffset=5,
         ikShape="cube"):

    # create template joints if needed
    tmp = None
    if not positions:
        positions = template(name=name)
        tmp = positions[:]  # copy

    # positions from template transforms/joints
    for i in range(3):
        if type(positions[i]) == str or type(positions[i]) == unicode:
            positions[i] = mc.xform(positions[i], q=True, rp=True, ws=True)

    if not control:
        center = [0, 0, 0]
        c = len(positions)
        for i in range(3):
            center[0] += positions[i][0]
            center[1] += positions[i][1]
            center[2] += positions[i][2]
        center[0] /= c
        center[1] /= c
        center[2] /= c

        control = common.control(name=name,
                                 position=center,
                                 normal=(0, 1, 0),
                                 color=17,
                                 radius=radius * 2,
                                 lockAttr=("v"),
                                 hideAttr=("v"))[1]

    if not parent: parent = control

    if not mc.objExists(control + ".joints"):
        mc.addAttr(control, ln="joints", at="bool", dv=True, k=True)
    if not mc.objExists(control + ".editJoints"):
        mc.addAttr(control, ln="editJoints", at="bool", k=True)
    if not mc.objExists(control + ".fkControls"):
        mc.addAttr(control, ln="fkControls", at="bool", dv=True, k=True)
    if not mc.objExists(control + ".ikControls"):
        mc.addAttr(control, ln="ikControls", at="bool", dv=True, k=True)

    #
    # fk controls
    #

    fk_ctrl_grp = [None] * 3
    fk_ctrl = [None] * 3
    fk_ctrl_grp[0], fk_ctrl[0] = common.control(
        name=name + "_jaw_fk",
        parent=control,
        position=positions[0],
        normal=(1, 0, 0),
        color=13,
        radius=radius * 0.75,
        lockAttr=["t", "s", "v"],
        hideAttr=["tx", "ty", "tz", "sx", "sy", "sz", "v"])
    c = mc.parentConstraint(parent, fk_ctrl_grp[0], mo=True)[0]
    mc.rename(c, fk_ctrl_grp[0] + "_parcon")
    fk_ctrl_grp[1], fk_ctrl[1] = common.control(
        name=name + "_eye_lf_fk",
        parent=control,
        position=positions[1],
        numOffsetGroups=1,
        normal=(0, 0, 1),
        color=13,
        radius=radius * 0.5,
        lockAttr=["t", "rz", "s", "v"],
        hideAttr=["tx", "ty", "tz", "rz", "sx", "sy", "sz", "v"])
    mc.addAttr(fk_ctrl[1],
               ln="lookAt",
               at="float",
               min=0,
               max=10,
               dv=10,
               k=True)
    c = mc.parentConstraint(parent, fk_ctrl_grp[1], mo=True)[0]
    mc.rename(c, fk_ctrl_grp[1] + "_parcon")
    fk_ctrl_grp[2], fk_ctrl[2] = common.control(
        name=name + "_eye_rt_fk",
        parent=control,
        position=positions[2],
        numOffsetGroups=1,
        normal=(0, 0, 1),
        color=13,
        radius=radius * 0.5,
        lockAttr=["t", "rz", "s", "v"],
        hideAttr=["tx", "ty", "tz", "rz", "sx", "sy", "sz", "v"])
    mc.addAttr(fk_ctrl[2],
               ln="lookAt",
               at="float",
               min=0,
               max=10,
               dv=10,
               k=True)
    c = mc.parentConstraint(parent, fk_ctrl_grp[2], mo=True)[0]
    mc.rename(c, fk_ctrl_grp[2] + "_parcon")

    for n in fk_ctrl:
        mc.connectAttr(control + ".fkControls",
                       mc.listRelatives(n, pa=True, s=True)[0] + ".v")

    #
    # fk joints
    #

    jnt = [None] * 3
    jnt[0] = mc.createNode("joint", n="jaw_jnt", p=fk_ctrl[0])
    jnt[1] = mc.createNode("joint", n="eye_lf_jnt", p=fk_ctrl[1])
    jnt[2] = mc.createNode("joint", n="eye_rt_jnt", p=fk_ctrl[2])

    for n in jnt:
        mc.setAttr(n + ".radius", radius * 0.5)
        mc.connectAttr(control + ".joints", n + ".v")

    #
    # ik controls
    #

    positions[0] = [0, 0, 0]
    positions[0][0] = (positions[1][0] + positions[2][0]) * 0.5
    positions[0][1] = (positions[1][1] + positions[2][1]) * 0.5
    positions[0][2] = (positions[1][2] +
                       positions[2][2]) * 0.5 + ikControlOffset * radius
    ik_ctrl_grp, ik_ctrl = common.control(name=name + "_eyes_ik",
                                          parent=control,
                                          position=positions[0],
                                          normal=(0, 0, 1),
                                          color=6,
                                          radius=radius * 0.5,
                                          shape=ikShape,
                                          lockAttr=["sy", "sz", "v"],
                                          hideAttr=["sy", "sz", "v"])
    mc.connectAttr(control + ".ikControls",
                   mc.listRelatives(ik_ctrl, pa=True, s=True)[0] + ".v")
    eye_lf_aim = mc.createNode("transform", n="eye_lf_aim", p=ik_ctrl)
    mc.delete(mc.pointConstraint(jnt[1], eye_lf_aim))
    mc.setAttr(eye_lf_aim + ".tz", 0)
    eye_rt_aim = mc.createNode("transform", n="eye_rt_aim", p=ik_ctrl)
    mc.delete(mc.pointConstraint(jnt[2], eye_rt_aim))
    mc.setAttr(eye_rt_aim + ".tz", 0)

    n = mc.listRelatives(fk_ctrl[1], pa=True, p=True)[0]
    n2 = mc.listRelatives(mc.listRelatives(fk_ctrl[1], pa=True, p=True)[0],
                          pa=True,
                          p=True)[0]
    n2 = mc.createNode("transform", n=name + "_lf_eye_base_dir", p=n2)
    mc.delete(mc.parentConstraint(fk_ctrl[1], n2))
    mc.setAttr(n2 + ".tz", 1)
    c = mc.aimConstraint(n2,
                         n,
                         aim=(0, 0, 1),
                         u=(0, 1, 0),
                         wut="objectrotation",
                         wu=(0, 1, 0),
                         wuo=ik_ctrl,
                         mo=True)[0]
    mc.aimConstraint(eye_lf_aim,
                     n,
                     aim=(0, 0, 1),
                     u=(0, 1, 0),
                     wut="objectrotation",
                     wu=(0, 1, 0),
                     wuo=ik_ctrl,
                     mo=True)
    mc.setDrivenKeyframe(c + "." + n2 + "W0",
                         v=1,
                         dv=0,
                         cd=fk_ctrl[1] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(c + "." + n2 + "W0",
                         v=0,
                         dv=10,
                         cd=fk_ctrl[1] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(c + "." + eye_lf_aim + "W1",
                         v=0,
                         dv=0,
                         cd=fk_ctrl[1] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(c + "." + eye_lf_aim + "W1",
                         v=1,
                         dv=10,
                         cd=fk_ctrl[1] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.rename(c, n + "_aimcon")
    n = mc.listRelatives(fk_ctrl[2], pa=True, p=True)[0]
    n2 = mc.listRelatives(mc.listRelatives(fk_ctrl[2], pa=True, p=True)[0],
                          pa=True,
                          p=True)[0]
    n2 = mc.createNode("transform", n=name + "_rt_eye_base_dir", p=n2)
    mc.delete(mc.parentConstraint(fk_ctrl[2], n2))
    mc.setAttr(n2 + ".tz", 1)
    c = mc.aimConstraint(n2,
                         n,
                         aim=(0, 0, 1),
                         u=(0, 1, 0),
                         wut="objectrotation",
                         wu=(0, 1, 0),
                         wuo=ik_ctrl,
                         mo=True)[0]
    mc.aimConstraint(eye_rt_aim,
                     n,
                     aim=(0, 0, 1),
                     u=(0, 1, 0),
                     wut="objectrotation",
                     wu=(0, 1, 0),
                     wuo=ik_ctrl,
                     mo=True)
    mc.setDrivenKeyframe(c + "." + n2 + "W0",
                         v=1,
                         dv=0,
                         cd=fk_ctrl[2] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(c + "." + n2 + "W0",
                         v=0,
                         dv=10,
                         cd=fk_ctrl[2] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(c + "." + eye_rt_aim + "W1",
                         v=0,
                         dv=0,
                         cd=fk_ctrl[2] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(c + "." + eye_rt_aim + "W1",
                         v=1,
                         dv=10,
                         cd=fk_ctrl[2] + ".lookAt",
                         itt="linear",
                         ott="linear")
    mc.rename(c, n + "_aimcon")

    # selection sets
    common.sets(name, jnt, fk_ctrl, [ik_ctrl])

    # selectable joints
    common.selectable(control + ".editJoints", jnt)

    if tmp: mc.delete(tmp)
    mc.select(control)
    mc.dgdirty(a=True)

    return control
コード例 #5
0
def main(name="spine", positions=None, radius=1):

    # create template joints if needed
    tmp = None
    if not positions:
        positions = template()
        tmp = positions[:]  # copy

    # positions from template transforms/joints
    for i in range(6):
        if type(positions[i]) == str or type(positions[i]) == unicode:
            positions[i] = mc.xform(positions[i], q=True, rp=True, ws=True)

    grp = mc.createNode("transform", n=name)

    #
    # joints, part 1
    #

    ik = [None] * 9
    mc.select(cl=True)
    ik[0] = mc.joint(p=positions[0], n=name + "_ik1_off")
    ik[2] = mc.joint(p=positions[1], n=name + "_ik2")
    mc.joint(ik[0], e=True, oj="xyz", sao="xup", ch=False, zso=True)
    # insert an "offset" joint for proper ik spline twist
    ik[3] = mc.joint(p=[i * 0.5 for i in map(add, positions[1], positions[2])],
                     n=name + "_ik3_off")
    mc.joint(ik[2], e=True, oj="xyz", sao="xup", ch=False, zso=True)
    ik[4] = mc.joint(p=positions[2], n=name + "_ik3")
    mc.joint(ik[3], e=True, oj="xyz", sao="xup", ch=False, zso=True)
    ik[5] = mc.joint(p=positions[3], n=name + "_ik4")
    mc.joint(ik[4], e=True, oj="xyz", sao="xup", ch=False, zso=True)
    ik[6] = mc.joint(p=positions[4], n=name + "_ik5")
    mc.joint(ik[5], e=True, oj="xyz", sao="xup", ch=False, zso=True)
    mc.setAttr(ik[6] + ".jo", 0, 0, 0)
    # insert an "offset" joint for proper ik spline twist
    ik[7] = mc.joint(p=[i * 0.5 for i in map(add, positions[4], positions[5])],
                     n=name + "_ik6_off")
    mc.joint(ik[6], e=True, oj="xyz", sao="xup", ch=False, zso=True)
    ik[8] = mc.joint(p=positions[5], n=name + "_ik6")
    mc.joint(ik[7], e=True, oj="xyz", sao="xup", ch=False, zso=True)

    ik[1] = mc.duplicate(ik[0])[0]
    ik[1] = mc.rename(ik[1], name + "_ik1")
    mc.delete(mc.listRelatives(ik[1], pa=True, ad=True))
    ik[1] = mc.parent(ik[1], ik[0])[0]

    for n in ik:
        mc.setAttr(n + ".radius", radius * 0.5)
    for n in [ik[0], ik[3], ik[7]]:
        mc.setAttr(n + ".drawStyle", 2)

    #
    # controls
    #

    ctrl_grp = [None] * 5
    ctrl = [None] * 5
    ctrl_grp[0], ctrl[0] = common.control(name=name + "_ik1",
                                          parent=grp,
                                          position=ik[0],
                                          radius=radius * 2,
                                          lockAttr=["s", "v"],
                                          hideAttr=["sx", "sy", "sz", "v"])
    mc.addAttr(ctrl[0], ln="joints", at="bool", dv=True, k=True)
    mc.addAttr(ctrl[0], ln="editJoints", at="bool", k=True)

    ctrl_grp[1], ctrl[1] = common.control(name=name + "_ik2",
                                          parent=ctrl[0],
                                          position=ik[2],
                                          radius=radius * 2,
                                          lockAttr=["s", "v"],
                                          hideAttr=["sx", "sy", "sz", "v"])

    ctrl_grp[2], ctrl[2] = common.control(name=name + "_ik3",
                                          parent=ctrl[1],
                                          position=ik[4],
                                          radius=radius * 2,
                                          lockAttr=["s", "v"],
                                          hideAttr=["sx", "sy", "sz", "v"])
    mc.addAttr(ctrl[2], ln="stretchAbove", at="bool", k=True)
    mc.addAttr(ctrl[2], ln="stretchBelow", at="bool", k=True)

    ctrl_grp[3], ctrl[3] = common.control(name=name + "_ik5",
                                          parent=ctrl[2],
                                          position=ik[6],
                                          radius=radius,
                                          lockAttr=["s", "v"],
                                          hideAttr=["sx", "sy", "sz", "v"])
    anchor = mc.createNode("transform", n="spine_ik4_anchor", p=ik[4])
    mc.delete(mc.parentConstraint(ik[5], anchor))
    c = mc.parentConstraint(anchor, ctrl_grp[3], mo=True)[0]
    mc.rename(c, ctrl_grp[3] + "_parcon")

    ctrl_grp[4], ctrl[4] = common.control(name=name + "_ik6",
                                          parent=ctrl[3],
                                          position=ik[8],
                                          radius=radius,
                                          lockAttr=["s", "v"],
                                          hideAttr=["sx", "sy", "sz", "v"])
    mc.addAttr(ctrl[4], ln="stretch", at="bool", k=True)

    md = mc.createNode("multiplyDivide")
    mc.setAttr(md + ".operation", 2)
    mc.setAttr(md + ".input1X", 1)
    mc.connectAttr(ik[4] + ".sx", md + ".input2X")
    mc.connectAttr(md + ".outputX", anchor + ".sx")

    #
    # joints, part 2
    #

    c = mc.orientConstraint(ctrl[2], ik[4], mo=True)[0]
    mc.rename(c, ik[4] + "_oricon")

    #
    # ik handles
    #

    mc.select(ik[0], ik[4])
    ikh, eff, crv = mc.ikHandle(sol="ikSplineSolver")
    crv = mc.parent(crv, grp)[0]
    mc.setAttr(crv + ".it", False)
    mc.setAttr(ikh + ".dTwistControlEnable", True)
    mc.setAttr(ikh + ".dWorldUpType", 4)
    mc.setAttr(ikh + ".dWorldUpAxis", 3)
    mc.setAttr(ikh + ".dWorldUpVector", 0, 0, -1)
    mc.setAttr(ikh + ".dWorldUpVectorEnd", 0, 0, -1)
    ikh = mc.parent(ikh, grp)[0]
    mc.hide(ikh, crv)
    ikh = mc.rename(ikh, name + "_ikh#")
    mc.rename(eff, name + "_eff#")

    # adjust "offset" joint position for proper ik spline twist
    mc.move(positions[2][0], positions[2][1], positions[2][2],
            ik[3] + ".scalePivot", ik[3] + ".rotatePivot")
    mc.move(0,
            -0.001,
            0,
            ik[3] + ".scalePivot",
            ik[3] + ".rotatePivot",
            r=True)

    c1 = mc.cluster(crv + ".cv[0]")[1]
    c2 = mc.cluster(crv + ".cv[1]", crv + ".cv[2]")[1]
    c3 = mc.cluster(crv + ".cv[3]")[1]
    mc.hide(c1, c2, c3)
    mc.parent(c1, ctrl[0])
    mc.parent(c2, ctrl[1])
    mc.parent(c3, ctrl[2])

    mc.connectAttr(ctrl[0] + ".worldMatrix", ikh + ".dWorldUpMatrix")
    mc.connectAttr(ctrl[2] + ".worldMatrix", ikh + ".dWorldUpMatrixEnd")

    mc.select(ik[5], ik[8])
    ikh, eff, crv2 = mc.ikHandle(sol="ikSplineSolver")
    crv2 = mc.parent(crv2, grp)[0]
    mc.setAttr(crv2 + ".t", 0, 0, 0)
    mc.setAttr(crv2 + ".r", 0, 0, 0)
    mc.setAttr(crv2 + ".s", 1, 1, 1)
    mc.setAttr(crv2 + ".it", False)
    mc.setAttr(ikh + ".dTwistControlEnable", True)
    mc.setAttr(ikh + ".dWorldUpType", 4)
    mc.setAttr(ikh + ".dWorldUpAxis", 3)
    mc.setAttr(ikh + ".dWorldUpVector", 0, 0, -1)
    mc.setAttr(ikh + ".dWorldUpVectorEnd", 0, 0, -1)
    ikh = mc.parent(ikh, grp)[0]
    mc.hide(ikh, crv2)
    ikh = mc.rename(ikh, name + "_ikh#")
    mc.rename(eff, name + "_eff#")

    # adjust "offset" joint position for proper ik spline twist
    mc.move(positions[5][0], positions[5][1], positions[5][2],
            ik[7] + ".scalePivot", ik[7] + ".rotatePivot")
    mc.move(0,
            -0.001,
            0,
            ik[7] + ".scalePivot",
            ik[7] + ".rotatePivot",
            r=True)

    c1 = mc.cluster(crv2 + ".cv[0]")[1]
    c2 = mc.cluster(crv2 + ".cv[1]", crv2 + ".cv[2]")[1]
    c3 = mc.cluster(crv2 + ".cv[3]")[1]
    mc.hide(c1, c2, c3)
    mc.parent(c1, ik[4])
    mc.parent(c2, ctrl[3])
    mc.parent(c3, ctrl[4])

    mc.connectAttr(ctrl[2] + ".worldMatrix", ikh + ".dWorldUpMatrix")
    mc.connectAttr(ctrl[4] + ".worldMatrix", ikh + ".dWorldUpMatrixEnd")

    mc.delete(mc.ls(typ="tweak"))

    c = mc.orientConstraint(ctrl[0], ik[1], mo=True)[0]
    mc.rename(c, ik[1] + "_oricon")
    c = mc.orientConstraint(ctrl[4], ik[8], mo=True)[0]
    mc.rename(c, ik[8] + "_oricon")

    #
    # stretch math
    #

    ci = mc.createNode("curveInfo")
    mc.connectAttr(crv + ".worldSpace", ci + ".inputCurve")

    ci2 = mc.createNode("curveInfo")
    mc.connectAttr(crv2 + ".worldSpace", ci2 + ".inputCurve")

    n = mc.listRelatives(crv, pa=True, s=True)[1]
    tg = mc.createNode("transformGeometry")
    mc.connectAttr(n + ".worldSpace", tg + ".inputGeometry")
    mc.connectAttr(grp + ".worldMatrix", tg + ".transform")
    ci3 = mc.createNode("curveInfo")
    mc.connectAttr(tg + ".outputGeometry", ci3 + ".inputCurve")

    n = mc.listRelatives(crv2, pa=True, s=True)[1]
    tg = mc.createNode("transformGeometry")
    mc.connectAttr(n + ".worldSpace", tg + ".inputGeometry")
    mc.connectAttr(grp + ".worldMatrix", tg + ".transform")
    ci4 = mc.createNode("curveInfo")
    mc.connectAttr(tg + ".outputGeometry", ci4 + ".inputCurve")

    md1 = mc.createNode("multiplyDivide")
    mc.setAttr(md1 + ".operation", 2)
    mc.connectAttr(ci + ".arcLength", md1 + ".input1X")
    mc.connectAttr(ci3 + ".arcLength", md1 + ".input2X")
    md2 = mc.createNode("multiplyDivide")
    mc.connectAttr(md1 + ".outputX", md2 + ".input1X")
    mc.connectAttr(ctrl[2] + ".stretchBelow", md2 + ".input2X")
    c = mc.createNode("condition")
    mc.setAttr(c + ".secondTerm", 1)
    mc.setAttr(c + ".operation", 3)
    mc.connectAttr(md1 + ".outputX", c + ".colorIfTrueR")
    mc.connectAttr(md2 + ".outputX", c + ".firstTerm")
    mc.connectAttr(c + ".outColorR", ik[0] + ".sx")
    mc.connectAttr(c + ".outColorR", ik[2] + ".sx")

    md2 = mc.createNode("multiplyDivide")
    mc.connectAttr(md1 + ".outputX", md2 + ".input1X")
    mc.connectAttr(ctrl[2] + ".stretchAbove", md2 + ".input2X")
    c = mc.createNode("condition")
    mc.setAttr(c + ".secondTerm", 1)
    mc.setAttr(c + ".operation", 3)
    mc.connectAttr(md1 + ".outputX", c + ".colorIfTrueR")
    mc.connectAttr(md2 + ".outputX", c + ".firstTerm")
    mc.connectAttr(c + ".outColorR", ik[4] + ".sx")

    md1 = mc.createNode("multiplyDivide")
    mc.setAttr(md1 + ".operation", 2)
    mc.connectAttr(ci2 + ".arcLength", md1 + ".input1X")
    mc.connectAttr(ci4 + ".arcLength", md1 + ".input2X")
    md2 = mc.createNode("multiplyDivide")
    mc.connectAttr(md1 + ".outputX", md2 + ".input1X")
    mc.connectAttr(ctrl[4] + ".stretch", md2 + ".input2X")
    c = mc.createNode("condition")
    mc.setAttr(c + ".secondTerm", 1)
    mc.setAttr(c + ".operation", 3)
    mc.connectAttr(md1 + ".outputX", c + ".colorIfTrueR")
    mc.connectAttr(md2 + ".outputX", c + ".firstTerm")
    mc.connectAttr(c + ".outColorR", ik[5] + ".sx")
    mc.connectAttr(c + ".outColorR", ik[6] + ".sx")

    j = 0
    jnt = [None] * 6
    for i in range(1, 9):
        if i == 3 or i == 7: continue
        jnt[j] = mc.createNode("joint", n=name + "_jnt" + str(j + 1), p=grp)
        mc.setAttr(jnt[j] + ".radius", radius * 0.5)
        mc.connectAttr(ctrl[0] + ".joints", jnt[j] + ".v")
        c = mc.parentConstraint(ik[i], jnt[j])[0]
        mc.rename(c, name + "_jnt" + str(j) + "_parcon")
        j += 1

    # selection sets
    common.sets(name, jnt, None, ctrl)

    # selectable joints
    common.selectable(ctrl[0] + ".editJoints", jnt)

    if tmp: mc.delete(tmp)
    ik[0] = mc.parent(ik[0], grp)[0]
    mc.hide(ik[0])
    mc.select(grp)
    mc.dgdirty(a=True)

    return grp
コード例 #6
0
ファイル: twist.py プロジェクト: PearseT/Maya_scripts
def main(name="twist",
         control=None,
         parent=None,
         stable=None,
         _twist=None,
         scale=None,
         aim=(1, 0, 0),
         up=(0, 1, 0),
         wu=(0, 0, 0),
         wuo=None,
         count=4):

    if not control:
        control = common.control(name=name, color=13)[1]
        addControlToSets = True
    else:
        addControlToSets = False

    if not mc.objExists(control + ".joints"):
        mc.addAttr(control, ln="joints", at="bool", dv=True, k=True)
    if not mc.objExists(control + ".editJoints"):
        mc.addAttr(control, ln="editJoints", at="bool", k=True)

    if count < 1: count = 1
    elif count > 4: count = 4
    count += 1

    tw = [None] * count
    tw[0] = mc.duplicate(stable)[0]
    try:
        mc.delete(mc.listRelatives(tw[0], pa=True, c=True))
    except:
        pass
    l = mc.listConnections(tw[0] + ".message", s=False, d=True, p=True) or []
    for na in l:
        mc.disconnectAttr(tw[0] + ".message", na)
    for a in [
            "t", "tx", "ty", "tz", "r", "rx", "ry", "rz", "s", "sx", "sy",
            "sz", "v", "radius", "template"
    ]:
        mc.setAttr(tw[0] + "." + a, l=False, k=True)
    mc.setAttr(tw[0] + ".radius", mc.getAttr(tw[0] + ".radius"))
    mc.setAttr(tw[0] + ".drawStyle", 0)
    mc.setAttr(tw[0] + ".template", False)
    tw[0] = mc.rename(tw[0], name + "_twr#")
    try:
        tw[0] = mc.parent(tw[0], parent)[0]
    except:
        pass
    tw[1] = mc.duplicate(tw[0])[0]
    mc.setAttr(tw[1] + ".drawStyle", 2)
    for i in range(2, count):
        tw[i] = mc.duplicate(tw[0])[0]

    if not wuo:
        c = mc.aimConstraint(_twist,
                             tw[0],
                             aim=aim,
                             u=up,
                             wut="vector",
                             wu=wu,
                             mo=True)[0]
    else:
        c = mc.aimConstraint(_twist,
                             tw[0],
                             aim=aim,
                             u=up,
                             wut="objectrotation",
                             wu=wu,
                             wuo=wuo,
                             mo=True)[0]
    mc.rename(c, tw[0] + "_aimcon")
    c = mc.aimConstraint(_twist,
                         tw[1],
                         aim=aim,
                         u=up,
                         wut="objectrotation",
                         wu=(0, 1, 0),
                         wuo=_twist,
                         mo=True)[0]
    mc.rename(c, tw[0] + "_aimcon")

    if count == 5:
        wgt = [0.75, 0.5, 0.25]
    elif count == 4:
        wgt = [0.6666, 0.3333]
    else:  # count == 3
        wgt = [0.5]

    for i in range(2, count):
        tw[i] = mc.rename(tw[i], name + "_tw#")
        c = mc.pointConstraint(tw[0], _twist, tw[i])[0]
        mc.setAttr(c + "." + tw[0].split("|")[-1] + "W0", wgt[i - 2])
        mc.setAttr(c + "." + _twist.split("|")[-1] + "W1", 1 - wgt[i - 2])
        mc.delete(c)
        c = mc.orientConstraint(tw[0], tw[1], tw[i])[0]
        mc.setAttr(c + "." + tw[0].split("|")[-1] + "W0", wgt[i - 2])
        mc.setAttr(c + "." + tw[1].split("|")[-1] + "W1", 1 - wgt[i - 2])
        mc.setAttr(c + ".interpType", 2)
        mc.rename(c, tw[i] + "_oricon")

    if count > 2: mc.parent(tw[2:], tw[1])
    mc.select(tw[0], tw[1])

    mc.connectAttr(scale + ".s", tw[1] + ".s")

    mc.connectAttr(control + ".joints", tw[0] + ".v")
    mc.connectAttr(control + ".joints", tw[1] + ".v")

    tw.pop(1)

    # selection sets
    if addControlToSets: common.sets(name, tw, [control], None)
    else: common.sets(name, tw, None, None)

    # selectable joints
    common.selectable(control + ".editJoints", tw)

    mc.select(control)
    mc.dgdirty(a=True)

    return control
コード例 #7
0
def main(name="leg",
         positions=None,
         inheritTemplateRotations=False,
         footWidth=1,
         radius=1,
         ikShape="cube"):

    # create template joints if needed
    tmp = None
    if not positions:
        positions = template(name=name)
        tmp = positions[:]  # copy

    # positions/orientations from template transforms/joints
    rotations = [None] * 6
    for i in range(6):
        if type(positions[i]) == str or type(positions[i]) == unicode:
            if inheritTemplateRotations:
                rotations[i] = mc.xform(positions[i], q=True, ro=True, ws=True)
            positions[i] = mc.xform(positions[i], q=True, rp=True, ws=True)

    grp = mc.createNode("transform", n=name)

    #
    # ik joints
    #

    ik = [None] * 5
    for i in range(5):
        ik[i] = mc.createNode("joint", n=name + "_ik" + str(i + 1))
        mc.setAttr(ik[i] + ".t", positions[i][0], positions[i][1],
                   positions[i][2])
        if inheritTemplateRotations:
            mc.setAttr(ik[i] + ".r", rotations[i][0], rotations[i][1],
                       rotations[i][2])

    if not inheritTemplateRotations:
        if positions[0][0] >= -0.001:
            mc.delete(
                mc.aimConstraint(ik[1],
                                 ik[0],
                                 aim=(1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[2]))
            mc.delete(
                mc.aimConstraint(ik[2],
                                 ik[1],
                                 aim=(1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[0]))
            mc.delete(
                mc.aimConstraint(ik[1],
                                 ik[2],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[0]))
            mc.delete(
                mc.aimConstraint(ik[4],
                                 ik[3],
                                 aim=(1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[1]))
            mc.delete(
                mc.aimConstraint(ik[3],
                                 ik[4],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[1]))
        else:
            mc.delete(
                mc.aimConstraint(ik[1],
                                 ik[0],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[2]))
            mc.delete(
                mc.aimConstraint(ik[2],
                                 ik[1],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[0]))
            mc.delete(
                mc.aimConstraint(ik[1],
                                 ik[2],
                                 aim=(1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=ik[0]))
            mc.delete(
                mc.aimConstraint(ik[4],
                                 ik[3],
                                 aim=(-1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[1]))
            mc.delete(
                mc.aimConstraint(ik[3],
                                 ik[4],
                                 aim=(1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=ik[1]))

    for i in range(5):
        r = mc.getAttr(ik[i] + ".r")[0]
        mc.setAttr(ik[i] + ".jo", r[0], r[1], r[2])
        mc.setAttr(ik[i] + ".r", 0, 0, 0)
        mc.setAttr(ik[i] + ".radius", radius * 0.5)

    #
    # ik controls
    #

    ik_ctrl_grp = [None] * 2
    ik_ctrl = [None] * 2
    ik_ctrl_grp[0], ik_ctrl[0] = common.control(
        name=name + "_ik1",
        parent=grp,
        position=ik[2],
        color=6,
        radius=radius,
        shape=ikShape,
        lockAttr=["s", "v"],
        hideAttr=["sx", "sy", "sz", "v"])
    mc.addAttr(ik_ctrl[0], ln="joints", at="bool", dv=True, k=True)
    mc.addAttr(ik_ctrl[0], ln="editJoints", at="bool", k=True)
    mc.addAttr(ik_ctrl[0], ln="fkControls", at="bool", dv=True, k=True)
    mc.addAttr(ik_ctrl[0], ln="ikControls", at="bool", dv=True, k=True)
    mc.addAttr(ik_ctrl[0], ln="stretch", at="bool", k=True)
    mc.addAttr(ik_ctrl[0], ln="footRoll", at="double", k=True)
    mc.addAttr(ik_ctrl[0], ln="footSide", at="double", k=True)
    mc.addAttr(ik_ctrl[0], ln="heelTwist", at="double", k=True)
    mc.addAttr(ik_ctrl[0], ln="ballLift", at="double", k=True)
    mc.addAttr(ik_ctrl[0], ln="toeLift", at="double", k=True)
    mc.addAttr(ik_ctrl[0], ln="toeTwist", at="double", k=True)

    heel = mc.createNode("transform", n=name + "_heel_grp")
    mc.setAttr(heel + ".t", positions[5][0], positions[5][1], positions[5][2])
    heel = mc.parent(heel, ik_ctrl[0])[0]
    mc.setAttr(heel + ".r", 0, 0, 0)
    mc.connectAttr(ik_ctrl[0] + ".footRoll", heel + ".rx")
    mc.connectAttr(ik_ctrl[0] + ".heelTwist", heel + ".ry")
    mc.transformLimits(heel, rx=(0, 0), erx=(False, True))
    mc.hide(heel)

    side_rt = mc.createNode("transform", n=name + "_side_rt_grp", p=heel)
    mc.setAttr(side_rt + ".r", 0, 0, 0)
    mc.delete(mc.pointConstraint(ik[3], side_rt))
    mc.setAttr(side_rt + ".tx", -footWidth)
    mc.connectAttr(ik_ctrl[0] + ".footSide", side_rt + ".rz")
    mc.transformLimits(side_rt, rz=(0, 0), erz=(True, False))

    side_lf = mc.createNode("transform", n=name + "_side_lf_grp", p=side_rt)
    mc.delete(mc.pointConstraint(ik[3], side_lf))
    mc.setAttr(side_lf + ".r", 0, 0, 0)
    mc.setAttr(side_lf + ".tx", footWidth * 2)
    mc.connectAttr(ik_ctrl[0] + ".footSide", side_lf + ".rz")
    mc.transformLimits(side_lf, rz=(0, 0), erz=(False, True))

    toe = mc.createNode("transform", n=name + "_toe_grp", p=side_lf)
    mc.delete(mc.pointConstraint(ik[4], toe))
    mc.setAttr(toe + ".r", 0, 0, 0)
    mc.connectAttr(ik_ctrl[0] + ".toeTwist", toe + ".ry")
    mc.setDrivenKeyframe(toe + ".rx",
                         v=0,
                         dv=45,
                         cd=ik_ctrl[0] + ".footRoll",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(toe + ".rx",
                         v=45,
                         dv=90,
                         cd=ik_ctrl[0] + ".footRoll",
                         itt="linear",
                         ott="linear")

    ball = mc.createNode("transform", n=name + "_ball_grp", p=toe)
    mc.delete(mc.pointConstraint(ik[3], ball))
    mc.setAttr(ball + ".r", 0, 0, 0)
    #	mc.connectAttr(ik_ctrl[0]+".ballLift", ball+".rax")
    mc.setDrivenKeyframe(ball + ".rx",
                         v=0,
                         dv=0,
                         cd=ik_ctrl[0] + ".footRoll",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(ball + ".rx",
                         v=45,
                         dv=45,
                         cd=ik_ctrl[0] + ".footRoll",
                         itt="linear",
                         ott="linear")

    ik_ctrl_grp[1], ik_ctrl[1] = common.control(
        name=name + "_pv",
        parent=grp,
        position=ik[1],
        rotation=ik[1],
        normal=(0, 0, 1),
        color=6,
        radius=radius * 0.25,
        shape=ikShape,
        lockAttr=["r", "s"],
        hideAttr=["rx", "ry", "rz", "sx", "sy", "sz", "v"])
    if positions[0][0] <= -0.001:
        direction = 1 if inheritTemplateRotations else -1
        mc.move(0,
                0,
                direction * 3 * radius,
                ik_ctrl_grp[1],
                os=True,
                r=True,
                wd=True)
    else:
        mc.move(0, 0, 3 * radius, ik_ctrl_grp[1], os=True, r=True, wd=True)
    mc.setAttr(ik_ctrl_grp[1] + ".r", 0, 0, 0)
    mc.connectAttr(ik_ctrl[0] + ".ikControls", ik_ctrl_grp[1] + ".v")

    #
    # ik stretch joints
    #

    ik2 = [None] * 3
    for i in range(3):
        ik2[i] = mc.duplicate(ik[i])[0]
        ik2[i] = mc.rename(ik2[i], name + "_ik" + str(i + 1) + "_str")
        if i == 0: ik2[i] = mc.parent(ik2[i], grp)[0]
        else: ik2[i] = mc.parent(ik2[i], ik2[i - 1])[0]
        c = mc.parentConstraint(ik[i], ik2[i])[0]
        mc.rename(c, name + "_ik" + str(i + 1) + "_parcon")
        mc.setAttr(ik2[i] + ".jo", 0, 0, 0)

    #
    # fk joints and controls
    #

    fk_ctrl = [None] * 4
    jnt = [None] * 4
    for i in range(4):
        # fk controls
        fk_ctrl[i] = mc.duplicate(ik[i])[0]
        fk_ctrl[i] = mc.rename(fk_ctrl[i], name + "_fk" + str(i + 1) + "_ctrl")
        if i != 3:
            mc.connectAttr(ik2[i] + ".t", fk_ctrl[i] + ".t")
            mc.connectAttr(ik2[i] + ".r", fk_ctrl[i] + ".jo")
        else:
            mc.connectAttr(ik[i] + ".t", fk_ctrl[i] + ".t")
            mc.connectAttr(ik[i] + ".jo", fk_ctrl[i] + ".jo")
        mc.setAttr(fk_ctrl[i] + ".r", 0, 0, 0)

        common.control(addShapeTo=fk_ctrl[i],
                       normal=(1, 0, 0),
                       color=13,
                       radius=radius * 0.7)
        mc.connectAttr(ik_ctrl[0] + ".fkControls",
                       mc.listRelatives(fk_ctrl[i], pa=True, s=True)[0] + ".v")
        if i: fk_ctrl[i] = mc.parent(fk_ctrl[i], fk_ctrl[i - 1])[0]
        else: fk_ctrl[i] = mc.parent(fk_ctrl[i], grp)[0]
        mc.setAttr(fk_ctrl[i] + ".drawStyle", 2)

        # fk joints
        jnt[i] = mc.duplicate(ik[i])[0]
        jnt[i] = mc.parent(jnt[i], fk_ctrl[i])[0]
        jnt[i] = mc.rename(jnt[i], name + "_jnt" + str(i + 1))
        for a in ["t", "r", "jo"]:
            mc.setAttr(jnt[i] + "." + a, 0, 0, 0)
        mc.connectAttr(ik_ctrl[0] + ".joints", jnt[i] + ".v")
        mc.setAttr(fk_ctrl[i] + ".drawStyle", 2)

    rev = mc.createNode("reverse")
    mc.connectAttr(ik_ctrl[0] + ".toeLift", rev + ".inputX")
    mc.connectAttr(rev + ".outputX", jnt[3] + ".ry")

    mc.setDrivenKeyframe(fk_ctrl[3] + ".ray",
                         v=30,
                         dv=-30,
                         cd=ik_ctrl[0] + ".footRoll",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(fk_ctrl[3] + ".ray",
                         v=0,
                         dv=0,
                         cd=ik_ctrl[0] + ".footRoll",
                         itt="linear",
                         ott="linear")
    mc.setDrivenKeyframe(fk_ctrl[3] + ".ray",
                         v=-45,
                         dv=45,
                         cd=ik_ctrl[0] + ".footRoll",
                         itt="linear",
                         ott="linear")

    #
    # ik handles
    #

    for i in range(1, 5):
        ik[i] = mc.parent(ik[i], ik[i - 1])[0]
    mc.select(ik)
    mc.SetPreferredAngle()

    common.ikHandle(name + "_ball", ik[2], ik[3], parent=ball)
    common.ikHandle(name + "_toe", ik[3], ik[4], parent=toe)
    ikh = common.ikHandle(name, ik[0], ik[2], parent=ball)[0]
    pvc = mc.poleVectorConstraint(ik_ctrl[1], ikh)[0]
    mc.rename(pvc, name + "_pvcon")

    #
    # stretch math
    #

    ik1_jnt_grp = mc.createNode("transform", n=ik[0] + "_grp", p=grp)
    mc.delete(mc.pointConstraint(ik[0], ik1_jnt_grp))
    n = mc.duplicate(ik1_jnt_grp)[0]
    n = mc.rename(n, "str_grp")
    db1 = mc.createNode("distanceBetween")
    mc.connectAttr(ik1_jnt_grp + ".worldMatrix", db1 + ".inMatrix1")
    mc.connectAttr(ik_ctrl[0] + ".worldMatrix", db1 + ".inMatrix2")
    db2 = mc.createNode("distanceBetween")
    mc.connectAttr(n + ".worldMatrix", db2 + ".inMatrix1")
    mc.connectAttr(ik_ctrl_grp[0] + ".worldMatrix", db2 + ".inMatrix2")
    md1 = mc.createNode("multiplyDivide")
    mc.setAttr(md1 + ".operation", 2)
    mc.connectAttr(db1 + ".distance", md1 + ".input1X")
    mc.connectAttr(db2 + ".distance", md1 + ".input2X")
    md2 = mc.createNode("multiplyDivide")
    mc.connectAttr(md1 + ".outputX", md2 + ".input1X")
    mc.connectAttr(ik_ctrl[0] + ".stretch", md2 + ".input2X")
    c = mc.createNode("condition")
    mc.setAttr(c + ".secondTerm", 1)
    mc.setAttr(c + ".operation", 3)
    mc.connectAttr(md1 + ".outputX", c + ".colorIfTrueR")
    mc.connectAttr(md2 + ".outputX", c + ".firstTerm")

    mc.connectAttr(c + ".outColorR", ik[0] + ".sx")
    mc.connectAttr(c + ".outColorR", ik[1] + ".sx")

    # offset the stretch group to enable full extension
    # of the leg before it starts stretching
    db3 = mc.createNode("distanceBetween")
    mc.connectAttr(ik[0] + ".worldMatrix", db3 + ".inMatrix1")
    mc.connectAttr(ik[1] + ".worldMatrix", db3 + ".inMatrix2")
    d = mc.getAttr(db3 + ".distance")
    mc.connectAttr(ik[2] + ".worldMatrix", db3 + ".inMatrix1", f=True)
    d += mc.getAttr(db3 + ".distance") - mc.getAttr(db1 + ".distance")
    mc.delete(db3)
    p = mc.xform(ik[0], q=True, rp=True, ws=True)
    p2 = mc.xform(ik[2], q=True, rp=True, ws=True)
    p[0] -= p2[0]
    p[1] -= p2[1]
    p[2] -= p2[2]
    m = math.sqrt(p[0] * p[0] + p[1] * p[1] + p[2] * p[2])
    p[0] /= m
    p[1] /= m
    p[2] /= m
    p2 = mc.getAttr(n + ".t")[0]
    mc.setAttr(n + ".t", p2[0] + p[0] * d, p2[1] + p[1] * d, p2[2] + p[2] * d)

    #
    # lock and hide attributes
    #

    for i in range(4):
        if i == 0 or i == 2:
            for a in ["tx", "ty", "tz", "sx", "sy", "sz", "v", "radius"]:
                mc.setAttr(fk_ctrl[i] + "." + a, l=True, k=False, cb=False)
        else:
            for a in [
                    "tx", "ty", "tz", "rx", "rz", "sx", "sy", "sz", "v",
                    "radius"
            ]:
                mc.setAttr(fk_ctrl[i] + "." + a, l=True, k=False, cb=False)

    # selection sets
    common.sets(name, jnt, fk_ctrl, ik_ctrl)

    # selectable joints
    common.selectable(ik_ctrl[0] + ".editJoints", jnt)

    # fk to ik bake ready
    for i in range(3):
        mc.addAttr(ik_ctrl[0], ln="fk" + str(i + 1), at="message", h=True)
        mc.addAttr(fk_ctrl[i], ln="fk", at="message", h=True)
        mc.connectAttr(ik_ctrl[0] + ".fk" + str(i + 1), fk_ctrl[i] + ".fk")
    mc.addAttr(ik_ctrl[0], ln="ik1", at="message", h=True)
    mc.addAttr(ik_ctrl[1], ln="ik", at="message", h=True)
    mc.connectAttr(ik_ctrl[0] + ".ik1", ik_ctrl[1] + ".ik")

    # organize
    if tmp: mc.delete(tmp)
    mc.hide(ik[0], ik2[0])
    mc.parent(ik[0], ik1_jnt_grp)
    mc.select(grp)
    mc.dgdirty(a=True)

    return grp
コード例 #8
0
ファイル: chassis.py プロジェクト: PearseT/Maya_scripts
def main(wheels=None, parent=None, radius=1):

    if wheels == None: wheels = mc.ls(sl=True, o=True) or []
    if len(wheels) < 2: return

    grp = mc.createNode("transform", n="chassis", ss=True)

    bb = mc.exactWorldBoundingBox(wheels)
    tx = (bb[3] + bb[0]) * 0.5
    ty = (bb[4] + bb[1]) * 0.5
    tz = (bb[5] + bb[2]) * 0.5
    sx = (bb[3] - bb[0]) * 2 * radius
    sz = (bb[5] - bb[2]) * 2 * radius
    grp2, ctrl = common.control(name="chassis",
                                position=[tx, ty, tz],
                                radius=[sx, sz],
                                shape="square",
                                parent=grp,
                                lockAttr="v",
                                hideAttr="v")
    mc.addAttr(ctrl, ln="joints", at="bool", dv=True, k=True)
    mc.addAttr(ctrl, ln="editJoints", at="bool", k=True)
    mc.addAttr(ctrl, ln="controls", at="bool", dv=True, k=True)
    mc.addAttr(ctrl, ln="placers", at="bool", k=True)
    mc.addAttr(ctrl, ln="wheels", at="bool", dv=True, k=True)
    mc.addAttr(ctrl, ln="terrain", at="bool", dv=True, k=True)
    mc.addAttr(ctrl, ln="editTerrain", at="bool", k=True)
    mc.addAttr(ctrl, ln="terrainDetection", at="bool", dv=True, k=True)

    jnt = mc.createNode("joint", n="chassis_jnt", p=ctrl, ss=True)
    mc.setAttr(jnt + ".radius", radius * 0.5)
    mc.connectAttr(ctrl + ".joints", jnt + ".v")
    common.selectable(ctrl + ".editJoints", jnt)
    common.sets("chassis", jnt, None, None)

    fr_wheels = []
    bk_wheels = []
    lf_wheels = []
    rt_wheels = []
    for n in wheels:
        name = n.replace("_grp", "")
        mc.connectAttr(ctrl + ".joints", name + "_ctrl.joints")
        mc.setAttr(name + "_ctrl.joints", k=False)
        mc.connectAttr(ctrl + ".editJoints", name + "_ctrl.editJoints")
        mc.setAttr(name + "_ctrl.editJoints", k=False)
        mc.connectAttr(ctrl + ".controls", name + "_ctrl.v")
        mc.setAttr(name + "_ctrl.v", k=False)
        mc.connectAttr(ctrl + ".controls",
                       name + "_placer_ctrlShape.overrideVisibility")
        mc.connectAttr(ctrl + ".placers", name + "_ctrl.placer")
        mc.setAttr(name + "_ctrl.placer", k=False)
        mc.connectAttr(ctrl + ".wheels", name + "_ctrl.wheel")
        mc.setAttr(name + "_ctrl.wheel", k=False)
        mc.connectAttr(ctrl + ".terrain", name + "_ctrl.terrain")
        mc.setAttr(name + "_ctrl.terrain", k=False)
        mc.connectAttr(ctrl + ".editTerrain", name + "_ctrl.editTerrain")
        mc.setAttr(name + "_ctrl.editTerrain", k=False)
        mc.connectAttr(ctrl + ".terrainDetection",
                       name + "_ctrl.terrainDetection")
        mc.setAttr(name + "_ctrl.terrainDetection", k=False)
        p = mc.xform(n, q=True, ws=True, rp=True)
        if p[0] >= 0: lf_wheels.append(n)
        else: rt_wheels.append(n)
        if p[2] >= 0: fr_wheels.append(n)
        else: bk_wheels.append(n)

    bb = mc.exactWorldBoundingBox(fr_wheels)
    tx = (bb[3] + bb[0]) * 0.5
    ty = bb[4] * 1.25
    tz = (bb[5] + bb[2]) * 0.5
    r = (bb[3] + bb[0]) * 0.25
    if r < (bb[4] + bb[1]) * 0.25: r = (bb[4] + bb[1]) * 0.25
    if r < (bb[5] + bb[2]) * 0.25: r = (bb[5] + bb[2]) * 0.25
    grp3, ctrl2 = common.control(
        name="steering",
        position=[tx, ty, tz],
        radius=radius * r,
        parent=grp,
        lockAttr=["tx", "ty", "tz", "rx", "rz", "sx", "sy", "sz"],
        hideAttr=["tx", "ty", "tz", "rx", "rz", "sx", "sy", "sz", "v"])
    mc.connectAttr(ctrl + ".controls", ctrl2 + ".v")
    mc.transformLimits(ctrl2, ry=(-55, 55), ery=(1, 1))

    fr_pos_grp = mc.createNode("transform",
                               p=grp,
                               n=name + "_fr_pos_grp",
                               ss=True)
    bk_pos_grp = mc.createNode("transform",
                               p=grp,
                               n=name + "_bk_pos_grp",
                               ss=True)
    lf_pos_grp = mc.createNode("transform",
                               p=grp,
                               n=name + "_lf_pos_grp",
                               ss=True)
    rt_pos_grp = mc.createNode("transform",
                               p=grp,
                               n=name + "_rt_pos_grp",
                               ss=True)
    fr_lf_pos_grp = mc.createNode("transform",
                                  p=grp,
                                  n=name + "_fr_lf_aim_grp",
                                  ss=True)
    bk_lf_pos_grp = mc.createNode("transform",
                                  p=grp,
                                  n=name + "_bk_lf_aim_grp",
                                  ss=True)
    bk_rt_pos_grp = mc.createNode("transform",
                                  p=grp,
                                  n=name + "_bk_rt_aim_grp",
                                  ss=True)
    fr_rt_pos_grp = mc.createNode("transform",
                                  p=grp,
                                  n=name + "_fr_rt_aim_grp",
                                  ss=True)

    for n in wheels:
        wheel_ctrl = n.replace("_grp", "_ctrl")
        if n in fr_wheels:
            mc.connectAttr(ctrl2 + ".ry", wheel_ctrl + ".ry")
            mc.setAttr(wheel_ctrl + ".ry", k=False, l=True)
        else:
            mc.setAttr(wheel_ctrl + ".tilt", 0)
        mc.pointConstraint(wheel_ctrl, grp2, skip=("x"))
        if n in lf_wheels: mc.pointConstraint(wheel_ctrl, lf_pos_grp)
        elif n in rt_wheels: mc.pointConstraint(wheel_ctrl, rt_pos_grp)
        if n in fr_wheels:
            mc.pointConstraint(wheel_ctrl, fr_pos_grp, skip=("x"))
        elif n in bk_wheels:
            mc.pointConstraint(wheel_ctrl, bk_pos_grp)
            mc.setAttr(wheel_ctrl + ".ry", k=False, l=True)
        mc.connectAttr(grp2 + ".rz", wheel_ctrl + ".rz")
        mc.setAttr(wheel_ctrl + ".rz", k=False, l=True)

    mc.pointConstraint(fr_pos_grp, bk_pos_grp, lf_pos_grp, rt_pos_grp,
                       fr_lf_pos_grp)
    mc.pointConstraint(fr_pos_grp, bk_pos_grp, lf_pos_grp, rt_pos_grp,
                       bk_lf_pos_grp)
    mc.pointConstraint(fr_pos_grp, bk_pos_grp, lf_pos_grp, rt_pos_grp,
                       bk_rt_pos_grp)
    mc.pointConstraint(fr_pos_grp, bk_pos_grp, lf_pos_grp, rt_pos_grp,
                       fr_rt_pos_grp)
    mc.aimConstraint(fr_pos_grp,
                     fr_lf_pos_grp,
                     aim=[0, 0, 1],
                     u=[1, 0, 0],
                     wut="object",
                     wuo=lf_pos_grp)
    mc.aimConstraint(lf_pos_grp,
                     bk_lf_pos_grp,
                     aim=[1, 0, 0],
                     u=[0, 0, -1],
                     wut="object",
                     wuo=bk_pos_grp)
    mc.aimConstraint(bk_pos_grp,
                     bk_rt_pos_grp,
                     aim=[0, 0, -1],
                     u=[-1, 0, 0],
                     wut="object",
                     wuo=rt_pos_grp)
    mc.aimConstraint(rt_pos_grp,
                     fr_rt_pos_grp,
                     aim=[-1, 0, 0],
                     u=[1, 0, 0],
                     wut="object",
                     wuo=fr_pos_grp)
    mc.orientConstraint(fr_lf_pos_grp,
                        bk_lf_pos_grp,
                        bk_rt_pos_grp,
                        fr_rt_pos_grp,
                        grp2,
                        mo=True)

    common.sets("chassis", None, [ctrl, ctrl2], None)

    mc.select(grp)
    mc.dgdirty(a=True)

    return grp, ctrl
コード例 #9
0
def main(name="hand",
         side="",
         control=None,
         parent=None,
         positions=None,
         inheritTemplateRotations=False,
         radius=1):

    # create template joints if needed
    tmp = None
    if not positions:
        positions = template(name=name)
        tmp = copy.deepcopy(positions)
    cnt = len(positions)
    if cnt == 0: return

    # positions from template transforms/joints
    rotations = {}
    for i in range(cnt):
        cnt2 = len(positions[i])
        if cnt2 < 3:
            raise Exception("At least 3 joints are needed to form a finger.")
        for j in range(cnt2):
            if type(positions[i][j]) == str or type(
                    positions[i][j]) == unicode:
                if inheritTemplateRotations:
                    if i not in rotations.viewkeys():
                        rotations[i] = [None] * cnt2
                    rotations[i][j] = mc.xform(positions[i][j],
                                               q=True,
                                               ro=True,
                                               ws=True)
                positions[i][j] = mc.xform(positions[i][j],
                                           q=True,
                                           rp=True,
                                           ws=True)

    if not control:
        center = [0, 0, 0]
        for i in range(cnt):
            center[0] += positions[i][0][0]
            center[1] += positions[i][0][1]
            center[2] += positions[i][0][2]
        center[0] /= cnt
        center[1] /= cnt
        center[2] /= cnt

        control = common.control(name=name,
                                 position=center,
                                 normal=(1, 0, 0),
                                 color=17,
                                 radius=radius * 2,
                                 lockAttr=("v"),
                                 hideAttr=("v"))[1]
        parent = control

    if not mc.objExists(control + ".joints"):
        mc.addAttr(control, ln="joints", at="bool", dv=True, k=True)
    if not mc.objExists(control + ".editJoints"):
        mc.addAttr(control, ln="editJoints", at="bool", dv=True, k=True)
    if not mc.objExists(control + ".fingerControls"):
        mc.addAttr(control, ln="fingerControls", at="bool", dv=True, k=True)
    if not mc.objExists(control + ".bend"):
        mc.addAttr(control, ln="bend", at="double", min=-10, max=10, k=True)
    if not mc.objExists(control + ".splay"):
        mc.addAttr(control, ln="splay", at="double", min=-10, max=10, k=True)

    md1 = mc.createNode("multiplyDivide")
    mc.connectAttr(control + ".bend", md1 + ".input1.input1X")
    mc.connectAttr(control + ".bend", md1 + ".input1.input1Y")
    mc.connectAttr(control + ".bend", md1 + ".input1.input1Z")
    mc.setAttr(md1 + ".input2X", 6)
    mc.setAttr(md1 + ".input2Y", 9)
    mc.setAttr(md1 + ".input2Z", 9)
    md2 = mc.createNode("multiplyDivide")
    mc.connectAttr(control + ".bend", md2 + ".input1.input1X")
    mc.connectAttr(control + ".bend", md2 + ".input1.input1Y")
    mc.connectAttr(control + ".bend", md2 + ".input1.input1Z")
    mc.setAttr(md2 + ".input2X", 8)
    mc.setAttr(md2 + ".input2Y", 11)
    mc.setAttr(md2 + ".input2Z", 7.5)

    jnt = [None] * cnt
    ctrl_grp = [None] * cnt
    ctrl = [None] * cnt
    for i in range(cnt):
        mc.select(cl=True)
        cnt2 = len(positions[i])
        jnt[i] = [None] * cnt2
        if cnt2 == 3: j = 0
        else: j = 1
        jnt[i][0] = mc.createNode("joint",
                                  n="finger_" + side + str(i + 1) + "_jnt" +
                                  str(j + 1))
        mc.setAttr(jnt[i][0] + ".t", positions[i][j][0], positions[i][j][1],
                   positions[i][j][2])
        jnt[i][1] = mc.createNode("joint",
                                  n="finger_" + side + str(i + 1) + "_jnt" +
                                  str(j + 2))
        mc.setAttr(jnt[i][1] + ".t", positions[i][j + 1][0],
                   positions[i][j + 1][1], positions[i][j + 1][2])
        jnt[i][2] = mc.createNode("joint",
                                  n="finger_" + side + str(i + 1) + "_jnt" +
                                  str(j + 3))
        mc.setAttr(jnt[i][2] + ".t", positions[i][j + 2][0],
                   positions[i][j + 2][1], positions[i][j + 2][2])

        if j:
            jnt[i][3] = mc.createNode("joint",
                                      n="finger_" + side + str(i + 1) +
                                      "_jnt1")
            mc.setAttr(jnt[i][3] + ".t", positions[i][0][0],
                       positions[i][0][1], positions[i][0][2])
            c = mc.aimConstraint(jnt[i][0],
                                 jnt[i][3],
                                 aim=(1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="objectrotation",
                                 wuo=parent)[0]
            mc.rename(c, jnt[i][3] + "_aimcon")
            jnt[i][3] = mc.parent(jnt[i][3], parent)[0]
            mc.setAttr(jnt[i][3] + ".radius", radius * 0.3)
            mc.connectAttr(control + ".joints", jnt[i][3] + ".v")

        if not inheritTemplateRotations:
            n = mc.createNode("transform")
            mc.delete(mc.pointConstraint(jnt[i][0], jnt[i][2], n))
            mc.delete(
                mc.aimConstraint(jnt[i][2],
                                 n,
                                 aim=(1, 0, 0),
                                 u=(0, 0, 1),
                                 wut="object",
                                 wuo=jnt[i][1]))
            mc.delete(
                mc.aimConstraint(jnt[i][1],
                                 jnt[i][0],
                                 aim=(1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=n))
            mc.delete(
                mc.aimConstraint(jnt[i][2],
                                 jnt[i][1],
                                 aim=(1, 0, 0),
                                 u=(0, 0, -1),
                                 wut="object",
                                 wuo=n))
            mc.delete(mc.orientConstraint(jnt[i][1], jnt[i][2]))
            mc.delete(n)
        else:
            for j in range(cnt2):
                mc.setAttr(jnt[i][j] + ".r", rotations[i][j][0],
                           rotations[i][j][1], rotations[i][j][2])

        r = mc.getAttr(jnt[i][0] + ".r")[0]
        mc.setAttr(jnt[i][0] + ".jo", r[0], r[1], r[2])
        mc.setAttr(jnt[i][0] + ".r", 0, 0, 0)
        r = mc.getAttr(jnt[i][1] + ".r")[0]
        mc.setAttr(jnt[i][1] + ".jo", r[0], r[1], r[2])
        mc.setAttr(jnt[i][1] + ".r", 0, 0, 0)
        mc.parent(jnt[i][1], jnt[i][0])
        r = mc.getAttr(jnt[i][2] + ".r")[0]
        mc.setAttr(jnt[i][2] + ".jo", r[0], r[1], r[2])
        mc.setAttr(jnt[i][2] + ".r", 0, 0, 0)
        mc.parent(jnt[i][2], jnt[i][1])

        ctrl_grp[i] = [None, None, None]
        ctrl[i] = [None, None, None]
        for j in range(3):
            if j:
                _parent = ctrl[i][j - 1]
                lockAttr = ("t", "rx", "rz", "s", "v")
                hideAttr = ("tx", "ty", "tz", "rx", "rz", "sx", "sy", "sz",
                            "v")
            else:
                _parent = parent
                lockAttr = ("s", "v")
                hideAttr = ("sx", "sy", "sz", "v")
            ctrl_grp[i][j], ctrl[i][j] = common.control(
                name="finger_" + side + str(i + 1) + "_fk" + str(j + 1),
                parent=_parent,
                position=jnt[i][j],
                rotation=jnt[i][j],
                normal=(1, 0, 0),
                color=13,
                radius=radius * 0.3,
                lockAttr=lockAttr,
                hideAttr=hideAttr,
                numOffsetGroups=True)
            mc.connectAttr(
                control + ".fingerControls",
                mc.listRelatives(ctrl[i][j], pa=True, s=True)[0] + ".v")
            jnt[i][j] = mc.parent(jnt[i][j], ctrl[i][j])[0]
            mc.setAttr(jnt[i][j] + ".radius", radius * 0.3)
            mc.connectAttr(control + ".joints", jnt[i][j] + ".v")

        # bend
        mc.connectAttr(md2 + ".outputX", ctrl_grp[i][0] + ".ray")
        mc.connectAttr(md2 + ".outputY", ctrl_grp[i][1] + ".ray")
        mc.connectAttr(md2 + ".outputZ", ctrl_grp[i][2] + ".ray")

    # splay
    delta = 3.0 / (cnt * 0.5)
    for i in range(cnt):
        md = mc.createNode("multiplyDivide")
        mc.connectAttr(control + ".splay", md + ".input1.input1X")
        if mc.xform(ctrl_grp[i][0], q=True, rp=True, ws=True)[0] >= -0.001:
            mc.setAttr(md + ".input2X", -3 + delta * i)
        else:
            mc.setAttr(md + ".input2X", 3 - delta * i)
        if len(jnt[i]) == 4:
            mc.connectAttr(md + ".outputX", ctrl_grp[i][0] + ".raz")
        else:
            mc.connectAttr(
                md + ".outputX",
                mc.listRelatives(ctrl[i][0], pa=True, p=True)[0] + ".raz")

    # selection sets
    if side: name += "_" + side
    common.sets(name, sum(jnt, []), sum(ctrl, []), None)

    # selectable joints
    common.selectable(control + ".editJoints", sum(jnt, []))

    if tmp: mc.delete(sum(tmp, []))
    mc.select(control)
    mc.dgdirty(a=True)

    return control