For the class below, I'm getting this exception:
AttributeError: LineIntensityProfileLogic instance has no attribute 'probeVolume'
How can I solve this issue? Thanks
class LineIntensityProfileLogic(ScriptedLoadableModuleLogic):
"""This class should implement all the actual
computation done by your module. The interface
should be such that other python code can import
this class and make use of the functionality without
requiring an instance of the Widget.
Uses ScriptedLoadableModuleLogic base class, available at:
https://github.com/Slicer/Slicer/blob/master/Base/Python/slicer/ScriptedLoadableModule.py
"""
def hasImageData(self,volumeNode):
"""This is a dummy logic method that
returns true if the passed in volume
node has valid image data
"""
if not volumeNode:
print('no volume node')
return False
if volumeNode.GetImageData() == None:
print('no image data')
return False
return True
def takeScreenshot(self,name,description,type=-1):
# show the message even if not taking a screen shot
self.delayDisplay(description)
if self.enableScreenshots == 0:
return
lm = slicer.app.layoutManager()
# switch on the type to get the requested window
widget = 0
if type == slicer.qMRMLScreenShotDialog.FullLayout:
# full layout
widget = lm.viewport()
elif type == slicer.qMRMLScreenShotDialog.ThreeD:
# just the 3D window
widget = lm.threeDWidget(0).threeDView()
elif type == slicer.qMRMLScreenShotDialog.Red:
# red slice window
widget = lm.sliceWidget("Red")
elif type == slicer.qMRMLScreenShotDialog.Yellow:
# yellow slice window
widget = lm.sliceWidget("Yellow")
elif type == slicer.qMRMLScreenShotDialog.Green:
# green slice window
widget = lm.sliceWidget("Green")
else:
# default to using the full window
widget = slicer.util.mainWindow()
# reset the type so that the node is set correctly
type = slicer.qMRMLScreenShotDialog.FullLayout
# grab and convert to vtk image data
qpixMap = qt.QPixmap().grabWidget(widget)
qimage = qpixMap.toImage()
imageData = vtk.vtkImageData()
slicer.qMRMLUtils().qImageToVtkImageData(qimage,imageData)
annotationLogic = slicer.modules.annotations.logic()
annotationLogic.CreateSnapShot(name, description, type, self.screenshotScaleFactor, imageData)
def run(self,volumeNode1,volumeNode2,rulerNode,enableScreenshots=0,screenshotScaleFactor=1):
"""
Run the actual algorithm
"""
print('LineIntensityProfileLogic run() called')
"""
1. get the list(s) of intensity samples along the ruler
2. set up quantitative layout
3. use the chart view to plot the intensity sampless
"""
"""
1. get the list of samples
"""
if not rulerNode or (not volumeNode1 and not volumeNode2):
print('Inputs are not initialized')
return
volumeSamples1 = None
volumeSamples2 = None
if volumeNode1:
volumeSamples1 = self.probeVolume(volumeNode1, rulerNode)
if volumeNode2:
volumeSamples2 = self.probeVolume(volumeNode2, rulerNode)
print('volumeSamples1 = '+str(volumeSamples1))
print('volumeSamples2 = '+str(volumeSamples2))
imageSamples = [volumeSamples1, volumeSamples2]
legendNames = [volumeNode1.GetName()+' - '+rulerNode.GetName(), volumeNode2.GetName+' - '+rulerNode.GetName()]
self.showChart(imageSamples, legendNames)
self.delayDisplay('Running the aglorithm')
self.enableScreenshots = enableScreenshots
self.screenshotScaleFactor = screenshotScaleFactor
self.takeScreenshot('LineIntensityProfile-Start','Start',-1)
return True
def probeVolume(self,volumeNode,rulerNode):
# get ruler endpoints coordinates in RAS
p0ras = rulerNode.GetPolyData().GetPoint(0)+(1,)
p1ras = rulerNode.GetPolyData().GetPoint(1)+(1,)
# Convert RAS to IJK coordinates of the vtkImageData
ras2ijk = vtk.vtkMatrix4x4()
volumeNode.GetRASToIJKMatrix(ras2ijk)
p0ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p0ras)[:3]]
p1ijk = [int(round(c)) for c in ras2ijk.MultiplyPoint(p1ras)[:3]]
# Create VTK line that will be used for sampling
line = vtk.vtkLineSource()
line.SetResolution(100)
line.SetPoint1(p0ijk[0],p0ijk[1],p0ijk[2])
line.SetPoint2(p1ijk[0],p1ijk[1],p1ijk[2])
# Create VTK probe filter and sample the image
probe = vtk.vtkProbeFilter()
probe.SetInputConnection(line.GetOutputPort())
probe.SetSourceData(volumeNode.GetImageData())
probe.Update()
# return VTK array
return probe.GetOutput().GetPointData().GetArray('ImageScalars')
def showChart(self, samples, names):
print("Logic showing chart")
# Switch to a layout containing a chart viewer
lm = slicer.app.layoutManager()
lm.setLayout(slicer.vtkMRMLLayoutNode.SlicerLayoutFourUpQuantitativeView)
# Initialize double array MRML node for each sample list since this is ,
# what chart view MRML node needs
doubleArrays = []
for sample in samples:
arrayNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLDoubleArrayNode())
array = arrayNode.GetArray()
nDataPoints = sample.GetNumberOfTuples()
array.SetNumberOfTuples(nDataPoints)
array.SetNumberOfComponents(3)
for i in range(nDataPoints):
array.SetComponent(i, 0, i)
array.SetComponent(i, 1, sample.GetTuple(i))
array.SetComponent(i, 2, 0)
doubleArrays.append(arrayNode)
# Get the chart view MRML node
cvNodes = slicer.mrmlScene.GetNodesByClass('vtkMRMLChartViewNode')
cvNodes.SetReferenceCount(cvNodes.GetReferenceCount()-1)
cvNodes.InitTraversal()
cvNode = cvNodes.GetNextItemAsObject()
# Create a new chart node
chartNode = slicer.mrmlScene.AddNode(slicer.vtkMRMLChartNode())
for pairs in zip(names, doubleArrays):
chartNode.AddArray(pairs[0], pairs[1], GetID())
cvNode.SetChartNodeID(chartNode.GetID())
return
You seem to be using 2 spaces for indentation.
But for the function - probeVolume , it is indented 4 spaces , which caused it to be inside function run , are you sure that indentation is correct?
Not just probeVolume , function - showChart is also indented 4 spaces,
Related
I'm writing a tool that will help make me make custom uvSets on a model that will be linked to ramps(no interpolation) that will be sent to vray_extraTex render elements. these will become mattes and used in the same way as vRay multi mattes. however, I am not able to link the ramp that is my texture for the vray_extraTex to the custom uvSet using pymel.
I can do all this manually in Maya but for some reason, I am missing something for pymel to link the UVs to the ramp. I am testing in a Maya scene with a pSphere that has two uvSets and the second set is active. This code has been stripped down a bit:
def main():
inclusiveSet = None
renderElements =[]
ramps = []
newChannels = ['TestA','TestB','TestC','TestD']
for i, channel in enumerate(newChannels):
modIndex = i % 3 # 0:Red, 1:Green, 2:Blue
shapeNode=pm.PyNode('pSphereShape1')
transformNode=shapeNode.getTransform()
if modIndex == 0: # the first channel in the new Render Element
# make an etex render element
eTexElement = pm.PyNode(pm.mel.eval('vrayAddRenderElement("ExtraTexElement")'))
eTexElement.vray_name_extratex.set('')
eTexElement.vray_explicit_name_extratex.set('empty_empty_empy')
renderElements.append(eTexElement)
# make a ramp
ramp = pm.shadingNode('ramp', asTexture=True, name='eTex_{}_ramp'.format(transformNode.name()))
ramps.append(ramp)
ramp.outColor.connect(eTexElement.vray_texture_extratex)
# make a place2dtexture
place2d = pm.shadingNode('place2dTexture', asUtility=True)
place2d.outUV.connect(ramp.uv)
place2d.translateFrameU.set(len(renderElements) - 1)
# link UVs to ramp
# NOT WORKING
indices = pm.polyUVSet(shapeNode.name(), query=True, allUVSetsIndices=True)
currentUVSet = pm.polyUVSet(shapeNode, query=True, currentUVSet=True )
for i in indices:
if currentUVSet == pm.getAttr("{}.uvSet[{}].uvSetName".format(shapeNode.name(), i)):
pm.uvLink(uvSet='{}.uvSet[{}].uvSetName'.format(shapeNode.name(), i) , texture=ramp)
explicit_name = eTexElement.vray_explicit_name_extratex.get()
nameTokens = explicit_name.split('_')
nameTokens[modIndex] = channel
explicit_name = '_'.join(nameTokens)
eTexElement.vray_explicit_name_extratex.set(explicit_name)
main()
I get no errors but when I check the UV Linking the ramps are still set to map1 uvSet and not the second set that was active.
I expected to see the ramps connected to the uvChooser node and linked to the second uvSet.
I realized while writing this post that maybe I need to attach the ramps to the shader that is assigned to the geo before I can uvlink them with python. I'll try and test that next
so after some more testing, I was able to get it to work after assigning the ramps to a new attr on the objects' shader and with a couple of minor fixes to the code I posted before. fixed:
def connectTexture(layeredTex, shapeNode):
shadingGrps = pm.listConnections(shapeNode,type='shadingEngine')
shaders = pm.ls(pm.listConnections(shadingGrps),materials=1)
shader = shaders[0]
pm.addAttr(shader, longName='eTexLayeredTex', usedAsColor=True, attributeType='float3' )
pm.addAttr(shader, longName='eTexLayeredTexR', attributeType='float', parent='eTexLayeredTex' )
pm.addAttr(shader, longName='eTexLayeredTexG', attributeType='float', parent='eTexLayeredTex' )
pm.addAttr(shader, longName='eTexLayeredTexB', attributeType='float', parent='eTexLayeredTex' )
layeredTex.outColor.connect(shader.eTexLayeredTex)
def main():
inclusiveSet=None
renderElements=[]
ramps=[]
newChannels = ['TestA','TestB','TestC','TestD']
shapeNode=pm.PyNode('pSphereShape1')
transformNode=shapeNode.getTransform()
# make a layeredTexture
layeredTexture = pm.shadingNode('layeredTexture', asTexture=True, name='eTex_{}_layeredTexture'.format(transformNode.name()))
layeredTexture.attr('inputs')[0].color.set(0,0,0)
layeredTexture.attr('inputs')[0].alpha.set(1)
layeredTexture.attr('inputs')[0].blendMode.set(1)
connectTexture(layeredTexture, shapeNode)
for i, channel in enumerate(newChannels):
modIndex = i % 3 # 0:Red, 1:Green, 2:Blue
if modIndex == 0: # the first channel in the new Render Element
# make an etex render element
eTexElement = pm.PyNode(pm.mel.eval('vrayAddRenderElement("ExtraTexElement")'))
eTexElement.vray_name_extratex.set('')
eTexElement.vray_explicit_name_extratex.set('empty_empty_empy')
renderElements.append(eTexElement)
# make a ramp
ramp = pm.shadingNode('ramp', asTexture=True, name='eTex_{}_ramp'.format(transformNode.name()))
ramps.append(ramp)
ramp.interpolation.set(0)
ramp.colorEntryList[0].position.set(0.0)
ramp.colorEntryList[1].position.set((1.0 / 3.0))
ramp.colorEntryList[2].position.set((2.0 / 3.0))
ramp.colorEntryList[0].color.set(1,0,0)
ramp.colorEntryList[1].color.set(0,1,0)
ramp.colorEntryList[2].color.set(0,0,1)
ramp.defaultColor.set(0,0,0)
ramp.outColor.connect(eTexElement.vray_texture_extratex)
ramp.outColor.connect( layeredTexture.attr( 'inputs[{}].color'.format( 1 + i // 3)))
# make a place2dtexture
place2d = pm.shadingNode('place2dTexture', asUtility=True)
place2d.outUV.connect(ramp.uv)
place2d.outUvFilterSize.connect(ramp.uvFilterSize)
place2d.wrapU.set(0)
place2d.wrapV.set(0)
place2d.translateFrameU.set(len(renderElements) - 1)
# link UVs to ramp
indices = pm.polyUVSet(shapeNode.name(), query=True, allUVSetsIndices=True)
currentUVSet = pm.polyUVSet(shapeNode, query=True, currentUVSet=True )[0]
for index in indices:
if currentUVSet == pm.getAttr('{}.uvSet[{}].uvSetName'.format(shapeNode.name(), index)):
pm.uvLink(uvSet='{}.uvSet[{}].uvSetName'.format(shapeNode.name(), index) , texture=ramp)
explicit_name = eTexElement.vray_explicit_name_extratex.get()
nameTokens = explicit_name.split('_')
nameTokens[modIndex] = channel
explicit_name = '_'.join(nameTokens)
eTexElement.vray_explicit_name_extratex.set(explicit_name)
main()
they key was connecting the ramps to a material assigned to the object. Strangely after the uvLinks are made you can delete the connection between the ramps and the material and the uvLinks still work correctly. I posted this in case someone comes across a similar problem
I really don't now why I can't call the method setTRSKey from inside my for loop. Am I missing something? This makes no sense to me at all. Pycharm declares it as an unsolved reference
Here is the code:
import math
import nuke
originNode = nuke.selectedNode()
world_matrix = originNode['world_matrix'] # this is an iArray Knob with 16 fields
mResult = nuke.math.Matrix4() # Matrix to copy iArray to
# Ask user for Frame Range operation
ret = nuke.getFramesAndViews('Frame range', '%s-%s' % (nuke.root().firstFrame(), nuke.root().lastFrame()))
if ret != None:
nuke.nodeCopy("%clipboard%") # creating node duplicate
originNode.setSelected(False)
newNode = nuke.nodePaste("%clipboard%") # creating origin node duplicate
newNode['translate'].clearAnimated()
newNode['translate'].setValue(0)
newNode['translate'].setAnimated()
newNode['rotate'].clearAnimated()
newNode['rotate'].setValue(0)
newNode['rotate'].setAnimated()
newNode['scaling'].clearAnimated()
newNode['scaling'].setValue(0)
newNode['scaling'].setAnimated()
frange = nuke.FrameRange(ret[0]) # convert to frange object
for frame in frange:
for i in xrange(0, 16):
mResult[i] = world_matrix.valueAt(frame)[i]
mResult.transpose() # row become columns and vice versa
mTranslate = nuke.math.Matrix4(mResult)
mTranslate.translationOnly()
mRotate = nuke.math.Matrix4(mResult)
mRotate.rotationOnly()
mScale = nuke.math.Matrix4(mResult)
mScale.scaleOnly()
translate = (mTranslate[12], mTranslate[13], mTranslate[14])
rotateRad = mRotate.rotationsZXY()
rotate = (math.degrees(rotateRad[0]), math.degrees(rotateRad[1]),
math.degrees(rotateRad[2])) # convert from radiants to defrees
scale = (mScale.xAxis().x, mScale.yAxis().y, mScale.zAxis().z)
setTRSKey(frame, translate, rotate, scale)
else:
print "User canceled the operation"
def setTRSKey(frame, translate, rotate, scale):
print type(translate(0))
newNode['translate'].setValueAt(translate(0), frame, 0)
newNode['translate'].setValueAt(translate(1), frame, 1)
newNode['translate'].setValueAt(translate(2), frame, 2)
edit: Example with classes where loadDataFromScript is called before defining
class Connecthor(QtWidgets.QDialog, Ui_Dialog):
#
allowedNodes = ["Read", "Write", "Merge", "Keymix", "ChannelMerge", "Roto", "RotoPaint", "Copy", "Shuffle", "PostageStamp", "Camera", "Camera2", "ScanlineRender", "Connector", "ReadGeo", "ReadGeo2", "BackdropNode"]
script_path = os.path.dirname(os.path.realpath(__file__))
#constructor
def __init__(self, parent=None):
super(Connecthor, self).__init__(parent)
self.setupUi(self)
self.setFixedSize(self.size())
#self.setWindowOpacity(0.95)
popupmenu = QtWidgets.QMenu(self.btn_settings)
#popupmenu.addAction("save links for script", self.writeListDictToJson)
#popupmenu.addAction("import links from json", self.readJsonToDict)
popupmenu.addAction("save links for script (manual)", self.saveDatatoScript)
popupmenu.addAction("settings", self.opensetting)
self.btn_settings.setMenu(popupmenu)
self.btn_settings.setIcon(QtGui.QIcon(os.path.join(iconpath, "settings.png")))
self.btn_addSelectedNodes.setIcon(QtGui.QIcon(os.path.join(iconpath, "add.png")))
self.btn_addSelectedNodes.clicked.connect(self.addSelectedNodes)
# #Loading test Json
#self.readJsonToDict()
self.loadDataFromScript()
In Python you must define functions before they are called. Move your setTRSKey definition above the for loop. Generally speaking, function definitions are one of the very first things in the file after imports, though this is not always the case.
I have a workaround to connect the transparency of a Metrial to the keyframe bar in Maya. I create a new Material press "s" for a keyframe got to the keyframe 10, set the transparency to 0 and press again "s".
So you are able to fade the transparency beteween the 1 and 10 keyframe.
I want to script this in python and I have no idea how to do this.
here is what I've made. I tested it with basic Maya materials, but it should work for any material wich has a transparency attribute (note that it won't work for surface shaders as the transparency attribute is called outTransparency, but you could fix it by changing the function so you could also pass it the attribute name).
I got the getShaderFrom (obj) function in this topic and just converted it in Python (it's in MEL).
I made it as simple and detailled as possible but it could probably be optimized. Anyway, hope this answered your question!
from maya import cmds
def getShaderFrom (obj):
'''
List all materials for the given object
'''
# List the shapes of the given object
shapes = cmds.ls (obj, objectsOnly = 1, dagObjects = 1, shapes = 1)
# List the shading engines connected to the shapes
shadingEngines = cmds.listConnections (shapes, type = "shadingEngine")
# List the materails connected to the shading engines
rawMaterials = cmds.ls ((cmds.listConnections (shadingEngines)), materials = 1)
# Remove duplicated occurences of materials
materials = []
for mat in rawMaterials:
if mat not in materials:
materials.append(mat)
return materials
def keyTransparency (startFrame, endFrame, material):
'''
Will key the transparency of
a given materials between
selected frames
'''
# Set transparency value at 1
cmds.setAttr (material + ".transparency", 1,1,1, type = "double3")
# Add a keyframe at last frame
cmds.setKeyframe (material, attribute = "transparency", time = endFrame, inTangentType = "linear", outTangentType = "linear")
# Add a keyframe at first frame
cmds.setKeyframe (material, attribute = "transparency", time = startFrame, inTangentType = "linear", outTangentType = "linear")
# Set transparency value at 0
cmds.setAttr (material + ".transparency", 0,0,0, type = "double3")
def doKeyTransparencyForMaterials (startTime, endTime):
'''
Main function
Call functions: 'getShaderFrom', 'keyTransparency'
'''
if ((not isinstance(startTime, int)) or (not isinstance(endTime, int))):
cmds.error ("You must provide start and end frame numbers (int).")
# List selected objects
selection = cmds.ls (selection = 1)
if len(selection) == 0:
cmds.error ("Nothing is selected.")
# List all materials
for obj in selection:
materials = getShaderFrom (obj)
# Key transparency
for mat in materials:
keyTransparency (startTime, endTime, mat)
print "Transparency has been successfully keyed for: " + (", ".join(materials)) + "\n",
doKeyTransparencyForMaterials (1, 10)
This is what I did.
import maya.cmds as cmds
######### Delete all existing Shader ##############
cmds.delete (cmds.ls(type='shadingDependNode'))
cmds.delete (cmds.ls(type='shadingEngine'))
######### Delete all existing Shader ##############
######### Delete all Keyframes between 0 and 20 ##############
cmds.setKeyframe( 'lambert1', t='1', at='transparency' )
cmds.cutKey ( t=(0,20) )
######### Delete all Keyframes ##############
#create Blau shader
Blau = cmds.shadingNode('phong', asShader=True, n='Blau', )
cmds.setAttr(Blau + '.color', 0.163,0.284,0.5)
#create KeyFrame Transparency ###Blau####
cmds.setKeyframe( 'Blau', t='1', at='transparency' )
cmds.setAttr ( 'Blau.transparency', 1,1,1, type = "double3")
cmds.setKeyframe( 'Blau', t='15', at='transparency' )
######### update Time to 2 and back to 0 ##############
cmds.currentTime ( 2, update=True )
cmds.currentTime ( 0, update=True )
######### update Time to 2 and back to 0 ##############
The setKeyFrame for lambert1 line is just to make the script running if no Key exists before.
I adjust the current time because your material is shown transparent if you apply it the first time.
PS: Blau is Blue in German
Im writing a custom reader node for maya (in python with openmaya API 2.0) and I would like to send my uv sets to a regular maya mesh node.
Im wondering what would be the best way to push the uv sets in a mesh node? I wasn't able to find what data I have to create and how to send them to the mesh node.
My reader is a OpenMaya.MPxNode who push custom data to a OpenMaya.MPxSurfaceShape. The shape is linked by out mesh / in mesh plugs to a regular maya mesh. I attempt to fill the uvSet plug of this shape using compute but without success. I expect the UVSets to be sent to the mesh.
The following code sample is a limited test where my only goal is to create a new UVSet and attach it to the mesh.
Any ideas or documentation which could help?
I tried several things but I always get the error below.
The error:
// Error: (kFailure): Object does not exist
# Traceback (most recent call last):
# File "/path/to/maya/plug-ins/test_create_uv_set.py", line 60, in compute
# mesh.createUVSet("toto")
# RuntimeError: (kFailure): Object does not exist //
The running code:
"""
usage
import maya.cmds as cmds
import pymel.core as pm
cmds.loadPlugin("/path/to/maya/plug-ins/test_create_uv_set.py")
transform_node = pm.polySphere(n='transform1', ch=1, o=1, r=4)[0]
mesh1_node = transform_node.getShape()
pm.setAttr(mesh1_node + ".visibility", False)
uv_set_mod_node = pm.createNode("uvSetModifier", name="uvsetmodifier1")
mesh2_node = pm.createNode("mesh", name="mesh2", parent=transform_node)
pm.hyperShade(assign="initialShadingGroup")
pm.Attribute.connect(mesh1_node.attr("outMesh"), uv_set_mod_node.attr("inMesh"))
pm.Attribute.connect(uv_set_mod_node.attr("outMesh"), mesh2_node.attr("inMesh"))
"""
import sys
import maya.api.OpenMaya as OpenMaya
def maya_useNewAPI():
pass
class uvSetModifier(OpenMaya.MPxNode):
typeName = "uvSetModifier"
id = OpenMaya.MTypeId(0xCCCCC)
inMesh = None
outMesh = None
#staticmethod
def creator():
return uvSetModifier()
#staticmethod
def initialize():
typedAttr = OpenMaya.MFnTypedAttribute()
uvSetModifier.inMesh = typedAttr.create("inMesh", "im", OpenMaya.MFnData.kMesh)
typedAttr.writable = True
OpenMaya.MPxNode.addAttribute(uvSetModifier.inMesh)
uvSetModifier.outMesh = typedAttr.create("outMesh", "om", OpenMaya.MFnData.kMesh)
typedAttr.writable = True
OpenMaya.MPxNode.addAttribute(uvSetModifier.outMesh)
def __init__(self):
OpenMaya.MPxNode.__init__(self)
def compute(self, plug, datablock):
if plug == uvSetModifier.outMesh:
inputData = datablock.inputValue(uvSetModifier.inMesh)
outputData = datablock.outputValue(uvSetModifier.outMesh)
outputData.setMObject(inputData.asMesh())
mesh = OpenMaya.MFnMesh(inputData.asMesh())
mesh.createUVSet("toto")
datablock.setClean(plug)
def initializePlugin(obj):
plugin = OpenMaya.MFnPlugin(obj, "Autodesk", "3.0", "Any")
try:
plugin.registerNode(uvSetModifier.typeName, uvSetModifier.id, uvSetModifier.creator, uvSetModifier.initialize)
except:
sys.stderr.write("Failed to register node\n")
raise
def uninitializePlugin(obj):
plugin = OpenMaya.MFnPlugin(obj)
try:
plugin.deregisterNode(uvSetModifier.id)
except:
sys.stderr.write("Failed to deregister node\n")
pass
UPDATE 1: THEODOX input
I added the following line (59)
if inputData.asMesh() is not None:
print "test"
mesh = OpenMaya.MFnMesh(inputData.asMesh())
mesh.createUVSet("toto")
results: I still get the same error message
I'm trying to color some specific part of the text, i have tried to say:
if word.strip().startswith(":"):
self.setAttributesForRange(NSColor.greenColor(), None, highlightOffset, len(word))
When someone types the sign : it gets colored green. That is good, but it keeps coloring the word after it like this:
:Hello Hello :Hello <---- this all gets colored green, but I want something like:
:Hello Hello :Hello <---- where everything get colored except the middle "hello" because it doesn't start with the sign : , please help me out
from Foundation import *
from AppKit import *
import objc
class PyObjC_HighlightAppDelegate(NSObject):
# The connection to our NSTextView in the UI
highlightedText = objc.IBOutlet()
# Default font size to use when highlighting
fontSize = 12
def applicationDidFinishLaunching_(self, sender):
NSLog("Application did finish launching.")
def textDidChange_(self, notification):
"""
Delegate method called by the NSTextView whenever the contents of the
text view have changed. This is called after the text has changed and
been committed to the view. See the Cocoa reference documents:
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html
Specifically the sections on Delegate Methods for information on additional
delegate methods relating to text control is NSTextView objects.
"""
# Retrieve the current contents of the document and start highlighting
content = self.highlightedText.string()
self.highlightText(content)
def setAttributesForRange(self, color, font, rangeStart, rangeLength):
"""
Set the visual attributes for a range of characters in the NSTextView. If
values for the color and font are None, defaults will be used.
The rangeStart is an index into the contents of the NSTextView, and
rangeLength is used in combination with this index to create an NSRange
structure, which is passed to the NSTextView methods for setting
text attributes. If either of these values are None, defaults will
be provided.
The "font" parameter is used as an key for the "fontMap", which contains
the associated NSFont objects for each font style.
"""
fontMap = {
"normal" : NSFont.systemFontOfSize_(self.fontSize),
"bold" : NSFont.boldSystemFontOfSize_(self.fontSize)
}
# Setup sane defaults for the color, font and range if no values
# are provided
if color is None:
color = NSColor.blackColor()
if font is None:
font = "normal"
if font not in fontMap:
font = "normal"
displayFont = fontMap[font]
if rangeStart is None:
rangeStart = 0
if rangeLength is None:
rangeLength = len(self.highlightedText.string()) - rangeStart
# Set the attributes for the specified character range
range = NSRange(rangeStart, rangeLength)
self.highlightedText.setTextColor_range_(color, range)
self.highlightedText.setFont_range_(displayFont, range)
def highlightText(self, content):
"""
Apply our customized highlighting to the provided content. It is assumed that
this content was extracted from the NSTextView.
"""
# Calling the setAttributesForRange with no values creates
# a default that "resets" the formatting on all of the content
self.setAttributesForRange(None, None, None, None)
# We'll highlight the content by breaking it down into lines, and
# processing each line one by one. By storing how many characters
# have been processed we can maintain an "offset" into the overall
# content that we use to specify the range of text that is currently
# being highlighted.
contentLines = content.split("\n")
highlightOffset = 0
for line in contentLines:
if line.strip().startswith("#"):
# Comment - we want to highlight the whole comment line
self.setAttributesForRange(NSColor.greenColor(), None, highlightOffset, len(line))
elif line.find(":") > -1:
# Tag - we only want to highlight the tag, not the colon or the remainder of the line
startOfLine = line[0: line.find(":")]
yamlTag = startOfLine.strip("\t ")
yamlTagStart = line.find(yamlTag)
self.setAttributesForRange(NSColor.blueColor(), "bold", highlightOffset + yamlTagStart, len(yamlTag))
elif line.strip().startswith("-"):
# List item - we only want to highlight the dash
listIndex = line.find("-")
self.setAttributesForRange(NSColor.redColor(), None, highlightOffset + listIndex, 1)
# Add the processed line to our offset, as well as the newline that terminated the line
highlightOffset += len(line) + 1
It all depends on what word is.
In [6]: word = ':Hello Hello :Hello'
In [7]: word.strip().startswith(':')
Out[7]: True
In [8]: len(word)
Out[8]: 19
Compare:
In [1]: line = ':Hello Hello :Hello'.split()
In [2]: line
Out[2]: [':Hello', 'Hello', ':Hello']
In [3]: for word in line:
print word.strip().startswith(':')
print len(word)
...:
True
6
False
5
True
6
Notice the difference in len(word), which I suspect is causing your problem.