def createConnector(self, pt1, pt2, radius=0.5): # create cylinder and align between pt1/pt2 base_name = pt1 _logger.debug('Connector base name is %s' % base_name) viz_shape = pm.cylinder(n=base_name + 'Connector') pm.pointConstraint([pt1, pt2], viz_shape[0], mo=0) pm.aimConstraint(pt2, viz_shape[0], weight=1, upVector=(0, 1, 0), worldUpType="vector", offset=(0, 0, 0), aimVector=(1, 0, 0), worldUpVector=(0, 1, 0)) viz_direction = pm.cone(n=base_name + 'Direction', r=3 * radius) viz_shape[0].t >> viz_direction[0].t viz_shape[0].r >> viz_direction[0].r # measure distance between dist_node = pm.createNode('distanceBetween', n=base_name + '_distanceBetween') pm.connectAttr(pt1 + '.worldMatrix[0]', dist_node + '.inMatrix1', f=True) pm.connectAttr(pt2 + '.worldMatrix[0]', dist_node + '.inMatrix2', f=True) _logger.debug('Distance between base name is %s' % base_name) # length divide_half = pm.createNode('multiplyDivide', n=base_name + '_distceBetween_multiplyDivide') divide_half.input2X.set(0.5) dist_node.distance >> divide_half.input1X divide_half.outputX >> viz_shape[0].scaleX # radius viz_shape[0].scaleZ.set(radius) viz_shape[0].scaleY.set(radius) return [viz_shape[0], viz_direction[0]]
def shadow_primitive(): shadow_targets = pm.ls(sl=1) shadow_start = shadow_targets[0] shadow_start_location = pm.xform(shadow_start, q=True, ws=True, rotatePivot=True) if shadow_start.hasAttr('radius'): start_radius_attr = str(shadow_start) + '.radius' shadow_radius = shadow_start.radius.get() else: shadow_radius = 5 #shadow_sphere = pm.sphere(radius = shadow_start_radius) shadow_cylinder = pm.cylinder(radius=shadow_radius, heightRatio=2, axis=[1, 0, 0], sections=12, constructionHistory=1) if len(shadow_targets) > 1: shadow_end = shadow_targets[-1] shadow_end_location = pm.xform(shadow_end, q=True, ws=True, rotatePivot=True) shadow_height_ratio = get_distance(shadow_start_location, shadow_end_location) shadow_cylinder[0].rotatePivot.set([-0.5 * shadow_height_ratio, 0, 0]) shadow_cylinder[0].scalePivot.set([-0.5 * shadow_height_ratio, 0, 0]) shadow_cylinder[1].heightRatio.set(shadow_height_ratio) else: pass pm.matchTransform(shadow_cylinder, shadow_start, scale=False)
def advSpaceSwitch(): # COMPLEX SPACE SWITCHING r = pmc.sphere(n="rightHand")[0] l = pmc.cylinder(n="leftHand")[0] w = pmc.cone(n="weapon")[0] ws = pmc.spaceLocator(n="worldLoc") locs = [ws] grps = [] names = ["World"] names.extend([s.name() for s in (r, l, w)]) for s in (r, l, w): grps.append(pmc.group(s, n=s.name() + "_space")) l = pmc.spaceLocator(n=s.name() + "_ws") l.setParent(s) locs.append(l) spaces = ":".join([n for n in names if n != s.name()]) s.addAttr("space", at="enum", k=True, enumName=spaces) for g in grps: spaceMat = pmc.nt.WtAddMatrix() spaceXform = pmc.nt.DecomposeMatrix() spaceMat.o >> spaceXform.inputMatrix spaceXform.ot >> g.t targets = [l for l in locs if not l.hasParent(g)] for i, t in enumerate(targets): driver = g.getChildren()[0] # uc = pmc.nt.UnitConversion() # t.wp >> uc.input # direct connection from l >> g, # modulated by enum value test = pmc.nt.FloatLogic() driver.space >> test.floatA test.floatB.set(i) cond = pmc.nt.FloatCondition() test.outBool >> cond.condition cond.floatA.set(0) cond.floatB.set(10) # cond.outFloat >> uc.nodeState hold = pmc.nt.HoldMatrix() t.wm >> hold.i cond.outFloat >> hold.nodeState # for second-round lookup # ensure no ping-ponging cond.message >> driver.childConditions cond.message >> t.getParent().parentConditions hold.o >> spaceMat.i[i].m test.outBool >> spaceMat.i[i].w
def build(): defaults = { 'ax': (0, 1, 0), 'ssw': 0, 'esw': 360, 'd': 3, 'ut': 0, 'tol': 0.02, 'ch': False } ctrl = sphere(p=(0, 0.8333333, 0), r=0.166667, s=6, nsp=4, **defaults)[0] cyl = cylinder(p=(0, 0.345, 0), r=.08333333, hr=8.333333, s=4, nsp=1, **defaults)[0] cyl.rename('tube') points = [ # shaft [0, 0, 0], [0, 0.666667, 0], # circle [-0.11785101234662772, 0.71548248765337219, 0], [-0.166667, 0.833333, 0], [-0.11785101234662777, 0.95118451234662771, 0], [0, 1, 0], [0.1178510123466277, 0.95118451234662771, 0], [0.166667, 0.833333, 0], [0.11785101234662779, 0.71548248765337241, 0], [0, 0.666667, 0], # cross line [0.11785101234662779, 0.71548248765337241, 0], [-0.11785101234662777, 0.95118451234662771, 0], # transition [-0.166667, 0.833333, 0], # cross line [-0.11785101234662772, 0.71548248765337219, 0], [0.1178510123466277, 0.95118451234662771, 0], ] line = curve(p=points, d=1) line.rename('outline') line.getShape().setParent(ctrl, add=True, shape=True) cyl.getShape().setParent(ctrl, add=True, shape=True) delete(line, cyl) return ctrl
def _test_squash2(step_size=10): cmds.file(new=True, f=True) root = pymel.createNode('transform', name='root') pymel.addAttr(root, longName='amount', defaultValue=1.0, k=True) pymel.addAttr(root, longName='shape', defaultValue=math.e, k=True) pymel.addAttr(root, longName='offset', defaultValue=0.0, k=True) attAmount = root.attr('amount') attShape = root.attr('shape') attOffset = root.attr('offset') attInput = parse("amount^2", amount=attAmount) for i in range(0, 100, step_size): cyl, make = pymel.cylinder() cyl.rz.set(90) cyl.ty.set(i+step_size/2) make.heightRatio.set(step_size) attSquash = parse("amount^(1/(shape^((x+offset)^2)))", x=(i-50)/50.0, amount=attInput, shape=attShape, offset=attOffset) pymel.connectAttr(attSquash, cyl.sy) pymel.connectAttr(attSquash, cyl.sz) return True
def _test_squash2(step_size=10): cmds.file(new=True, f=True) root = pymel.createNode('transform', name='root') pymel.addAttr(root, longName='amount', defaultValue=1.0, k=True) pymel.addAttr(root, longName='shape', defaultValue=math.e, k=True) pymel.addAttr(root, longName='offset', defaultValue=0.0, k=True) attAmount = root.attr('amount') attShape = root.attr('shape') attOffset = root.attr('offset') attInput = parse("amount^2", amount=attAmount) for i in range(0, 100, step_size): cyl, make = pymel.cylinder() cyl.rz.set(90) cyl.ty.set(i + step_size / 2) make.heightRatio.set(step_size) attSquash = parse("amount^(1/(shape^((x+offset)^2)))", x=(i - 50) / 50.0, amount=attInput, shape=attShape, offset=attOffset) pymel.connectAttr(attSquash, cyl.sy) pymel.connectAttr(attSquash, cyl.sz) return True
def build(): ctrl = cylinder(ax=[0, 1, 0], ssw=0, esw=360, r=0.455, hr=0.455, d=3, ch=0)[0] major = util.CirclePoints.major minor = util.CirclePoints.minor body = util.CirclePoints.body terminal = util.CirclePoints.terminal s = [-minor, 0, -major] e = [minor, 0, -major] top = [terminal, s, s] + body + [e, e, terminal, terminal, terminal] bot = [terminal, terminal, terminal, terminal, s, s ] + body + [e, e, terminal, terminal, terminal] line = curve(p=top + bot) line.rename('outline') move(line.cv[:len(top)], [0, .125, 0], r=1) move(line.cv[len(top):], [0, -.125, 0], r=1) line.getShape().setParent(ctrl, add=True, shape=True) delete(line) return ctrl
shadow_targets = pm.ls(sl=1) shadow_start = shadow_targets[0] shadow_start_location = pm.xform(shadow_start, q=True, ws=True, rotatePivot=True) if shadow_start.hasAttr('radius'): start_radius_attr = str(shadow_start) + '.radius' shadow_radius = shadow_start.radius.get() else: shadow_radius = 5 #shadow_sphere = pm.sphere(radius = shadow_start_radius) shadow_cylinder = pm.cylinder(radius=shadow_radius, heightRatio=2, axis=[1, 0, 0], sections=12, constructionHistory=1) if len(shadow_targets) > 1: shadow_end = shadow_targets[-1] shadow_end_location = pm.xform(shadow_end, q=True, ws=True, rotatePivot=True) shadow_height_ratio = get_distance(shadow_start_location, shadow_end_location) print shadow_height_ratio shadow_cylinder[0].rotatePivot.set([-0.5 * shadow_height_ratio, 0, 0]) shadow_cylinder[0].scalePivot.set([-0.5 * shadow_height_ratio, 0, 0]) shadow_cylinder[1].heightRatio.set(shadow_height_ratio / shadow_radius)
def jnt_or_control_grp(name, object_type="joint", parent_node=None, have_loc=False, have_jnt=False): u"""骨骼组或控制器组 :param name: :param object_type: 可选项:joint,plane,tours,locator,sphere,cylinder :param parent_node: :param have_loc: :return: grp """ pm.select(cl=True) if object_type == "joint": pm.joint(name=name), if object_type == "plane": pm.nurbsPlane(name=name, p=[0, 0, 0], ax=[0, 0, 1], w=1, lr=0.6, d=1, u=1, v=1, ch=0), if object_type == "tours": pm.torus(name=name, p=[0, 0, 0], ax=[0, 0, 1], ssw=0, esw=360, msw=360, r=1, hr=0.5, d=3, ut=0, tol=0.01, s=8, nsp=4, ch=1), if object_type == "locator": pm.spaceLocator(name=name) if object_type == "sphere": pm.sphere(name=name, p=[0, 0, 0], ax=[0, 1, 0], ssw=0, esw=360, r=1, d=3, ut=0, tol=0.01, s=4, nsp=2, ch=1) if object_type == "cylinder": pm.cylinder(name=name, p=[0, 0, 0], ax=[0, 0, 1], ssw=0, esw=360, r=1, hr=2, d=3, ut=0, tol=0.01, s=8, nsp=1, ch=1) pm.parent(name, pm.createNode("transform", name="{}_Grp".format(name))) if have_loc: loc = pm.spaceLocator(name=name.replace('_Jnt', "_Loc")) pm.parent(loc, name) if have_jnt: pm.select(cl=True) pm.joint(name="{}_Jnt".format(name)) pm.parent("{}_Jnt".format(name), name) if parent_node is not None: pm.parent("{}_Grp".format(name), parent_node) pm.PyNode("{}_Grp".format(name)).translate.set([0, 0, 0]) pm.PyNode("{}_Grp".format(name)).rotate.set([0, 0, 0]) return pm.PyNode("{}_Grp".format(name))
def createAxis(self): pm.select(cl=1) xyzTopGrp = pm.group(name=self.axName) rgbShaders = self.createRGBShaders() xCylinder = pm.cylinder(hr=12, r=0.1, p=[0, 0, 0], ax=[1, 0, 0], name=self.axName + 'X')[0] xCylinder.translateX.set(0.6) xCylinder.setPivots([0, 0, 0], worldSpace=1) pm.makeIdentity(t=1, a=1) pm.delete(ch=1) pm.hyperShade(a=rgbShaders[0]) yCylinder = pm.cylinder(hr=12, r=0.1, p=[0, 0, 0], ax=[0, 1, 0], name=self.axName + 'Y')[0] yCylinder.translateY.set(0.6) yCylinder.setPivots([0, 0, 0], worldSpace=1) pm.makeIdentity(t=1, a=1) pm.delete(ch=1) pm.hyperShade(a=rgbShaders[1]) zCylinder = pm.cylinder(hr=12, r=0.1, p=[0, 0, 0], ax=[0, 0, 1], name=self.axName + 'Z')[0] zCylinder.translateZ.set(0.6) zCylinder.setPivots([0, 0, 0], worldSpace=1) pm.makeIdentity(t=1, a=1) pm.delete(ch=1) pm.hyperShade(a=rgbShaders[2]) xArrow = pm.cone(hr=2, r=0.2, p=[0, 0, 0], ax=[1, 0, 0], name=self.axName + 'arrX')[0] xArrow.translateX.set(1.4) xArrow.setPivots([0, 0, 0], worldSpace=1) pm.makeIdentity(t=1, a=1) pm.delete(ch=1) pm.hyperShade(a=rgbShaders[0]) yArrow = pm.cone(hr=2, r=0.2, p=[0, 0, 0], ax=[0, 1, 0], name=self.axName + 'arrY')[0] yArrow.translateY.set(1.4) yArrow.setPivots([0, 0, 0], worldSpace=1) pm.makeIdentity(t=1, a=1) pm.delete(ch=1) pm.hyperShade(a=rgbShaders[1]) zArrow = pm.cone(hr=2, r=0.2, p=[0, 0, 0], ax=[0, 0, 1], name=self.axName + 'arrZ')[0] zArrow.translateZ.set(1.4) zArrow.setPivots([0, 0, 0], worldSpace=1) pm.makeIdentity(t=1, a=1) pm.delete(ch=1) pm.hyperShade(a=rgbShaders[2]) pm.parent([ xCylinder.getShape(), yCylinder.getShape(), zCylinder.getShape(), xArrow.getShape(), yArrow.getShape(), zArrow.getShape() ], xyzTopGrp, r=1, s=1) guidePos = self.axGuide.getTranslation(worldSpace=1) jntRadius = self.axGuide.radius.get() scaleFactor = [jntRadius * 1.2, jntRadius * 1.2, jntRadius * 1.2] xyzTopGrp.translate.set(guidePos) xyzTopGrp.scale.set(scaleFactor) pm.makeIdentity(xyzTopGrp, t=1, s=1, r=1, a=1) pm.delete([xCylinder, yCylinder, zCylinder, xArrow, yArrow, zArrow]) plug = pm.listConnections(xyzTopGrp.getShapes()[0].name() + '.instObjGroups[0]', plugs=1) if plug: xyzTopGrp.getShapes()[0].instObjGroups[0] // plug[0] xyzTopGrp.getShapes()[0].instObjGroups[0] >> plug[0] plug = pm.listConnections(xyzTopGrp.getShapes()[3].name() + '.instObjGroups[0]', plugs=1) if plug: xyzTopGrp.getShapes()[3].instObjGroups[0] // plug[0] xyzTopGrp.getShapes()[3].instObjGroups[0] >> plug[0] plug = pm.listConnections(xyzTopGrp.getShapes()[1].name() + '.instObjGroups[0]', plugs=1) if plug: xyzTopGrp.getShapes()[1].instObjGroups[0] // plug[0] xyzTopGrp.getShapes()[1].instObjGroups[0] >> plug[0] plug = pm.listConnections(xyzTopGrp.getShapes()[4].name() + '.instObjGroups[0]', plugs=1) if plug: xyzTopGrp.getShapes()[4].instObjGroups[0] // plug[0] xyzTopGrp.getShapes()[4].instObjGroups[0] >> plug[0] plug = pm.listConnections(xyzTopGrp.getShapes()[2].name() + '.instObjGroups[0]', plugs=1) if plug: xyzTopGrp.getShapes()[2].instObjGroups[0] // plug[0] xyzTopGrp.getShapes()[2].instObjGroups[0] >> plug[0] plug = pm.listConnections(xyzTopGrp.getShapes()[5].name() + '.instObjGroups[0]', plugs=1) if plug: xyzTopGrp.getShapes()[5].instObjGroups[0] // plug[0] xyzTopGrp.getShapes()[5].instObjGroups[0] >> plug[0]