Python maya.OpenMaya.MFnSingleIndexedComponent() Examples

The following are 11 code examples of maya.OpenMaya.MFnSingleIndexedComponent(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module maya.OpenMaya , or try the search function .
Example #1
Source File: utils.py    From maya-retarget-blendshape with GNU General Public License v3.0 6 votes vote down vote up
def asComponent(index, t=OpenMaya.MFn.kMeshVertComponent):
    """
    index -> OpenMaya.MFn.kComponent
    Based on the input type it will create a component type for this tool
    the following components are being used.
    
    * OpenMaya.MFn.kMeshVertComponent
    * OpenMaya.MFn.kMeshEdgeComponent
    
    :param int/OpenMaya.MIntArray index: indices to create component for
    :param OpenMaya.MFn.kComponent t: can be all of OpenMaya component types.
    :return: Initialized components
    :rtype: OpenMaya.MFnSingleIndexedComponent
    """
    # convert input to an MIntArray if it not already is one
    array = asMIntArray(index)

    # initialize component
    component = OpenMaya.MFnSingleIndexedComponent().create(t)
    OpenMaya.MFnSingleIndexedComponent(component).addElements(array)
    return component


# ---------------------------------------------------------------------------- 
Example #2
Source File: conversion.py    From maya-skinning-tools with GNU General Public License v3.0 6 votes vote down vote up
def asComponent(index):
    """
    index -> OpenMaya.MFn.kMeshVertComponent

    :param int/OpenMaya.MIntArray index: indices to create component for
    :return: Initialized component(s)
    :rtype: OpenMaya.MFn.kMeshVertComponent
    """
    # convert input to an MIntArray if it not already is one
    indices = asMIntArray(index)

    # initialize component(s)
    t = OpenMaya.MFn.kMeshVertComponent
    component = OpenMaya.MFnSingleIndexedComponent().create(t)
    OpenMaya.MFnSingleIndexedComponent(component).addElements(indices)

    return component 
Example #3
Source File: skinio.py    From cmt with MIT License 6 votes vote down vote up
def set_data(self, data, selected_components=None):
        """Sets the data and stores it in the Maya skinCluster node.

        :param data: Data dictionary.
        """

        self.data = data
        dag_path, components = self.__get_geometry_components()
        if selected_components:
            fncomp = OpenMaya.MFnSingleIndexedComponent()
            components = fncomp.create(OpenMaya.MFn.kMeshVertComponent)
            for i in selected_components:
                fncomp.addElement(i)
        self.set_influence_weights(dag_path, components)
        self.set_blend_weights(dag_path, components)

        for attr in SkinCluster.attributes:
            cmds.setAttr("{0}.{1}".format(self.node, attr), self.data[attr]) 
Example #4
Source File: utils.py    From maya-retarget-blendshape with GNU General Public License v3.0 5 votes vote down vote up
def getAveragePosition(dag, component, space):
    """
    Get average position of connected vertices.
    
    :param OpenMaya.MDagPath dag: 
    :param OpenMaya.MFnSingleIndexedComponent component: 
    :param OpenMaya.MSpace space:
    :return: Average length of the connected edges
    :rtype: OpenMaya.MPoint
    """
    averagePos = OpenMaya.MPoint()

    # get connected vertices
    connected = OpenMaya.MIntArray()
    
    iterate = OpenMaya.MItMeshVertex(dag, component)
    iterate.getConnectedVertices(connected)
    
    # get original position
    originalPos = iterate.position(space)
      
    # ignore if no vertices are connected
    if not connected.length():
        return averagePos
    
    # get average 
    component = asComponent(connected)
    iterate = OpenMaya.MItMeshVertex(dag, component)
    while not iterate.isDone():
        averagePos += OpenMaya.MVector(iterate.position(space))
        iterate.next()
        
    averagePos = averagePos/connected.length()
    return originalPos, averagePos 
Example #5
Source File: utils.py    From maya-retarget-blendshape with GNU General Public License v3.0 5 votes vote down vote up
def getAverageLength(dag, component, space):
    """
    Get average length of connected edges.
    
    :param OpenMaya.MDagPath dag: 
    :param OpenMaya.MFnSingleIndexedComponent component: 
    :param OpenMaya.MSpace space:
    :return: Average length of the connected edges
    :rtype: float
    """
    total = 0
    
    lengthUtil = OpenMaya.MScriptUtil()
    lengthPtr = lengthUtil.asDoublePtr()

    # get connected edges
    connected = OpenMaya.MIntArray()

    iterate = OpenMaya.MItMeshVertex(dag, component)
    iterate.getConnectedEdges(connected)
    
    # ignore if no edges are connected
    if not connected.length():
        return 0
    
    # get average
    component = asComponent(connected, OpenMaya.MFn.kMeshEdgeComponent)
    iterate = OpenMaya.MItMeshEdge(dag, component)
    while not iterate.isDone():
        iterate.getLength(lengthPtr, space)
        total += lengthUtil.getDouble(lengthPtr)
        
        iterate.next()
        
    return total/connected.length()


# ---------------------------------------------------------------------------- 
Example #6
Source File: paintRemoveInfluenceCtxCommands.py    From maya-skinning-tools with GNU General Public License v3.0 5 votes vote down vote up
def getSelection(self):
        # variable
        indices = OpenMaya.MIntArray()
        allIndices = OpenMaya.MIntArray()
        
        # get active selection
        selection = OpenMaya.MSelectionList()  
        OpenMaya.MGlobal.getActiveSelectionList(selection)

        # loop selection
        iter = OpenMaya.MItSelectionList(selection)
        while not iter.isDone():
            # variables
            component = OpenMaya.MObject()
            dag = OpenMaya.MDagPath()

            iter.getDagPath(dag, component)
            
            if not component.isNull(): 
                objIndices = OpenMaya.MIntArray()

                components = OpenMaya.MFnSingleIndexedComponent(component) 
                components.getElements(indices)
                
                for i in range(indices.length()):
                    allIndices.append(indices[i])
                
            iter.next()
            
        return allIndices 
Example #7
Source File: skinio.py    From cmt with MIT License 5 votes vote down vote up
def set_influence_weights(self, dag_path, components):
        """Sets all the influence weights.

        :param dag_path: MDagPath of the deformed geometry.
        :param components: Component MObject of the deformed components.
        """
        influence_paths = OpenMaya.MDagPathArray()
        influence_count = self.fn.influenceObjects(influence_paths)

        elements = OpenMaya.MIntArray()
        fncomp = OpenMaya.MFnSingleIndexedComponent(components)
        fncomp.getElements(elements)
        weights = OpenMaya.MDoubleArray(elements.length() * influence_count)

        components_per_influence = elements.length()

        for imported_influence, imported_weights in self.data["weights"].items():
            imported_influence = imported_influence.split("|")[-1]
            for ii in range(influence_paths.length()):
                influence_name = influence_paths[ii].partialPathName()
                influence_without_namespace = shortcuts.remove_namespace_from_name(
                    influence_name
                )
                if influence_without_namespace == imported_influence:
                    # Store the imported weights into the MDoubleArray
                    for jj in range(components_per_influence):
                        weights.set(imported_weights[elements[jj]], jj * influence_count + ii)
                    break

        influence_indices = OpenMaya.MIntArray(influence_count)
        for ii in range(influence_count):
            influence_indices.set(ii, ii)
        self.fn.setWeights(dag_path, components, influence_indices, weights, False) 
Example #8
Source File: skinio.py    From cmt with MIT License 5 votes vote down vote up
def set_blend_weights(self, dag_path, components):
        """Set the blendWeights.

        :param dag_path: MDagPath of the deformed geometry.
        :param components: Component MObject of the deformed components.
        """
        elements = OpenMaya.MIntArray()
        fncomp = OpenMaya.MFnSingleIndexedComponent(components)
        fncomp.getElements(elements)
        blend_weights = OpenMaya.MDoubleArray(elements.length())
        for i in range(elements.length()):
            blend_weights.set(self.data["blendWeights"][elements[i]], i)
        self.fn.setBlendWeights(dag_path, components, blend_weights) 
Example #9
Source File: ml_softWeights.py    From ml_tools with MIT License 5 votes vote down vote up
def getSoftSelectionWeights():

    #get selection
    sel = om.MSelectionList()
    softSelection = om.MRichSelection()
    om.MGlobal.getRichSelection(softSelection)
    softSelection.getSelection(sel)

    dagPath = om.MDagPath()
    component = om.MObject()

    iter = om.MItSelectionList(sel, om.MFn.kMeshVertComponent)
    weights = {}

    while not iter.isDone():

        iter.getDagPath( dagPath, component )
        dagPath.pop() #Grab the parent of the shape node
        node = dagPath.fullPathName()
        fnComp = om.MFnSingleIndexedComponent(component)

        for i in range(fnComp.elementCount()):
            weight = 1.0
            if fnComp.hasWeights():
                weight = fnComp.weight(i).influence()

            weights['{}.vtx[{}]'.format(node, fnComp.element(i))] = weight

        iter.next()

    return weights 
Example #10
Source File: bake_skin_weight.py    From SIWeightEditor with MIT License 5 votes vote down vote up
def redoIt(self, flash=True):
        for node, vtxIndices in self.bake_node_id_dict.items():
            weights = self.bake_node_weight_dict[node]
            infIndices = self.bake_node_inf_dict[node]
            skinFn = self.node_skinFn_dict[node]
            
            if MAYA_VER >= 2016:
                sList = om2.MSelectionList()
                sList.add(node)
                meshDag, component = sList.getComponent(0)
                # 指定の頂点をコンポーネントとして取得する
                singleIdComp = om2.MFnSingleIndexedComponent()
                vertexComp = singleIdComp.create(om2.MFn.kMeshVertComponent )
                singleIdComp.addElements(vtxIndices)
            else:
                sList = om.MSelectionList()
                sList.add(node)
                meshDag = om.MDagPath()
                component = om.MObject()
                sList.getDagPath(0, meshDag, component)
                singleIdComp = om.MFnSingleIndexedComponent()
                vertexComp = singleIdComp.create(om.MFn.kMeshVertComponent )
                singleIdComp.addElements(vtxIndices)
                            
            ##引数(dag_path, MIntArray, MIntArray, MDoubleArray, Normalize, old_weight_undo)
            #print meshDag, vertexComp , infIndices , weights
            #print type(infIndices)
            #print type(vertexComp)
            skinFn.setWeights(meshDag, vertexComp , infIndices , weights, False)
        #アンドゥ用ウェイトデータをアップデートする
        siweighteditor.update_dict(self.redo_node_weight_dict, self.bake_node_id_dict)
        if flash:
            if self.ignore_undo:#スライダー制御中のアンドゥ履歴は全無視する
                return
            siweighteditor.refresh_window() 
Example #11
Source File: bake_skin_weight.py    From SIWeightEditor with MIT License 5 votes vote down vote up
def undoIt(self):
        siweighteditor.reverse_dict(self.undo_node_weight_dict, self.bake_node_id_dict)
        if self.ignore_undo:#スライダー制御中のアンドゥ履歴は全無視する
            return
        for node, vtxIndices in self.bake_node_id_dict.items():
            weights = self.org_node_weight_dict[node]
            infIndices = self.bake_node_inf_dict[node]
            skinFn = self.node_skinFn_dict[node]
            
            if MAYA_VER >= 2016:
                sList = om2.MSelectionList()
                sList.add(node)
                meshDag, component = sList.getComponent(0)
                singleIdComp = om2.MFnSingleIndexedComponent()
                vertexComp = singleIdComp.create(om2.MFn.kMeshVertComponent )
                singleIdComp.addElements(vtxIndices)
            else:
                sList = om.MSelectionList()
                sList.add(node)
                meshDag = om.MDagPath()
                component = om.MObject()
                sList.getDagPath(0, meshDag, component)
                singleIdComp = om.MFnSingleIndexedComponent()
                vertexComp = singleIdComp.create(om.MFn.kMeshVertComponent )
                singleIdComp.addElements(vtxIndices)
                        
            ##引数(dag_path, MIntArray, MIntArray, MDoubleArray, Normalize, old_weight_undo)
            skinFn.setWeights(meshDag, vertexComp , infIndices , weights, False)
        #アンドゥの度に読み込むと重いからどうしよう。
        siweighteditor.refresh_window()