Beispiel #1
0
def addSurfaceConstraintToBnd(bnd, surface):
    '''
    '''
    secDrv = bnd.getParent()
    matrix = secDrv.getMatrix(worldSpace=True)
    
    # create additional nulls
    gc = pm.group(em=True, n=bnd.nodeName().replace('_bnd', '_gc'))
    gc_offset = pm.group(em=True, n=bnd.nodeName().replace('_bnd', '_gc_offset'))
    acs = pm.group(em=True, n=bnd.nodeName().replace('_bnd', '_acs'))
    
    # point constraint first to get precise position to secDrv
    pm.pointConstraint(secDrv, gc)
    # geometry constraint to surface
    pm.geometryConstraint(surface, gc)
    # normal constraint, using secDrv-Y as up
    pm.normalConstraint(surface, gc, aim=(0,0,1), u=(0,1,0),
                        wuo=secDrv, wut='objectrotation', wu=(0,1,0))
    
    # ensure that gc_offset stores offset to secDrv
    gc_offset.setMatrix(matrix, worldSpace=True)
    acs.setMatrix(matrix, worldSpace=True)
    
    # hierarchy
    secDrv | gc | gc_offset | acs | bnd
Beispiel #2
0
    def disperse(self, *args):
        """Called when the disperse button is pressed"""
        if self.sourceObj == None or self.targetObjs == None:
            pm.confirmDialog(t='Error', b=['OK'],
                m='Please make sure source and targets are selected.')
            return

        # get copy number
        copyNum = self.copyNum.getValue()
        copyNum = min(copyNum, len(self.vertIndexList))

        # get rotation
        rotationMode = self.rotationModeRC.getSelect()
        rotationMode = pm.control(rotationMode, q=True, fpn=True)
        if rotationMode == self.rotBtnRand:
            origRot = pm.xform(self.sourceObj, ro=True, q=True)
            rotRange = self.rotationRange.getValue()

        # get scale
        scaleMode = self.scaleModeRC.getSelect()
        scaleMode = pm.control(scaleMode, q=True, fpn=True)
        if scaleMode == self.scaleBtnRand:
            scaleRange = self.scaleRange.getValue()

        # make copies
        randVertIndexList = random.sample(self.vertIndexList, copyNum)
        for i in randVertIndexList:
            newObj = pm.duplicate(self.sourceObj, n='%s_copy'%self.sourceObj)
            # decide which target the random vert index falls on
            vertSum = 0
            targetIndex = 0
            targetVertIndex = 0
            for j, k in enumerate(self.targetVertNumList):
                vertSum += k
                if i + 1 <= vertSum:
                    targetIndex = j
                    targetVertIndex = i - (vertSum - k)
                    break
            # apply scale
            if scaleMode == self.scaleBtnRand:
                randScale = random.uniform(scaleRange[0], scaleRange[1])
                pm.xform(newObj, s=(randScale,randScale,randScale))
            # apply rotation
            if rotationMode == self.rotBtnAlign: # normal constraint
                pm.normalConstraint(self.targetObjs[targetIndex], newObj, aim=(0,0,1), u=(0,1,0))
            elif rotationMode == self.rotBtnRand:
                newRotX = random.uniform(origRot[0]-rotRange[0]/2,origRot[0]+rotRange[0]/2)
                newRotY = random.uniform(origRot[1]-rotRange[1]/2,origRot[1]+rotRange[1]/2)
                newRotZ = random.uniform(origRot[2]-rotRange[2]/2,origRot[2]+rotRange[2]/2)
                pm.xform(newObj, ro=(newRotX,newRotY,newRotZ))
            rotatePivot = pm.xform(newObj, rp=True, q=True)
            newPos = pm.pointPosition('%s.vtx[%d]'%(self.targetObjs[targetIndex],targetVertIndex))
            posOffset = [newPos[0]-rotatePivot[0], newPos[1]-rotatePivot[1], newPos[2]-rotatePivot[2]]
            pm.xform(newObj, t=posOffset)
            # remove constraint after translation
            if rotationMode == self.rotBtnAlign:
                pm.delete(newObj, cn=True)
Beispiel #3
0
def placeOnObject(src, dest, orient=False, keep=False):
    """ Moves & Orients Object in XYZ to Closest Point in dest """
    geo_const = pm.geometryConstraint(dest, src)
    if not keep:
        pm.delete(geo_const)
    if orient:
        nrm_const = pm.normalConstraint(dest, src, wu=(0, 1, 0), wut="vector", u=(0, 1, 0), aim=(0, 1, 0))
        if not keep:
            pm.delete(nrm_const)
Beispiel #4
0
def onVerts(target=None, normals=True, type="vector"):
    #1. make an array of all selected objects
    if not target:
        target = mc.ls(sl=1)

    #2. get the first selected object so it can be copied...
    foo = target[0]

    #3. then get the vertices of each other object and copy the first one there.
    for i in range(1,len(target)):

        v = getVertPos(target[i])
            
        for j in range(0,len(v)):
            s(foo)
            foo2 = py.duplicate()
            mc.move(v[j][0],v[j][1],v[j][2])

            if(normals==True):
                py.mel.eval("select -r " + target[i] + ".vtx[" + str(j) +"] " + foo2[0] + ";")
                py.normalConstraint(worldUpType=type)

            '''
getSel  = pm.ls(os=1)
vtxGrp = pm.ls(pm.polyListComponentConversion(getSel,fe=1,tv=1),fl=1)
vtxNum = len(vtxGrp)

vtxPosGrp = [pm.xform(vtx, q=True, ws=True, t=True)for vtx in vtxGrp]

tmpPoly = mc.polyCreateFacet(p=[tuple(vec) for vec in vtxPosGrp ])[0]
pm.xform(tmpPoly,cp=1)
centerPos = mc.xform(tmpPoly, q=True, ws=True, rp=True)
distGrp = [((vec[0] - centerPos[0]) ** 2 + (vec[1] - centerPos[1]) ** 2 + (vec[2] - centerPos[2]) ** 2) ** 0.5 for vec in vtxPosGrp]
dist = sum(distGrp)/vtxNum

circle = pm.circle(s=vtxNum, r=dist, c=(0, 0, 0), d=3, ch=False)[0]
pm.xform(circle, ws=True, t=tuple(centerPos))

wu = (vtxPosGrp[0][0] - centerPos[0]),(vtxPosGrp[0][1] - centerPos[1]),(vtxPosGrp[0][2] - centerPos[2])

pm.normalConstraint(tmpPoly,circle,aim=[0,0,1],u=[0,1,0],wu=(0,1,0))

e2vInfos= pm.polyInfo(getSel,ev=1)
e2vDict = {}
for info in e2vInfos:
    evList = [ int(i) for i in re.findall('\\d+', info) ]
    e2vDict.update(dict([(evList[0], evList[1:])]))


# average normal
vtxGrp = pm.mel.eval("PolySelectConvert 3;")
avNmGrp = pm.polyNormalPerVertex(getSel,q=1,xyz=1)
Beispiel #6
0
def create_handle(name=Vars.PLUGIN_NAME, camera="", light="", geometry=""):
    """
    Handle creation master method. Params are accepted, no params
    means creating from selection.
    :param name : string, Optional. Handle name.
    :param camera : string, Optional. Camera name.
    :param light : string, Optional. Light name.
    :param geometry : string, Optional. Geometry to constraint to.
    :return Parameter dictionary {handle, node, camera, light, geo}
    """

    # Get from selection ?
    if not any((camera, light, geometry)):
        camera, light, geometry = get_data_from_selection()

    # Store snap position before creating everything otherwise
    component_pos = get_components_pos()

    # Snap to component, or to cam if there's geo, otherwise at root
    if component_pos != (0, 0, 0):
        snap_pos = component_pos
    elif camera and geometry:
        snap_pos = pmc.PyNode(camera).getTranslation(ws=True)
    else:
        snap_pos = (0, 0, 0)

    # Create Sol Handle
    s_node = pmc.createNode(Vars.PLUGIN_NAME, n='{}Shape#'.format(name))

    # Connect root world matrix and set position on component
    s_trans = s_node.getParent()
    s_trans.worldMatrix.connect(s_node.selfMatrix)
    s_trans.setTranslation(snap_pos, ws=True)

    # Add Parameters
    s_trans.addAttr("_", at='enum', en='Controls', k=1)
    s_trans.addAttr('normalReflectionBlend', at='double', min=0, max=1, dv=1, k=1)
    s_trans.addAttr('distance', at='double', dv=10, k=1)
    s_trans.addAttr('lightRoll', at='double', dv=0, k=1)
    s_trans.addAttr('drawMode', at='long', min=0, max=3, dv=2, k=1)
    s_trans.addAttr('normalLength', at='double', min=0, dv=3, k=1)

    # Set and connect parameters
    pmc.setAttr(s_trans.name() + '._', lock=1)
    s_trans.normalReflectionBlend.connect(s_node.normalReflectionBlend, f=1)
    s_trans.distance.connect(s_node.distance, f=1)
    s_trans.lightRoll.connect(s_node.lightRoll, f=1)
    s_trans.drawMode.connect(s_node.drawMode, f=1)
    s_trans.normalLength.connect(s_node.normalLength, f=1)

    # Connect Camera and light
    c_node = change_handle_camera(s_node, camera)
    # TODO : disconnected, here, might be useful ?
    _, l_node, disconnected = change_handle_light(s_node, light)

    # TODO : if l_node, create parameters from typelight settings...

    # Constrain to geometry
    if geometry:
        g_node = pmc.PyNode(geometry)
        pmc.geometryConstraint(g_node, s_trans)
        pmc.normalConstraint(g_node, s_trans, aimVector=(0, 1, 0))
    else:
        g_node = None

    # Select node and return data
    pmc.select(s_trans, r=True)
    return Vars.HANDLE_DATA(
        handle=s_trans,
        node=s_node,
        camera=c_node,
        light=l_node,
        geometry=g_node
    )