Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Maya custom shader/texture outputs single color

1 REPLY 1
Reply
Message 1 of 2
grijs013
471 Views, 1 Reply

Maya custom shader/texture outputs single color

Hi there,

 

I am trying to write a "masking" plugin for maya. It uses a mesh as an input, and masks the mesh which has been assigned the custom 'masking' shader. What I am trying to get done is,  if the input mesh (attribute is a string) overlaps the shaded mesh, then that part will be visible in the final render, if not, it will be transparent. For this I use mfnmesh.closestIntersection. When I print to see if  a ray has hit the mask mesh, or not, I can see that it should be working, but the outColor is a single color. For this plugin I used the depthshader as base.

I dont know why, but I dont seem to get it working. And the depthshader doesnt work either. Both plugins just output a single color to the outColor attribute. But if a print the So, what am I doing wrong, or is it something I overlooked?

 

I have maya 2020.2 installed on fedora 32. I have an indie license.

Nvidia card, with proprietary drivers (450.66)

 

Thanks in advance.

Below I have pasted the contents of my source file:

 

# testShader.py
import sys
import math
import random
import maya.api.OpenMaya as om
import maya.cmds as mc

#-----------------------------------------------------------------------
def maya_useNewAPI():
pass

#-----------------------------------------------------------------------
kPluginNodeName = 'pyTestShader'
kPluginNodeClassify = 'utility/general'
kPluginNodeId = om.MTypeId(0x871FE)


#-----------------------------------------------------------------------
def stringToDag(var):
# ~ om.MGlobal.selectByName(var)
selList = om.MGlobal.getSelectionListByName(var)
if selList.length() > 0:
return selList.getDagPath(0)
else:
return None


#-----------------------------------------------------------------------
class testShader(om.MPxNode):
outColorAttribute = om.MObject()
pointCamera = om.MObject()
rayOrigin = om.MObject()
maskObject = om.MObject()
matrixEyeToWorld = om.MObject()

def __init__(self):
om.MPxNode.__init__(self)

def compute(self, plug, datablock):
if (plug == testShader.outColorAttribute):

uv = datablock.inputValue( testShader.aUVCoord ).asFloat2()
sampledPointHandle = datablock.inputValue( testShader.pointCamera )
sampledPointasVector = sampledPointHandle.asFloatVector()
sampledPoint = om.MFloatPoint(sampledPointasVector)

matrixEyeToWorldHandle = datablock.inputValue( testShader.matrixEyeToWorld )
matrixEyeToWorld = matrixEyeToWorldHandle.asFloatMatrix()

maskObjectHandle = datablock.inputValue( testShader.maskObject )
maskObject = maskObjectHandle.asString()
dagpath = stringToDag(maskObject)

rayOriginHandle = datablock.inputValue( testShader.rayOrigin )
rayOrigin = om.MFloatPoint(rayOriginHandle.asFloatVector())

outColorDataHandle = datablock.outputValue(testShader.outColorAttribute)
outColor = om.MFloatVector( 0,0,0 )
# ~ outColor = om.MFloatVector(random.random(),random.random(),random.random())

if dagpath:
sampledPointWS = sampledPoint*matrixEyeToWorld
rayOriginWS = rayOrigin*matrixEyeToWorld
rayDirWS = sampledPointWS - rayOriginWS
newEndPOint = rayOriginWS + rayDirWS
# ~ mc.curve(d=1, p=[[rayOriginWS.x,rayOriginWS.y,rayOriginWS.z], [newEndPOint.x,newEndPOint.y,newEndPOint.z]] )
mfnMesh = om.MFnMesh(dagpath)
results = mfnMesh.closestIntersection(rayOriginWS, rayDirWS, om.MSpace.kWorld, 10000, False, idsSorted=True, tolerance=0.0000001)

if results[2] == -1:
outColor.x = 1
else:
outColor.y = 1

else:
outColor.z = 1

outColorDataHandle.setMFloatVector(outColor)
outColorDataHandle.setClean()

else:
return None

#-----------------------------------------------------------------------
def nodeCreator():
return testShader()

#-----------------------------------------------------------------------
def nodeInitializer():
nAttr = om.MFnNumericAttribute()
tAttr = om.MFnTypedAttribute()
mAttr = om.MFnMatrixAttribute()


child1 = nAttr.create( "uCoord", "u", om.MFnNumericData.kFloat)
child2 = nAttr.create( "vCoord", "v", om.MFnNumericData.kFloat)
testShader.aUVCoord = nAttr.create( "uvCoord", "uv", child1, child2)
nAttr.keyable = True
nAttr.storable = True
nAttr.readable = True
nAttr.writable = True
nAttr.hidden = True
testShader.addAttribute(testShader.aUVCoord)

child1 = nAttr.create( "uvFilterSizeX", "fsx", om.MFnNumericData.kFloat)
child2 = nAttr.create( "uvFilterSizeY", "fsy", om.MFnNumericData.kFloat)
testShader.aFilterSize = nAttr.create("uvFilterSize", "fs", child1, child2)
nAttr.keyable = True
nAttr.storable = True
nAttr.readable = True
nAttr.writable = True
nAttr.hidden = True
testShader.addAttribute(testShader.aFilterSize)


testShader.pointCamera = nAttr.createPoint('pointCamera', 'p')
nAttr.storable = False
nAttr.hidden = True
testShader.addAttribute( testShader.pointCamera )


testShader.rayOrigin = nAttr.createPoint( 'rayOrigin', 'ro')
nAttr.storable = False
nAttr.hidden = True
testShader.addAttribute( testShader.rayOrigin )


testShader.maskObject = tAttr.create('maskObject', 'mo', om.MFnData.kString)
tAttr.storable = True
tAttr.writable = True
tAttr.hidden = False
testShader.addAttribute( testShader.maskObject )


testShader.matrixEyeToWorld = mAttr.create( 'matrixEyeToWorld', 'mew', om.MFnMatrixAttribute.kFloat)
mAttr.storable = False
mAttr.hidden = True
testShader.addAttribute( testShader.matrixEyeToWorld )


testShader.outColorAttribute = nAttr.createColor( 'outColor', 'oc')
nAttr.storable = False
nAttr.writable = False
nAttr.readable = True
nAttr.hidden = False
testShader.addAttribute( testShader.outColorAttribute )


testShader.attributeAffects( testShader.pointCamera, testShader.outColorAttribute )
testShader.attributeAffects( testShader.matrixEyeToWorld, testShader.outColorAttribute )
testShader.attributeAffects( testShader.maskObject, testShader.outColorAttribute )
testShader.attributeAffects( testShader.rayOrigin, testShader.outColorAttribute )
testShader.attributeAffects( testShader.aFilterSize, testShader.outColorAttribute )
testShader.attributeAffects( testShader.aUVCoord, testShader.outColorAttribute )


#-----------------------------------------------------------------------
def initializePlugin( mobject 😞
mplugin = om.MFnPlugin( mobject )
try:
mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator, nodeInitializer, om.MPxNode.kDependNode, kPluginNodeClassify )
except:
sys.stderr.write( 'Failed to register node: ' + kPluginNodeName )
raise


def uninitializePlugin( mobject 😞
mplugin = om.MFnPlugin( mobject )
try:
mplugin.deregisterNode( kPluginNodeId )
except:
sys.stderr.write( 'Failed to deregister node: ' + kPluginNodeName )
raise

 

Labels (4)
1 REPLY 1
Message 2 of 2
grijs013
in reply to: grijs013

I tried different thing, but nothing helped, but then I thought, maybe it has something todo with the python api 2.0?

Because as you can see in the code I pasted above in theprevious post, I used the python api 2.0.

So, I rewrote the plugin using api python 1.0, and you know what, it now it works as expected! Great!

Now I need to figure out which section of the api 2.0 of the plugin is failing. Perhaps I am going to do that some day. But first I am going to finish writing my plugin..

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report