def create_point_on_mesh(geo, position, sticky_target, free_rotation=True): """ Create point on mesh setup @param position: @param geo: @parem sticky: @return: """ pom = pm.createNode("closestPointOnMesh") pom.inPosition.set(position) geo.worldMatrix[0] >> pom.inputMatrix geo.worldMesh[0] >> pom.inMesh pom.position >> sticky_target.translate index = pom.closestVertexIndex.get() locator = pm.spaceLocator() libUtilities.snap(locator, geo.vtx[index], rotate=False) libUtilities.freeze_transform(locator) pm.pointOnPolyConstraint(geo.vtx[index], locator, maintainOffset=True) pm.delete(pom) constraint = pm.listRelatives(locator, type="constraint")[0] if free_rotation: for attr in ["rx", "rz", "ry"]: libUtilities.break_connection(locator, attr) locator.attr(attr).set(0) return {"constraint": constraint, "locator": locator}
def pointToLocator(): SelectVertex = pm.ls(selection=True) if not SelectVertex: pm.inViewMessage(amg='<hl>Select One Vertex </hl>.', pos='midCenter', fade=True) else: baseName = SelectVertex[0].name().split(".")[0] base = pm.PyNode(baseName) trans = base.getParent() toParent = trans.getAllParents()[-1] Locator = pm.spaceLocator(name=toParent + '_' + 'Light' + '_' + 'Locator') Locator.useOutlinerColor.set(1) Locator.outlinerColor.set(1, 1, .5) Locator.overrideEnabled.set(1) Locator.overrideVisibility.set(1) Locator.overrideColor.set(17) pm.parent(Locator, toParent) ##Select Vertex and Locator pm.select(SelectVertex, Locator, r=True) ##Apply Constraimt Pointconstraint = pm.pointOnPolyConstraint(SelectVertex, Locator, maintainOffset=False) pm.inViewMessage(amg='<hl>ConstraintDone </hl>.', pos='midCenter', fade=True)
def snap_nearest(ob, mesh_node,constraint=False): if constraint: closest_uv = get_closest_component(ob, mesh_node) constraint = pm.pointOnPolyConstraint( mesh_node, ob, mo=False) stripname = mesh_node.split('|')[-1] constraint.attr(stripname + 'U0').set(closest_uv[0]) constraint.attr(stripname + 'V0').set(closest_uv[1]) return constraint closest_component_pos = get_closest_component(ob, mesh_node, uv=False, pos=True) ob.setTranslation(closest_component_pos,'world')
def bdLocOnVertex(): ''' Category: General ''' selection = pm.ls(sl=1, fl=1) if selection: if isinstance(selection[0], pm.MeshVertex): locators = [] for i in range(len(selection)): vtx = selection[i] loc = pm.spaceLocator() pointCnstr = pm.pointOnPolyConstraint(vtx, loc)
def attach(source, target, p): """Instances source, and attaches it to mesh target as close as it can on the surface to point p. """ n = _getMeshNode(target) inst = pm.instance(source) constraint = pm.pointOnPolyConstraint(target, inst) uvs = n.getUVAtPoint(p, space="world") transform = n.firstParent() constraint.attr("%sU0" % transform.nodeName()).set(uvs[0]) constraint.attr("%sV0" % transform.nodeName()).set(uvs[1])
def locOnVertex(): selection = pm.ls(sl=1, fl=1) if selection: if isinstance(selection[0], pm.MeshVertex): locators = [] for i in range(len(selection)): vtx = selection[i] loc = pm.spaceLocator() pointCnstr = pm.pointOnPolyConstraint(vtx, loc) uv = vtx.getUV() attrs = pm.listAttr(pointCnstr, ud=1) pointCnstr.attr(attrs[1]).set(uv[0]) pointCnstr.attr(attrs[2]).set(uv[1]) locators.append(loc) if len(selection) > 1: drvLoc = pm.spaceLocator(name='drvLoc') pm.parentConstraint(locators, drvLoc, mo=0)
def set_constraint(self, target=None): if all([self.guide_mesh, self.guide_mesh, self.root]): pm.delete(self.constraint) if target is None: target = self.root closest_uv = ul.get_closest_component(target, self.guide_mesh) self.constraint = pm.pointOnPolyConstraint( self.guide_mesh, self.root, mo=False, name=self.constraint_name) stripname = self.guide_mesh.getParent().name().split('|')[-1] self.constraint.attr(stripname + 'U0').set(closest_uv[0]) self.constraint.attr(stripname + 'V0').set(closest_uv[1]) # pm.select(closest_info['Closest Vertex']) else: pm.error('Please set guide mesh')
def pointOnPol(*args): pointList = pm.ls(selection = True) for p in pointList: loc = pm.spaceLocator() pm.pointOnPolyConstraint(p, loc, mo = False)
def _deformPlanes(self, autoGrp, baseGrp=None): """ Create a plane and copy the deforms from base mesh, then constraint the autoGrp and baseGrp to the plane :param autoGrp:autoGrp, generally contains a controller as child :param baseGrp: to control a baseShape like wire deformer baseCure :return: planes """ # create a small plane per point, then combine them planes = [] for ctr in autoGrp: plane = pm.polyPlane(h=0.01, w=0.01, sh=1, sw=1, ch=False)[0] plane.setTranslation(ctr.getTranslation("world"), "world") planes.append(plane) # combine planes if len(planes) > 1: # len 1 gives an error with polyUnite planes = pm.polyUnite(planes, ch=False, mergeUVSets=True)[0] else: planes = planes[0] pm.makeIdentity(planes, a=True, r=True, s=True, t=True) planes.setPivots([0, 0, 0]) planes.rename("%s_planes" % self._baseName) # rename pm.polyAutoProjection(planes, ch=False, lm=0, pb=0, ibd=1, cm=0, l=2, sc=1, o=1, p=6, ps=0.2, ws=0) # uvs if self._MESH_SHAPE: # if skin sample, copy skin weights to planes # find skin node skinNode = pm.listHistory(self._MESH_SHAPE, type='skinCluster')[0] # joint list jointList = skinNode.influenceObjects() # create skinCluster copySkinCluster = pm.skinCluster(planes, jointList, mi=3) # copy skin weigths pm.copySkinWeights(ss=skinNode, ds=copySkinCluster, noMirror=True, surfaceAssociation='closestPoint', influenceAssociation=('closestJoint', 'closestJoint')) # connect each auto grp to each poly face numFaces = planes.getShape().numFaces() logger.debug("num Faces: %s" % numFaces) for i in range(numFaces): pm.select(planes.f[i], r=True) pm.select(autoGrp[i], add=True) pm.pointOnPolyConstraint(maintainOffset=True) pm.select(cl=True) if baseGrp: pm.select(planes.f[i], r=True) pm.select(baseGrp[i], add=True) pm.pointOnPolyConstraint(maintainOffset=True) pm.select(cl=True) #hammer weights try: #TODO: bad aproximation vertex = pm.modeling.polyListComponentConversion(planes.f[i], tv=True) pm.select(vertex, r=True) logger.debug("vertices %s:" % vertex) mel.eval("weightHammerVerts;") pm.select(cl=True) except: pm.select(cl=True) # parent planes to nonXform grp self._noXformGrp.addChild(planes) # parent to noXform return planes