#-----------Value to String Node-------------#
#----------------Version 1.0-------------------#
#--------------By Peter Rabel-----------------#
import maya.OpenMaya as OpenMaya
import maya.OpenMayaMPx as OpenMayaMPx
import sys

kcompUtilNodeTypeName = "valueToString"
kcompUtilNodeClassify = "utility/general"
kcompUtilNodeId = OpenMaya.MTypeId(0x87477)

class valueToStringNode(OpenMayaMPx.MPxNode):

	#define class variables
	input = OpenMaya.MObject()
	prefix = OpenMaya.MObject()
	suffix = OpenMaya.MObject()
	limitPrecision = OpenMaya.MObject()
	output = OpenMaya.MObject()
	
	def __init__(self):
		OpenMayaMPx.MPxNode.__init__(self)
		
	def compute(self, plug, dataBlock):
	
		# if these attributes are requested, recompute their values
		if plug == valueToStringNode.output or \
			valueToStringNode.prefix or valueToStringNode.suffix:
		
			# get MDataHandle's to attributes
			try:
				input_dataHandle = dataBlock.inputValue( valueToStringNode.input )
			except:
				sys.stderr.write( "Failed to get MDataHandle input input" )
				raise
			try:
				prefix_dataHandle = dataBlock.inputValue( valueToStringNode.prefix )
			except:
				sys.stderr.write( "Failed to get MDataHandle prefix input" )
				raise
			try:
				suffix_dataHandle = dataBlock.inputValue( valueToStringNode.suffix )
			except:
				sys.stderr.write( "Failed to get MDataHandle suffix input" )
				raise
			try:
				limitPrecision_dataHandle = dataBlock.inputValue( valueToStringNode.limitPrecision )
			except:
				sys.stderr.write( "Failed to get MDataHandle limitPrecision input" )
				raise
			try:
				output_dataHandle = dataBlock.outputValue( valueToStringNode.output )
			except:
				sys.stderr.write( "Failed to get MDataHandle output input" )
				raise
                
			#get values from data handles
			input = input_dataHandle.asFloat()
			prefix = prefix_dataHandle.asString()
			suffix = suffix_dataHandle.asString()
			limitPrecision = limitPrecision_dataHandle.asInt()
			output = output_dataHandle.asString()
			
		else:
			return OpenMaya.kUnknownParameter
			
		#run computation
################Compute
		if limitPrecision > 0:
			var = "%." + str(limitPrecision) + "f"
			res = var %input
		else:
			res = 0
		output_dataHandle.setString( prefix + str(res) + suffix )
				
		dataBlock.setClean(plug)
		
		return OpenMaya.MStatus.kSuccess
		
# defines createNode function in maya for this node.
def nodeCreator():
	return OpenMayaMPx.asMPxPtr( valueToStringNode() )
	
# create and initialize the attributes to the node
def nodeInitializer():

	nAttr = OpenMaya.MFnNumericAttribute()
	cAttr = OpenMaya.MFnCompoundAttribute()
	eAttr = OpenMaya.MFnEnumAttribute()
	tAttr = OpenMaya.MFnTypedAttribute()

	# create input attributes
	valueToStringNode.input = nAttr.create("input", "in", OpenMaya.MFnNumericData.kFloat, 0.0)
	nAttr.setWritable(True)
	nAttr.setStorable(True)
	nAttr.setReadable(True)
	nAttr.setKeyable(True)
	
	# create input attributes
	valueToStringNode.prefix = tAttr.create("prefix", "pr", OpenMaya.MFnNumericData.kString)
	tAttr.setWritable(True)
	tAttr.setStorable(True)
	tAttr.setReadable(True)
	tAttr.setKeyable(True)
	
	# create input attributes
	valueToStringNode.suffix = tAttr.create("suffix", "su", OpenMaya.MFnNumericData.kString)
	tAttr.setWritable(True)
	tAttr.setStorable(True)
	tAttr.setReadable(True)
	tAttr.setKeyable(True)
	
	# create input attributes
	valueToStringNode.limitPrecision = nAttr.create("limitPrecision", "lp", OpenMaya.MFnNumericData.kInt, 5)
	nAttr.setWritable(True)
	nAttr.setStorable(True)
	nAttr.setReadable(True)
	nAttr.setKeyable(True)
	
	# create output attributes
	valueToStringNode.output = tAttr.create("output", "out", OpenMaya.MFnStringData.kString)
	tAttr.setWritable(False)
	tAttr.setStorable(True)
	tAttr.setReadable(True)
	
	# add attribues
	#
	valueToStringNode.addAttribute( valueToStringNode.input)
	valueToStringNode.addAttribute( valueToStringNode.prefix)
	valueToStringNode.addAttribute( valueToStringNode.suffix)
	valueToStringNode.addAttribute( valueToStringNode.limitPrecision)
	valueToStringNode.addAttribute( valueToStringNode.output)
	
	# Setup which attributes affect each other
	valueToStringNode.attributeAffects ( valueToStringNode.input,  valueToStringNode.output )
	valueToStringNode.attributeAffects ( valueToStringNode.prefix,  valueToStringNode.output )
	valueToStringNode.attributeAffects ( valueToStringNode.suffix,  valueToStringNode.output )
	valueToStringNode.attributeAffects ( valueToStringNode.limitPrecision,  valueToStringNode.output )
	#valueToStringNode.attributeAffects ( valueToStringNode.output,  valueToStringNode.input )
	
# initialize the script plug-in
def initializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject, "Autodesk", "1.0", "Any")
	try:
		mplugin.registerNode( kcompUtilNodeTypeName, kcompUtilNodeId, nodeCreator, nodeInitializer, OpenMayaMPx.MPxNode.kDependNode, kcompUtilNodeClassify)
	except:
		sys.stderr.write( "Failed to register node: %s" % kcompUtilNodeTypeName )
		raise
		
# uninitialize the script plug-in
def uninitializePlugin(mobject):
	mplugin = OpenMayaMPx.MFnPlugin(mobject)
	try:
		mplugin.deregisterNode( kcompUtilNodeId )
	except:
		sys.stderr.write( "Failed to deregister node: %s" % kcompUtilNodeTypeName )
		raise
