Multiplatform dynamic font path

Written by Victor Perez on .

Font knobs compatible with Win, Mac and Linux... at the same time

Every time we create a group or a gizmo, or even when we simply save our script that uses a Text node, we know that the font path inside the Text node remains baked, fixed. That is not a problem if you are keeping that tool for yourself in your own computer. The problem rises when you open that script or gizmo in another operative system, a red error appears on top of your Viewer informing you that the font path of that font knob inside the Text node is wrong, so you have to browse for the right font path, very intuitive search under Windows or Mac, not so much in Linux, unless you are smart enough to get the right font path in a txt file to simply copy and paste the path.

In a community like Nukepedia, where you could reach users at all levels of experience, some more interested in the TD side than others, I consider a feature to set automatically the font knob automatically depending on the OS the script/gizmo is open is a good smart feature to avoid the problem listed above. That is why I wrote this Python script you can include in any group/gizmo that contains a Text node or simply to been placed in your menu.py to dynamically change the fonts on every script you load (advance).

Here you are the recipe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
nuke.thisNode().knob('onCreate').setValue('''
import sys
 
########## Customisations ##########
 
# Name of the knob you like to set
fontKnob = 'font'
 
# 'Arial' or 'ArialBold'
fontType = 'Arial'
 
####################################
 
 
 
### Font paths
 
ArialMac = '/Library/Fonts/Arial.ttf'
ArialBoldMac = '/Library/Fonts/Arial Bold.ttf'
ArialWin = 'C:/Windows/Fonts/arial.ttf'
ArialBoldWin = 'C:/Windows/Fonts/arialbd.ttf'
ArialLinux = '/usr/share/fonts/default/Type1/n019003l.pfb'
ArialBoldLinux = '/usr/share/fonts/default/Type1/n019004l.pfb'
 
fontPaths = [ArialMac, ArialBoldMac, ArialWin, ArialBoldWin, ArialLinux, ArialBoldLinux]
 
### Collect information about your OS and set the right path for every font
 
if sys.platform == 'darwin':
    OS = 'Mac'
    ArialPath = ArialMac
    ArialBoldPath = ArialBoldMac
elif sys.platform == 'win32':
    OS = 'Windows'
    ArialPath = ArialWin
    ArialBoldPath = ArialBoldWin
else:
    OS = 'Linux'
    ArialPath = ArialLinux
    ArialBoldPath = ArialBoldLinux
 
 
### OS and font path check
 
#print 'Current OS: '+OS
#print 'Arial: '+ArialPath
#print 'Arial Bold: '+ArialBoldPath
 
 
### Functions
 
def setArial():
    nuke.thisNode().knob(fontKnob).setValue(ArialPath)
def setArialBold():
    nuke.thisNode().knob(fontKnob).setValue(ArialBoldPath)
 
 
### Application
 
currentFontKnob = nuke.thisNode().knob(fontKnob).value()
 
if currentFontKnob in fontPaths or currentFontKnob is '':
    if fontType == 'Arial':
        setArial()
    elif fontType == 'ArialBold':
        setArialBold()
    else:
        nuke.message('Please check "fontType" variable')
else:
    pass
 
''')

 

How to include this code in your gizmos/groups:

1. Once your finish to build the group create a 'Python Custom' knob (Manage User Knobs / Add / Python Custom...).

2. After entered a Name and a Label for this new knob -you can name and label it as you like- just copy and paste the reciepe above in the Script field.

3. Ensure to customise this area of the reciep to match your font knob in case you change the Name when you placed this knob on the main group:

1
2
3
4
5
6
7
8
9
########## Customisations ##########
 
# Name of the knob you like to set
fontKnob = 'font'
 
# 'Arial' or 'ArialBold'
fontType = 'Arial'
 
####################################

* In this reciepe you have 2 choices: Arial and Arial Bold. But you can customise this if you are handy with python.

That's it really. Test it and let me know.

Comments   

 
-3 # Sean Brice 2012-08-02 15:18
As an alternate solution, you can use the nuke.defaultFon tPathname() function (wrapped in TCL) in the font knob, like so:

[python nuke.defaultFon tPathname()]
 
 
-4 # Philippe HUBERDEAU 2012-12-16 03:09
Hi,
Few weeks ago, i faced the same issue concerning multiplatform handling of Text tool. I found the defaultFontPath name() method, but unfortunately, for some reason, it didn't work on all my testing machines. If i recall well, it was on a Mac.
So to make sure it works 100%, as a workaround, i create a dummy Text Node, get the fontPath et delete it !
cheers,
philhub
 

You have no rights to post comments

We have 5320 guests and 117 members online