예제 #1
0
def vertexColorFloodMaya(colorSet, value, channels=[False, False, False, False], replace=True):
    """
    Assigns vertex colors to a specific colorSet of all selected objects
    Args:
        colorSet (str): Which color set to flood
        value (float): What value to flood the color with
        channels (bool lst): RGBA list of color channels to flood
        replace (bool): If the value needs to be replaced
    """
    logger.debug("-> Flooding {0} at channels {1} with value {2}".format(colorSet, channels, value))
    channelChars = ['r', 'g', 'b', 'a']
    # find selected shapes
    selected = cmds.ls(sl=True)
    shapes = lib.getShapes(selected)
    enableVtxCtrl(shapes)
    # set the current color set
    try:
        cmds.polyColorSet(shapes, currentColorSet=True, cs=colorSet)  # sets the current color set of all shapes
    except RuntimeError:
        cmds.error("One or more of the objects has not been prepped")
    # find channel to flood
    for channel in channels:
        if channel:
            channelChar = channelChars[channels.index(channel)]
            if replace:
                logger.debug("Resetting control parameters in: {0} with {1}".format(shapes, value))
                eval("cmds.polyColorPerVertex({0}={1})".format(channelChar, value))
            else:
                logger.debug("Flooding control parameters in: {0} with {1}".format(shapes, value))
                eval("cmds.polyColorPerVertex({0}={1}, rel=True)".format(channelChar, value))
예제 #2
0
def createMaterial(objs,
                   name="mnprMat_SFX",
                   prototype="shaderFX",
                   graph="mnpr_uber"):
    """
    Create and assign material to all objs
    Args:
        name (str): Name of new material
        objs (list): List of objects to assign new material into
        prototype (str): "shaderFX" or "prototypeC"
    Returns: Material name (str)
    """
    logger.debug("Creating new material for {0}".format(objs))

    # get shader file
    shaderFile = os.path.join(mnpr_info.environment, "shaders",
                              "{0}.sfx".format(graph))
    if prototype != "shaderFX":
        if os.name == 'nt' and mnpr_info.backend == 'dx11':
            shaderFile = os.path.join(mnpr_info.environment, "shaders",
                                      "{0}.fx".format(prototype))
            if not os.path.isfile(shaderFile):
                shaderFile = os.path.join(mnpr_info.environment, "shaders",
                                          "{0}.fxo".format(prototype))
        else:
            shaderFile = os.path.join(mnpr_info.environment, "shaders",
                                      "{0}.ogsfx".format(prototype))

    # check if objects are meshes
    shapes = lib.getShapes(objs)
    if not len(shapes):
        cmds.error(
            "{0} -> All selected objects need to be meshes".format(objs))

    # generate name of material
    newName = "{0}_{1}".format(name, mnpr_info.abbr)

    shader = ""
    if prototype == "shaderFX":
        shader = cmds.shadingNode('ShaderfxShader', asShader=True, name=name)
        cmds.shaderfx(sfxnode=shader, loadGraph=shaderFile)
    else:
        if os.name == 'nt' and mnpr_info.backend == 'dx11':
            shader = cmds.shadingNode('dx11Shader', asShader=True, n=newName)
        else:
            shader = cmds.shadingNode('GLSLShader', asShader=True, n=newName)

    # assign shader to selected
    cmds.select(objs, r=True)
    cmds.hyperShade(assign=shader)

    if prototype != "shaderFX":
        lib.setAttr(shader, "shader", shaderFile)
        lib.setAttr(shader, "xUseControl", False)

    return shader
예제 #3
0
def paintKeyClicked(widget, key=True):
    """
    Inserts or removes a keyframe on the vertex colors of a shape
    Args:
        widget (PaintWidget): PaintWidget object calling the function
        key (bool): Key or remove key
    """
    # get vertices to key
    mel.eval("ConvertSelectionToVertices;")
    selectedVertices = cmds.ls(sl=True, et="float3")
    if selectedVertices:
        c = ["ColorR", "ColorG", "ColorB", "Alpha"]
        # get shape, channels and colorset
        shapes = lib.getShapes(selectedVertices)
        channels = widget.fx.channels
        colorSet = widget.fx.controlSet
        cmds.polyColorSet(shapes, currentColorSet=True,
                          cs=colorSet)  # sets the current color set of shapes

        # get suffix of attribute to key
        channelIdx = math.trunc(paintIndex(widget) / 2.0)
        suffix = ""
        for idx in xrange(len(channels[channelIdx])):
            if channels[channelIdx][idx]:
                suffix = c[idx]
                break
        # vertex colors in maya are stored per adjacent face, to minimize the amount
        # of animation curves, we can find exactly which vtx face and attribute to key
        # in the specified vertex color set, and its respective polyColorPerVertex node
        vtxFaces = [vtx.replace("vtx", "vtxFace") for vtx in selectedVertices]
        vtxFaceAttrs = cmds.listAttr(
            vtxFaces, s=True)  # list attributes of adjacent faces
        attributes = [
            attr for attr in vtxFaceAttrs
            if "vertexFace{0}".format(suffix) in attr
        ]
        pColorVertexNodes = polyColorPerVertexNodes(shapes, colorSet)
        if pColorVertexNodes:
            for attr in attributes:
                if key:
                    # key vertex color attribute
                    cmds.setKeyframe("{0}.{1}".format(pColorVertexNodes[0],
                                                      attr))
                else:
                    # remove the vertex color key
                    currentTime = cmds.currentTime(query=True)
                    cmds.cutKey("{0}.{1}".format(pColorVertexNodes[0], attr),
                                time=(currentTime, currentTime))
        else:
            cmds.error(
                "History has been deleted from the mesh object, keying of vertex colors is impossible"
            )

    showKeyedTimeline(widget)
예제 #4
0
def paint(RGBA, paintType, cClamp=["both", 0, 1], aClamp=["none", 0, 1], colorSet="controlSetA"):
    """
    Set artisan context with painting and brush parameters for artAttrPaintVertexCtx
    Args:
        RGBA (list): Channels to paint e.g. [ 0, 1, 0, 0 ]
        paintType (str): "additive" or "subtract"
        cClamp (list): Clamping settings for color [clamp, clamplower, clampupper]
        aClamp (list): Clamping settings for alpha [clamp, clamplower, clampupper]
        colorSet (str): Color set to paint into
    """
    # SET UP ARTISAN CONTEXT
    # set channels to RGBA and enable paint tool attributes
    cmds.radioButtonGrp('artAttrColorChannelChoices', sl=2, e=True)  # set to RGBA in artisan UI
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', paintRGBA=True, e=True)  # set to RGBA
    cmds.floatSliderGrp('colorPerVertexAlpha', en=True, e=True)  # enable alpha painting
    cmds.floatFieldGrp('colorPerVertexMinMaxAlphaValue', en=True, e=True)  # enable alpha min max
    cmds.checkBoxGrp('artAttrAlphaClampChkBox', en=True, e=True)  # enable alpha clamp checkbox

    # before stroke command
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext',
                               bsc="artAttrPaintVertexCtx -edit -showactive 0 artAttrColorPerVertexContext", e=True)
    # after stroke command
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext',
                               asc="artAttrPaintVertexCtx -edit -showactive 1 artAttrColorPerVertexContext", e=True)

    # PREPARE MESHES
    selected = cmds.ls(sl=True)
    shapes = lib.getShapes(selected)
    enableVtxCtrl(shapes)

    # SET UP PAINTING PARAMETERS
    cmds.polyColorSet(shapes, currentColorSet=True, cs=colorSet)  # sets the current color set of all shapes
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', selectedattroper=paintType, e=True)  # add or substract color
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', cl4=RGBA, e=True)  # define channels to paint
    cmds.floatSliderGrp('colorPerVertexAlpha', value=RGBA[3], e=True)  # set alpha painting value
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', opacity=0.2, e=True)  # set opacity to 0.2
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', accopacity=True, e=True)  # oppacity accumulates
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', clamp=cClamp[0], e=True)  # color clamp
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', clamplower=cClamp[1], e=True)  # lower clamp
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', clampupper=cClamp[2], e=True)  # upper clamp
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', alphaclamp=aClamp[0], e=True)  # alpha clamp
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', alphaclamplower=aClamp[1], e=True)  # alpha lower clamp
    cmds.artAttrPaintVertexCtx('artAttrColorPerVertexContext', alphaclampupper=aClamp[2], e=True)  #alpha upper clamp
예제 #5
0
    def load(self, name, options):
        """
        Loads the specified attribute set
        Args:
            name (str): Name of the attribute set to import
        """
        # get data
        selection = cmds.ls(sl=True, l=True)
        mat, xform = getMaterial(selection)
        if not mat:
            cmds.error("Nothing was selected")

        # if not the same material type, create new material
        matType = cmds.objectType(mat)
        try:
            graph = self[name]['graph']
        except KeyError:
            graph = "mnpr_uber"

        prevGraph = "NA"
        if self[name]['type'] != matType:
            mat = createMaterial(xform, graph=graph)
        else:
            # shaderFX shader, get current graph name
            try:
                nodeId = cmds.shaderfx(sfxnode=mat,
                                       getNodeIDByName="graphName")
                prevGraph = cmds.shaderfx(sfxnode=mat,
                                          getPropertyValue=(nodeId, "value"))
            except RuntimeError:
                pass
            # if a new material is desired, create anyways
            if options["newMaterial"]:
                mat = createMaterial([xform[0]], graph=graph)
            elif graph != prevGraph:
                shaderFile = os.path.join(mnpr_info.environment, "shaders",
                                          "{0}.sfx".format(graph))
                cmds.shaderfx(sfxnode=mat, loadGraph=shaderFile)

        # default lighting in case there are no lights
        defaultLighting()

        shapes = lib.getShapes(xform)
        # if colorSets are present, enable control to avoid wrong vertex stylization inputs
        if cmds.polyColorSet(shapes, query=True, allColorSets=True):
            mnpr_pFX.enableVtxCtrl(shapes)
        # disable/enable shadows when proxy geometry is involved
        if graph == "mnpr_geoproxy":
            for shape in shapes:
                lib.setAttr(shape, "castsShadows", False)
                lib.setAttr(shape, "receiveShadows", False)
        elif prevGraph == "mnpr_geoproxy":
            for shape in shapes:
                lib.setAttr(shape, "castsShadows", True)
                lib.setAttr(shape, "receiveShadows", True)

        # set material settings and attributes
        if matType == 'ShaderfxShader':
            setMaterialAttrs(mat, self[name], options)
        else:
            # set attributes in material
            print "->{0} will be replaced".format(mat)
            attrs = self[name]['attributes']
            for attr in attrs:
                lib.setAttr(mat, attr, attrs[attr])
            cmds.select(mat, r=True)