Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

API access to list of available supported fonts

21 REPLIES 21
SOLVED
Reply
Message 1 of 22
balunist
9622 Views, 21 Replies

API access to list of available supported fonts

balunist
Advocate
Advocate

I'm looking for a way to access the font list presented in Edit Text.  This list contains the available fonts for the current system (installed OS) that Fusion can handle.   I'm planning on adding a similar dropDown menu to my command.

Thanks in advance!

2 Likes

API access to list of available supported fonts

I'm looking for a way to access the font list presented in Edit Text.  This list contains the available fonts for the current system (installed OS) that Fusion can handle.   I'm planning on adding a similar dropDown menu to my command.

Thanks in advance!

Tags (3)
21 REPLIES 21
Message 2 of 22
kandennti
in reply to: balunist

kandennti
Mentor
Mentor

Hi balunist.

I tried.(windows)
I thought it was correct with the OS font, but most failed.

import adsk.core, adsk.fusion, traceback
import re, os

def run(context):
    ui = None
    app = adsk.core.Application.get()
    ui = app.userInterface
    doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    design = app.activeProduct
    rootComp = design.rootComponent
    
    sketches = rootComp.sketches
    xyPlane = rootComp.xYConstructionPlane
    sketch = sketches.add(xyPlane)
    sketchTexts = sketch.sketchTexts
    
    rp = re.compile(".*ttf")
    fontsPath = r"C:/Windows/Fonts" #win only
    font_list = [fnt for fnt in os.listdir(fontsPath) if rp.match(fnt)]       
    
    for idx, font_name in enumerate(font_list):
        point = adsk.core.Point3D.create(0.0, 2.0 * idx, 0.0)
        sketchTextInput = sketchTexts.createInput(font_name, 1.0, point)
        txt = sketchTexts.add(sketchTextInput)
        fn, ext = os.path.splitext(font_name)
        try:
            txt.fontName = fn
            print('OK:{}'.format(font_name))
        except:
            print('NG:{}'.format(font_name))
1 Like

Hi balunist.

I tried.(windows)
I thought it was correct with the OS font, but most failed.

import adsk.core, adsk.fusion, traceback
import re, os

def run(context):
    ui = None
    app = adsk.core.Application.get()
    ui = app.userInterface
    doc = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)
    design = app.activeProduct
    rootComp = design.rootComponent
    
    sketches = rootComp.sketches
    xyPlane = rootComp.xYConstructionPlane
    sketch = sketches.add(xyPlane)
    sketchTexts = sketch.sketchTexts
    
    rp = re.compile(".*ttf")
    fontsPath = r"C:/Windows/Fonts" #win only
    font_list = [fnt for fnt in os.listdir(fontsPath) if rp.match(fnt)]       
    
    for idx, font_name in enumerate(font_list):
        point = adsk.core.Point3D.create(0.0, 2.0 * idx, 0.0)
        sketchTextInput = sketchTexts.createInput(font_name, 1.0, point)
        txt = sketchTexts.add(sketchTextInput)
        fn, ext = os.path.splitext(font_name)
        try:
            txt.fontName = fn
            print('OK:{}'.format(font_name))
        except:
            print('NG:{}'.format(font_name))
Message 3 of 22
JeromeBriot
in reply to: kandennti

JeromeBriot
Mentor
Mentor

Hello,

 

Here is a code that lists system fonts for Mac and Windows:

 

#Author-
#Description-

import adsk.core, adsk.fusion, traceback

import os
import sys

fontToolPath = os.path.dirname(os.path.realpath(__file__))
fontToolPath = fontToolPath + "/FontTools"
if not fontToolPath in sys.path:
    sys.path.append(fontToolPath)
from fontTools import ttLib

######### begin functions for Font ##################
FONT_SPECIFIER_NAME_ID = 4
FONT_SPECIFIER_FAMILY_ID = 1

#short name of a truetype font
#platformID: Windows or Mac
def shortName(font,platformID):
     name = ""
     family = ""
     for record in font['name'].names:
         if record.nameID == FONT_SPECIFIER_NAME_ID and not name and record.platformID==platformID:
             name = record.toUnicode()
         elif record.nameID == FONT_SPECIFIER_FAMILY_ID and not family and record.platformID==platformID:
             family = record.toUnicode()
         if name and family:
            break

     return name, family

# get font list (Windows only in this release)
def getFontList(dic):

     app = adsk.core.Application.get()
     ui = app.userInterface
     if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
            #Windows
            FontPath = os.path.join(os.environ['WINDIR'], 'Fonts')
            PlatFormID = 3
     elif sys.platform.startswith('darwin'):
            #Mac
            FontPath = '/Library/Fonts/'
            PlatFormID = 1
     else:
         if ui:
              ui.messageBox('This is an unknown OS!!')
              return

     #iterate each *.ttf font in the specific folder
     for file in os.listdir(FontPath):
        if file.lower().endswith(".ttf") or file.lower().endswith(".ttc"):
            source_file_name = FontPath+"/"+file
            tt = ttLib.TTFont(source_file_name,fontNumber=0)
            font_ori_name =  shortName(tt,PlatFormID)[1]
            #store this font to fonts map
            if not font_ori_name in dic:
                dic[font_ori_name] = source_file_name

####### end of functions for Font######################

def run(context):

    ui = None

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

        design = app.activeProduct
        rootComp = design.rootComponent

        sketches = rootComp.sketches
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)
        sketchTexts = sketch.sketchTexts

        g_fontDic = {}
        getFontList(g_fontDic)
        newDic = sorted(g_fontDic.keys())

        idx_row = 0
        idx_col = 0

        for key in newDic:

            point = adsk.core.Point3D.create(0.0, 0.0, 0.0)

            sketchTextInput = sketchTexts.createInput(key, 1.0, point)

            txt = sketchTexts.add(sketchTextInput)

            try:

                txt.fontName = key

                point.x = point.x + 10.0*idx_col
                point.y = point.y - 1.0*idx_row
                txt.position = point

                idx_row += 1

                if idx_row > 40:
                    idx_col += 1
                    idx_row = 0

                print(key)

            except:
                txt.deleteMe()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

The code is inspired by this discussion: Surface Text Add-In

You need to download the FontTools subfolder from this repository: https://github.com/AutodeskFusion360/SurfaceText_python and add it in the same location as the Python script above.

 

Here is the font list returned on my PC (Windows 7):

 

Agency FB
Aharoni
Alef
Algerian
Amiri
Amiri Quran
Andalus
Angsana New
AngsanaUPC
Aparajita
Arabic Typesetting
Arial
Arial Black
Arial Narrow
Arial Rounded MT Bold
Arial Unicode MS
Baskerville Old Face
Batang
Bauhaus 93
Bell MT
Berlin Sans FB
Berlin Sans FB Demi
Bernard MT Condensed
Blackadder ITC
Bodoni MT
Bodoni MT Black
Bodoni MT Condensed
Bodoni MT Poster Compressed
Book Antiqua
Bookman Old Style
Bradley Hand ITC
Britannic Bold
Broadway
Browallia New
BrowalliaUPC
Brush Script MT
Caladea
Calibri
Calibri Light
Californian FB
Calisto MT
Cambria
Candara
Carlito
Castellar
Centaur
Century
Century Gothic
Century Schoolbook
Chiller
Colonna MT
Comic Sans MS
Consolas
Constantia
Cooper Black
Copperplate Gothic Bold
Copperplate Gothic Light
Corbel
Cordia New
CordiaUPC
Courier New
Curlz MT
DaunPenh
David
David CLM
David Libre
DejaVu Math TeX Gyre
DejaVu Sans
DejaVu Sans Condensed
DejaVu Sans Light
DejaVu Sans Mono
DejaVu Serif
DejaVu Serif Condensed
DilleniaUPC
DokChampa
Dubai
Dubai Light
Dubai Medium
Ebrima
Edwardian Script ITC
Elephant
EmojiOne Color
Engravers MT
Eras Bold ITC
Eras Demi ITC
Eras Light ITC
Eras Medium ITC
Estrangelo Edessa
EucrosiaUPC
Euphemia
FangSong
Felix Titling
Footlight MT Light
Forte
Frank Ruehl CLM
FrankRuehl
Franklin Gothic Book
Franklin Gothic Demi
Franklin Gothic Demi Cond
Franklin Gothic Heavy
Franklin Gothic Medium
Franklin Gothic Medium Cond
FreesiaUPC
Freestyle Script
French Script MT
Gabriola
Gadugi
Garamond
Gautami
Gentium Basic
Gentium Book Basic
Georgia
Gigi
Gill Sans MT
Gill Sans MT Condensed
Gill Sans MT Ext Condensed Bold
Gill Sans Ultra Bold
Gill Sans Ultra Bold Condensed
Gisha
Gloucester MT Extra Condensed
Goudy Old Style
Goudy Stout
Gulim
Haettenschweiler
Harlow Solid Italic
Harrington
High Tower Text
Impact
Imprint MT Shadow
Informal Roman
IrisUPC
Iskoola Pota
JasmineUPC
Jokerman
Juice ITC
KaiTi
Kalinga
Kartika
Khmer UI
KodchiangUPC
Kokila
Kristen ITC
Kunstler Script
Lao UI
Latha
Leelawadee
Levenim MT
Liberation Mono
Liberation Sans
Liberation Sans Narrow
Liberation Serif
LilyUPC
Linux Biolinum G
Linux Libertine Display G
Linux Libertine G
Lucida Bright
Lucida Calligraphy
Lucida Console
Lucida Fax
Lucida Handwriting
Lucida Sans
Lucida Sans Typewriter
Lucida Sans Unicode
MS Gothic
MS Mincho
MS Reference Sans Serif
MV Boli
Magneto
Maiandra GD
Malgun Gothic
Mangal
Matura MT Script Capitals
Meiryo
Microsoft Himalaya
Microsoft New Tai Lue
Microsoft PhagsPa
Microsoft Sans Serif
Microsoft Tai Le
Microsoft Uighur
Microsoft YaHei
Microsoft Yi Baiti
Miriam
Miriam CLM
Miriam Fixed
Miriam Mono CLM
Mistral
Mongolian Baiti
Monotype Corsiva
MoolBoran
Narkisim
Niagara Engraved
Niagara Solid
Nirmala UI
Noto Kufi Arabic
Noto Mono
Noto Naskh Arabic
Noto Naskh Arabic UI
Noto Sans
Noto Sans Arabic
Noto Sans Arabic UI
Noto Sans Armenian
Noto Sans Cond
Noto Sans Georgian
Noto Sans Hebrew
Noto Sans Lao
Noto Sans Light
Noto Sans Lisu
Noto Serif
Noto Serif Armenian
Noto Serif Cond
Noto Serif Georgian
Noto Serif Hebrew
Noto Serif Lao
Noto Serif Light
Nyala
OCR A Extended
Old English Text MT
Onyx
Open Sans
OpenSymbol
PT Serif
Palace Script MT
Palatino Linotype
Papyrus
Parchment
Perpetua
Perpetua Titling MT
Plantagenet Cherokee
Playbill
Poor Richard
Pristina
Raavi
Rage Italic
Ravie
Rockwell
Rockwell Condensed
Rockwell Extra Bold
Rod
Rubik
Sakkal Majalla
Scheherazade
Script MT Bold
Segoe Print
Segoe Script
Segoe UI
Segoe UI Light
Segoe UI Semibold
Segoe UI Semilight
Segoe UI Symbol
Shonar Bangla
Showcard Gothic
Shruti
SimHei
SimSun
SimSun-ExtB
Simplified Arabic
Simplified Arabic Fixed
Snap ITC
Source Code Pro
Source Code Pro Black
Source Code Pro ExtraLight
Source Code Pro Light
Source Code Pro Medium
Source Code Pro Semibold
Source Sans Pro
Source Sans Pro Black
Source Sans Pro ExtraLight
Source Sans Pro Light
Source Sans Pro Semibold
SpaceClaim ASME CB
SpaceClaim ISO CB
Stencil
Sylfaen
Tahoma
Tempus Sans ITC
Times New Roman
Traditional Arabic
Trebuchet MS
Tunga
Tw Cen MT
Tw Cen MT Condensed
Tw Cen MT Condensed Extra Bold
Utsaah
Vani
Verdana
Vijaya
Viner Hand ITC
Vivaldi
Vladimir Script
Vrinda
Wide Latin

fusion-360-font-windows.png

 

And the list on my Mac (macOS Mojave 10.14.5):

 

Andale mono
Apple chancery
Apple symbols
Applegothic
Applemyungjo
Arial
Arial black
Arial narrow
Ayuthaya
Bodoni ornaments
Chalkduster
Comic sans ms
Courier new
Diwan thuluth
Farisi
Georgia
Herculanum
Impact
Khmer sangam mn
Kokonor
Krungthep
Lao sangam mn
Luminari
Microsoft sans serif
Mishafi
Mishafi gold
Sathu
Silom
Skia
Symbol
Tahoma
Times new roman
Trattatello
Trebuchet ms
Verdana
Zapfino

fusion-360-font-mac.png

 

 

Both lists on Windows and Mac are not complete. The code searches only for system fonts. Some additional fonts are shipped with Fusion 360 and some are located on user specific folder (see List of installed fonts OS X / C)

 

1 Like

Hello,

 

Here is a code that lists system fonts for Mac and Windows:

 

#Author-
#Description-

import adsk.core, adsk.fusion, traceback

import os
import sys

fontToolPath = os.path.dirname(os.path.realpath(__file__))
fontToolPath = fontToolPath + "/FontTools"
if not fontToolPath in sys.path:
    sys.path.append(fontToolPath)
from fontTools import ttLib

######### begin functions for Font ##################
FONT_SPECIFIER_NAME_ID = 4
FONT_SPECIFIER_FAMILY_ID = 1

#short name of a truetype font
#platformID: Windows or Mac
def shortName(font,platformID):
     name = ""
     family = ""
     for record in font['name'].names:
         if record.nameID == FONT_SPECIFIER_NAME_ID and not name and record.platformID==platformID:
             name = record.toUnicode()
         elif record.nameID == FONT_SPECIFIER_FAMILY_ID and not family and record.platformID==platformID:
             family = record.toUnicode()
         if name and family:
            break

     return name, family

# get font list (Windows only in this release)
def getFontList(dic):

     app = adsk.core.Application.get()
     ui = app.userInterface
     if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
            #Windows
            FontPath = os.path.join(os.environ['WINDIR'], 'Fonts')
            PlatFormID = 3
     elif sys.platform.startswith('darwin'):
            #Mac
            FontPath = '/Library/Fonts/'
            PlatFormID = 1
     else:
         if ui:
              ui.messageBox('This is an unknown OS!!')
              return

     #iterate each *.ttf font in the specific folder
     for file in os.listdir(FontPath):
        if file.lower().endswith(".ttf") or file.lower().endswith(".ttc"):
            source_file_name = FontPath+"/"+file
            tt = ttLib.TTFont(source_file_name,fontNumber=0)
            font_ori_name =  shortName(tt,PlatFormID)[1]
            #store this font to fonts map
            if not font_ori_name in dic:
                dic[font_ori_name] = source_file_name

####### end of functions for Font######################

def run(context):

    ui = None

    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType)

        design = app.activeProduct
        rootComp = design.rootComponent

        sketches = rootComp.sketches
        xyPlane = rootComp.xYConstructionPlane
        sketch = sketches.add(xyPlane)
        sketchTexts = sketch.sketchTexts

        g_fontDic = {}
        getFontList(g_fontDic)
        newDic = sorted(g_fontDic.keys())

        idx_row = 0
        idx_col = 0

        for key in newDic:

            point = adsk.core.Point3D.create(0.0, 0.0, 0.0)

            sketchTextInput = sketchTexts.createInput(key, 1.0, point)

            txt = sketchTexts.add(sketchTextInput)

            try:

                txt.fontName = key

                point.x = point.x + 10.0*idx_col
                point.y = point.y - 1.0*idx_row
                txt.position = point

                idx_row += 1

                if idx_row > 40:
                    idx_col += 1
                    idx_row = 0

                print(key)

            except:
                txt.deleteMe()

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

 

The code is inspired by this discussion: Surface Text Add-In

You need to download the FontTools subfolder from this repository: https://github.com/AutodeskFusion360/SurfaceText_python and add it in the same location as the Python script above.

 

Here is the font list returned on my PC (Windows 7):

 

Agency FB
Aharoni
Alef
Algerian
Amiri
Amiri Quran
Andalus
Angsana New
AngsanaUPC
Aparajita
Arabic Typesetting
Arial
Arial Black
Arial Narrow
Arial Rounded MT Bold
Arial Unicode MS
Baskerville Old Face
Batang
Bauhaus 93
Bell MT
Berlin Sans FB
Berlin Sans FB Demi
Bernard MT Condensed
Blackadder ITC
Bodoni MT
Bodoni MT Black
Bodoni MT Condensed
Bodoni MT Poster Compressed
Book Antiqua
Bookman Old Style
Bradley Hand ITC
Britannic Bold
Broadway
Browallia New
BrowalliaUPC
Brush Script MT
Caladea
Calibri
Calibri Light
Californian FB
Calisto MT
Cambria
Candara
Carlito
Castellar
Centaur
Century
Century Gothic
Century Schoolbook
Chiller
Colonna MT
Comic Sans MS
Consolas
Constantia
Cooper Black
Copperplate Gothic Bold
Copperplate Gothic Light
Corbel
Cordia New
CordiaUPC
Courier New
Curlz MT
DaunPenh
David
David CLM
David Libre
DejaVu Math TeX Gyre
DejaVu Sans
DejaVu Sans Condensed
DejaVu Sans Light
DejaVu Sans Mono
DejaVu Serif
DejaVu Serif Condensed
DilleniaUPC
DokChampa
Dubai
Dubai Light
Dubai Medium
Ebrima
Edwardian Script ITC
Elephant
EmojiOne Color
Engravers MT
Eras Bold ITC
Eras Demi ITC
Eras Light ITC
Eras Medium ITC
Estrangelo Edessa
EucrosiaUPC
Euphemia
FangSong
Felix Titling
Footlight MT Light
Forte
Frank Ruehl CLM
FrankRuehl
Franklin Gothic Book
Franklin Gothic Demi
Franklin Gothic Demi Cond
Franklin Gothic Heavy
Franklin Gothic Medium
Franklin Gothic Medium Cond
FreesiaUPC
Freestyle Script
French Script MT
Gabriola
Gadugi
Garamond
Gautami
Gentium Basic
Gentium Book Basic
Georgia
Gigi
Gill Sans MT
Gill Sans MT Condensed
Gill Sans MT Ext Condensed Bold
Gill Sans Ultra Bold
Gill Sans Ultra Bold Condensed
Gisha
Gloucester MT Extra Condensed
Goudy Old Style
Goudy Stout
Gulim
Haettenschweiler
Harlow Solid Italic
Harrington
High Tower Text
Impact
Imprint MT Shadow
Informal Roman
IrisUPC
Iskoola Pota
JasmineUPC
Jokerman
Juice ITC
KaiTi
Kalinga
Kartika
Khmer UI
KodchiangUPC
Kokila
Kristen ITC
Kunstler Script
Lao UI
Latha
Leelawadee
Levenim MT
Liberation Mono
Liberation Sans
Liberation Sans Narrow
Liberation Serif
LilyUPC
Linux Biolinum G
Linux Libertine Display G
Linux Libertine G
Lucida Bright
Lucida Calligraphy
Lucida Console
Lucida Fax
Lucida Handwriting
Lucida Sans
Lucida Sans Typewriter
Lucida Sans Unicode
MS Gothic
MS Mincho
MS Reference Sans Serif
MV Boli
Magneto
Maiandra GD
Malgun Gothic
Mangal
Matura MT Script Capitals
Meiryo
Microsoft Himalaya
Microsoft New Tai Lue
Microsoft PhagsPa
Microsoft Sans Serif
Microsoft Tai Le
Microsoft Uighur
Microsoft YaHei
Microsoft Yi Baiti
Miriam
Miriam CLM
Miriam Fixed
Miriam Mono CLM
Mistral
Mongolian Baiti
Monotype Corsiva
MoolBoran
Narkisim
Niagara Engraved
Niagara Solid
Nirmala UI
Noto Kufi Arabic
Noto Mono
Noto Naskh Arabic
Noto Naskh Arabic UI
Noto Sans
Noto Sans Arabic
Noto Sans Arabic UI
Noto Sans Armenian
Noto Sans Cond
Noto Sans Georgian
Noto Sans Hebrew
Noto Sans Lao
Noto Sans Light
Noto Sans Lisu
Noto Serif
Noto Serif Armenian
Noto Serif Cond
Noto Serif Georgian
Noto Serif Hebrew
Noto Serif Lao
Noto Serif Light
Nyala
OCR A Extended
Old English Text MT
Onyx
Open Sans
OpenSymbol
PT Serif
Palace Script MT
Palatino Linotype
Papyrus
Parchment
Perpetua
Perpetua Titling MT
Plantagenet Cherokee
Playbill
Poor Richard
Pristina
Raavi
Rage Italic
Ravie
Rockwell
Rockwell Condensed
Rockwell Extra Bold
Rod
Rubik
Sakkal Majalla
Scheherazade
Script MT Bold
Segoe Print
Segoe Script
Segoe UI
Segoe UI Light
Segoe UI Semibold
Segoe UI Semilight
Segoe UI Symbol
Shonar Bangla
Showcard Gothic
Shruti
SimHei
SimSun
SimSun-ExtB
Simplified Arabic
Simplified Arabic Fixed
Snap ITC
Source Code Pro
Source Code Pro Black
Source Code Pro ExtraLight
Source Code Pro Light
Source Code Pro Medium
Source Code Pro Semibold
Source Sans Pro
Source Sans Pro Black
Source Sans Pro ExtraLight
Source Sans Pro Light
Source Sans Pro Semibold
SpaceClaim ASME CB
SpaceClaim ISO CB
Stencil
Sylfaen
Tahoma
Tempus Sans ITC
Times New Roman
Traditional Arabic
Trebuchet MS
Tunga
Tw Cen MT
Tw Cen MT Condensed
Tw Cen MT Condensed Extra Bold
Utsaah
Vani
Verdana
Vijaya
Viner Hand ITC
Vivaldi
Vladimir Script
Vrinda
Wide Latin

fusion-360-font-windows.png

 

And the list on my Mac (macOS Mojave 10.14.5):

 

Andale mono
Apple chancery
Apple symbols
Applegothic
Applemyungjo
Arial
Arial black
Arial narrow
Ayuthaya
Bodoni ornaments
Chalkduster
Comic sans ms
Courier new
Diwan thuluth
Farisi
Georgia
Herculanum
Impact
Khmer sangam mn
Kokonor
Krungthep
Lao sangam mn
Luminari
Microsoft sans serif
Mishafi
Mishafi gold
Sathu
Silom
Skia
Symbol
Tahoma
Times new roman
Trattatello
Trebuchet ms
Verdana
Zapfino

fusion-360-font-mac.png

 

 

Both lists on Windows and Mac are not complete. The code searches only for system fonts. Some additional fonts are shipped with Fusion 360 and some are located on user specific folder (see List of installed fonts OS X / C)

 

Tags (4)
Message 4 of 22
balunist
in reply to: JeromeBriot

balunist
Advocate
Advocate

Jerome,

Thanks you.   This approaches the end result I was looking for but as you pointed out it is missing fonts that Fusion has added.  Also it is possible that they have omitted some fonts that have caused issues as I had found somewhere in the forums. 

My hopes were that someone might have figured out how to tap into Fusion to extract their dynamically generated font list which they support without all the overhead.   Adding font selection is a nice-to-have feature for my addin but not worth adding the package, etc for a less then perfect result.  It seems like an addition to the API is in order.   Don't you think?    Thanks again!   Icarus 

0 Likes

Jerome,

Thanks you.   This approaches the end result I was looking for but as you pointed out it is missing fonts that Fusion has added.  Also it is possible that they have omitted some fonts that have caused issues as I had found somewhere in the forums. 

My hopes were that someone might have figured out how to tap into Fusion to extract their dynamically generated font list which they support without all the overhead.   Adding font selection is a nice-to-have feature for my addin but not worth adding the package, etc for a less then perfect result.  It seems like an addition to the API is in order.   Don't you think?    Thanks again!   Icarus 

Message 5 of 22
BrianEkins
in reply to: balunist

BrianEkins
Mentor
Mentor

The API doesn't provide access to any internal list of fonts.  However, the API does provide access to the user interface and there is a command in the drawing workspace that builds a list of fonts to allow you to pick the default font.  Because that list is constructed as part of the UI and not just embedded within a command, we can access that list through the API.  Here's a simple script that demonstrates how to do it.

 

def run(context):
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Get the "Annotation Font" command that exists in the NavBar in the Drawing workspace.
        fontCmdDef = ui.commandDefinitions.itemById('FusionDrawingAnnotationFontCmd')
        
        # Get the list control associated with the command.
        listCntrl = adsk.core.ListControlDefinition.cast(fontCmdDef.controlDefinition)
        
        # Iterate through and print the names of the fonts.
        listItem = adsk.core.ListItem.cast(None)
        for listItem in listCntrl.listItems:
            print(listItem.name)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

The API doesn't provide access to any internal list of fonts.  However, the API does provide access to the user interface and there is a command in the drawing workspace that builds a list of fonts to allow you to pick the default font.  Because that list is constructed as part of the UI and not just embedded within a command, we can access that list through the API.  Here's a simple script that demonstrates how to do it.

 

def run(context):
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Get the "Annotation Font" command that exists in the NavBar in the Drawing workspace.
        fontCmdDef = ui.commandDefinitions.itemById('FusionDrawingAnnotationFontCmd')
        
        # Get the list control associated with the command.
        listCntrl = adsk.core.ListControlDefinition.cast(fontCmdDef.controlDefinition)
        
        # Iterate through and print the names of the fonts.
        listItem = adsk.core.ListItem.cast(None)
        for listItem in listCntrl.listItems:
            print(listItem.name)
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 6 of 22
balunist
in reply to: BrianEkins

balunist
Advocate
Advocate

Brian,

When made to work this will be a home run.   OS independent,  provides Fusion compatible fonts now and going forward,  and simple.    One small problem I need help on, the cast throws the following exception.

 

>>> Failed:
Traceback (most recent call last):
File "/Users/Shared/Google Drive/MapBoards/Submodules/Samples/getfonts.py", line 18, in run
listCntrl = adsk.core.ListControlDefinition.cast(fontCmdDef.controlDefinition)
File "/Users/icarus/Library/Application Support/Autodesk/webdeploy/production/10507a1a00e75a1d1f4d0552cc264036b1151c7b/Autodesk Fusion 360.app/Contents/Api/Python/packages/adsk/core.py", line 3666, in _get_controlDefinition
return _core.CommandDefinition__get_controlDefinition(self)
RuntimeError: 2 : InternalValidationError : inputControl

0 Likes

Brian,

When made to work this will be a home run.   OS independent,  provides Fusion compatible fonts now and going forward,  and simple.    One small problem I need help on, the cast throws the following exception.

 

>>> Failed:
Traceback (most recent call last):
File "/Users/Shared/Google Drive/MapBoards/Submodules/Samples/getfonts.py", line 18, in run
listCntrl = adsk.core.ListControlDefinition.cast(fontCmdDef.controlDefinition)
File "/Users/icarus/Library/Application Support/Autodesk/webdeploy/production/10507a1a00e75a1d1f4d0552cc264036b1151c7b/Autodesk Fusion 360.app/Contents/Api/Python/packages/adsk/core.py", line 3666, in _get_controlDefinition
return _core.CommandDefinition__get_controlDefinition(self)
RuntimeError: 2 : InternalValidationError : inputControl

Message 7 of 22
balunist
in reply to: balunist

balunist
Advocate
Advocate

I dumped the user interface to file and this entry showed the following:

 

Command Control____
Control ID: FusionDrawingAnnotationFontCmd
isVisible: True
index: 4
Command Definition____
Unable to get the associated CommandDefinition.

0 Likes

I dumped the user interface to file and this entry showed the following:

 

Command Control____
Control ID: FusionDrawingAnnotationFontCmd
isVisible: True
index: 4
Command Definition____
Unable to get the associated CommandDefinition.

Message 8 of 22
BrianEkins
in reply to: balunist

BrianEkins
Mentor
Mentor
Accepted solution

I tested the program and it works but I had opened a drawing to see what command I was accessing the list from.  It turns out that control isn't created until a drawing has been open.  As long as any drawing has been open in the current session of Fusion, the control and the list of fonts will be available.  Otherwise, you'll get the error you're seeing.

 

I tried to figure out a way to somehow initialize the drawing workspace but without any luck.  There's currently absolutely no API access to the drawing functionality.

 

Sorry about what looks like a dead end.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes

I tested the program and it works but I had opened a drawing to see what command I was accessing the list from.  It turns out that control isn't created until a drawing has been open.  As long as any drawing has been open in the current session of Fusion, the control and the list of fonts will be available.  Otherwise, you'll get the error you're seeing.

 

I tried to figure out a way to somehow initialize the drawing workspace but without any luck.  There's currently absolutely no API access to the drawing functionality.

 

Sorry about what looks like a dead end.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 9 of 22
balunist
in reply to: BrianEkins

balunist
Advocate
Advocate
Wow, and it works so well. Since it's works as-is when the control is
initialized might there be less resistance to a change in the
initialization order or a way to force a load by the development team.
I'm assuming memory footprint would be the reason it is not currently
loaded.

Thanks!
0 Likes

Wow, and it works so well. Since it's works as-is when the control is
initialized might there be less resistance to a change in the
initialization order or a way to force a load by the development team.
I'm assuming memory footprint would be the reason it is not currently
loaded.

Thanks!
Message 10 of 22
balunist
in reply to: BrianEkins

balunist
Advocate
Advocate

Brian,

I was able to take advantage of the method you provided as an initialization and refresh tool.   

Thanks you for your time in providing this expertise!

Icarus 

0 Likes

Brian,

I was able to take advantage of the method you provided as an initialization and refresh tool.   

Thanks you for your time in providing this expertise!

Icarus 

Message 11 of 22
epeleg
in reply to: BrianEkins

epeleg
Participant
Participant

Hi @BrianEkins 

As of the time of writing I can't seem to be able to get 

fontCmdDef = ui.commandDefinitions.itemById('FusionDrawingAnnotationFontCmd')

to return anything.... 
Any idea if there is any other similar workaround? 

0 Likes

Hi @BrianEkins 

As of the time of writing I can't seem to be able to get 

fontCmdDef = ui.commandDefinitions.itemById('FusionDrawingAnnotationFontCmd')

to return anything.... 
Any idea if there is any other similar workaround? 

Message 12 of 22
kandennti
in reply to: epeleg

kandennti
Mentor
Mentor

Hi @epeleg .

 

Do you want to get the names of fonts available in Draw?

How about something like this?

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import re

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app :adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface

        fontNames = getDrawFonts()
        [print(f) for f in fontNames]

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def getDrawFonts() -> list:
    app :adsk.fusion.Application = adsk.core.Application.get()

    app.executeTextCommand(u'Commands.Start FusionChangeFontCommand')
    txtCmdRes = app.executeTextCommand(u'Toolkit.cmdDialog')
    app.executeTextCommand(u'NuCommands.CancelCmd')

    fontTxt = txtCmdRes.split('FusionDocumentSettingsFont')[1].split('MenuItems:\n')[1].split('FusionDocumentSettingsTextHeightGroup')[0]
    fontDatas = [re.sub(r"^\s+", "", s.split(',')[0]) for s in fontTxt.split('\n')][0:-2]

    return fontDatas
0 Likes

Hi @epeleg .

 

Do you want to get the names of fonts available in Draw?

How about something like this?

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core
import re

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app :adsk.fusion.Application = adsk.core.Application.get()
        ui = app.userInterface

        fontNames = getDrawFonts()
        [print(f) for f in fontNames]

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


def getDrawFonts() -> list:
    app :adsk.fusion.Application = adsk.core.Application.get()

    app.executeTextCommand(u'Commands.Start FusionChangeFontCommand')
    txtCmdRes = app.executeTextCommand(u'Toolkit.cmdDialog')
    app.executeTextCommand(u'NuCommands.CancelCmd')

    fontTxt = txtCmdRes.split('FusionDocumentSettingsFont')[1].split('MenuItems:\n')[1].split('FusionDocumentSettingsTextHeightGroup')[0]
    fontDatas = [re.sub(r"^\s+", "", s.split(',')[0]) for s in fontTxt.split('\n')][0:-2]

    return fontDatas
Message 13 of 22
epeleg
in reply to: kandennti

epeleg
Participant
Participant

@kandennti 
Thank you very much,
This is Very good, and de-facto is enough for me to get what I need to do - done.

However for the sake of learning I have 3 more Q's:

1) I understand that this some how interrogates the structure browser tree when on the Drawing workspace, 
    Can something similar be achieved while being the Design workspace ?

2) Can something similar be done with the fonts dropdown that shows when editing a sketchText entity?

3) Is there any documentation for the stuff going on here:

    app.executeTextCommand(u'Commands.Start FusionChangeFontCommand')
    txtCmdRes = app.executeTextCommand(u'Toolkit.cmdDialog')
    app.executeTextCommand(u'NuCommands.CancelCmd')

And the Q's is Not about "What is going on here" as I do understand it but rather more like "How can I learn what other similar things can be done like this..." ? (searching google for "fusionChangeFontCommand" returns no results at all - I assume in the future it will at least return this page...)

0 Likes

@kandennti 
Thank you very much,
This is Very good, and de-facto is enough for me to get what I need to do - done.

However for the sake of learning I have 3 more Q's:

1) I understand that this some how interrogates the structure browser tree when on the Drawing workspace, 
    Can something similar be achieved while being the Design workspace ?

2) Can something similar be done with the fonts dropdown that shows when editing a sketchText entity?

3) Is there any documentation for the stuff going on here:

    app.executeTextCommand(u'Commands.Start FusionChangeFontCommand')
    txtCmdRes = app.executeTextCommand(u'Toolkit.cmdDialog')
    app.executeTextCommand(u'NuCommands.CancelCmd')

And the Q's is Not about "What is going on here" as I do understand it but rather more like "How can I learn what other similar things can be done like this..." ? (searching google for "fusionChangeFontCommand" returns no results at all - I assume in the future it will at least return this page...)

Message 14 of 22
epeleg
in reply to: epeleg

epeleg
Participant
Participant

@kandennti 


so Thanks to your initial code and doing some more reading here:
https://github.com/kantoku-code/Fusion360_Small_Tools_for_Developers/tree/master/TextCommands

and here:
https://modthemachine.typepad.com/my_weblog/2016/01/run-fusion-commands.html
and in another answer by yourself here:
https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/td-p/9645688


I was able to positively answer the first two of my own additional Q and created the following code based on yours.

def getSketchTextFonts() -> list:
    app :adsk.fusion.Application = adsk.core.Application.get()
    ui = app.userInterface

    #The Commands.Start EditMTextCmd needs to have a Text Element selected
    #so we create a temporary sketch with a 
    design = app.activeProduct
    rootComp = design.rootComponent        
    sketches = rootComp.sketches        
    
    xYPlane = rootComp.xYConstructionPlane
    mySketch = sketches.add(xYPlane)
    texts= mySketch.sketchTexts
    # Add multi-line text.
    input = texts.createInput2('.', 0.5)
    input.setAsMultiLine(adsk.core.Point3D.create(0, 0, 0),
                            adsk.core.Point3D.create(1, 1, 0),
                            adsk.core.HorizontalAlignments.LeftHorizontalAlignment,
                            adsk.core.VerticalAlignments.TopVerticalAlignment, 0)
    temp_text = texts.add(input)
    ui.activeSelections.clear()
    ui.activeSelections.add(temp_text)


    app.executeTextCommand(u'Commands.Start EditMTextCmd')
    txtCmdRes = app.executeTextCommand(u'Toolkit.cmdDialog')
    app.executeTextCommand(u'NuCommands.CancelCmd')
    fontTxt = txtCmdRes.split('TextFont')[1].split('MenuItems:\n')[1].split('TextExStyle')[0]
    fontDatas = [re.sub(r"^\s+", "", s.split(',')[0]) for s in fontTxt.split('\n')][0:-1]

    ui.activeSelections.clear()
    temp_text.deleteMe()
    mySketch.deleteMe()

    #app.log(f'\n--------------\fontDatas = {fontDatas}')
    return fontDatas

 This is a replacement of your getDrawFonts function that works in the Design workspace.


Thank you so very much again...

I am now left with the following Q:
As it says here that "The use of text commands is deprecated by Autodesk." I would like to try to convert this code to not use text commands but use python only API possibly using commandDefs and 

Command.commandInputs as a starting point.
I will post here if I find such a solution.
0 Likes

@kandennti 


so Thanks to your initial code and doing some more reading here:
https://github.com/kantoku-code/Fusion360_Small_Tools_for_Developers/tree/master/TextCommands

and here:
https://modthemachine.typepad.com/my_weblog/2016/01/run-fusion-commands.html
and in another answer by yourself here:
https://forums.autodesk.com/t5/fusion-360-api-and-scripts/use-textcommands/td-p/9645688


I was able to positively answer the first two of my own additional Q and created the following code based on yours.

def getSketchTextFonts() -> list:
    app :adsk.fusion.Application = adsk.core.Application.get()
    ui = app.userInterface

    #The Commands.Start EditMTextCmd needs to have a Text Element selected
    #so we create a temporary sketch with a 
    design = app.activeProduct
    rootComp = design.rootComponent        
    sketches = rootComp.sketches        
    
    xYPlane = rootComp.xYConstructionPlane
    mySketch = sketches.add(xYPlane)
    texts= mySketch.sketchTexts
    # Add multi-line text.
    input = texts.createInput2('.', 0.5)
    input.setAsMultiLine(adsk.core.Point3D.create(0, 0, 0),
                            adsk.core.Point3D.create(1, 1, 0),
                            adsk.core.HorizontalAlignments.LeftHorizontalAlignment,
                            adsk.core.VerticalAlignments.TopVerticalAlignment, 0)
    temp_text = texts.add(input)
    ui.activeSelections.clear()
    ui.activeSelections.add(temp_text)


    app.executeTextCommand(u'Commands.Start EditMTextCmd')
    txtCmdRes = app.executeTextCommand(u'Toolkit.cmdDialog')
    app.executeTextCommand(u'NuCommands.CancelCmd')
    fontTxt = txtCmdRes.split('TextFont')[1].split('MenuItems:\n')[1].split('TextExStyle')[0]
    fontDatas = [re.sub(r"^\s+", "", s.split(',')[0]) for s in fontTxt.split('\n')][0:-1]

    ui.activeSelections.clear()
    temp_text.deleteMe()
    mySketch.deleteMe()

    #app.log(f'\n--------------\fontDatas = {fontDatas}')
    return fontDatas

 This is a replacement of your getDrawFonts function that works in the Design workspace.


Thank you so very much again...

I am now left with the following Q:
As it says here that "The use of text commands is deprecated by Autodesk." I would like to try to convert this code to not use text commands but use python only API possibly using commandDefs and 

Command.commandInputs as a starting point.
I will post here if I find such a solution.
Message 15 of 22
kandennti
in reply to: epeleg

kandennti
Mentor
Mentor

@epeleg .

 

I'm not sure exactly why, but the CommandDefinition.execute method is processed either just before or after the end of the script in a lump.
Therefore, if we use it in the middle of a script, we know that it will not be executed at the desired timing, so the only way is to use a TextCommands.

0 Likes

@epeleg .

 

I'm not sure exactly why, but the CommandDefinition.execute method is processed either just before or after the end of the script in a lump.
Therefore, if we use it in the middle of a script, we know that it will not be executed at the desired timing, so the only way is to use a TextCommands.

Message 16 of 22
MichaelT_123
in reply to: kandennti

MichaelT_123
Advisor
Advisor

Hi Mr Epeleg,

One way to do it is to scan OS font's dictionaries for common and 'legal' font files.
It is essential to realize that the outcome list might contain only some (or excess) of the list presented in the F360 test dialog box, as ADSK uses also its own proprietary font format.
Also, some system legacy fonts on Windows do not exist in (easily accessible) file form.
I am not sure about apple juice candied Unix, although many common sense similarities .... have prevailed.

Please find below some snippets of the code that might alleviate the pain of tackling the issue; however, the deeper you go, the more convoluted/intricate it is. I am attaching the dump of what is inside the belly of the typical font (a lot of s.... for sure) and an abbreviated executive summary (of Rockwell Condensed). Have fun!

 

 

# ONE OF MANY MODULE TO DEAL WITH FONTS  :hourglass_not_done::hourglass_not_done::hourglass_not_done::hourglass_not_done::hourglass_not_done:
import fontTools
from fontTools.ttLib import TTFont

# WHERE TO FIND THE FONTS
fontResourcePathsLst  = [ ['C:\\Windows\\Fonts\\','.ttf'], 
                        ['C:\\Windows\\Fonts\\','.otf'], 
                        ['C:\\Program Files (x86)\\Adobe\\Acrobat DC\\Resource\\Font\\','.otf'], 
                        ['C:\\Program Files (x86)\\Adobe\\Acrobat DC\\Resource\\CIDFont\\','.otf'],
                        ['D:\\???????\\FontAddins\\','.ttf']]

# LIST FONTS VIA OS's FILE SYSTEM
def getFontNamesTable( vDict, fontSystemFiles, fontNamesTable, charSet ):
    fontNamesTable = [['fontFamilyName', 'fontSubFamilyName', 'isItalic', 'isBold', 'lfWeight', 'uniqueFontIdentifier', 'fullFontName', 'fontFilePath', 'fontType', 'licenseType', 'mapTableNames']]

    for fontFilePath in fontSystemFiles:
        ttf                  = TTFont( fontFilePath, 0, allowVID=0, ignoreDecompileErrors=True, fontNumber=-1)  
        nameMap              = ttf['name']
        fontFamilyName       = None
        fontSubFamilyName    = None
        uniqueFontIdentifier = None
        fullFontName         = None
        isItalic             = False 
        isBold               = False
        lfWeight             = 400   
        fontType             = 0    
        for nameRecIdx in range(len(nameMap.names)):
            nameRec = nameMap.names[nameRecIdx]
            if  nameRec.nameID == Constants.FONT_FAMILY_NAME:
                fontFamilyName = str(NameRecord.toUnicode( nameRec, errors='strict'))  
            elif nameRec.nameID == Constants.FONT_COPYRIGHT_NOTICE:
                fontCopyRight =    NameRecord.toUnicode( nameRec, errors='strict') 
                if  fontCopyRight == '':
                    fontCopyRight == 'None'    
                fontCopyRight = fontCopyRight.replace( ',', '|'  )           
            elif nameRec.nameID == Constants.FONT_SUBFAMILY_NAME:
                fontSubFamilyName = NameRecord.toUnicode( nameRec, errors='strict') 
                ( isItalic, isBold, lfWeight ) = getItalicBoldWeight( fontSubFamilyName )
            elif nameRec.nameID == Constants.FONT_UNIQUE_IDENTIFIER:
                uniqueFontIdentifier = NameRecord.toUnicode( nameRec, errors='strict') 
                uniqueFontIdentifier = uniqueFontIdentifier.replace( ',', ' '  )              # Lucida and others has comma char here ????
                uniqueFontIdentifier = uniqueFontIdentifier.replace( u',', u' ') 
            elif nameRec.nameID == Constants.FONT_FULL_NAME:
                fullFontName = NameRecord.toUnicode( nameRec, errors='strict')  
            if fontFamilyName and fontSubFamilyName and uniqueFontIdentifier and fullFontName:
                mapTableNames = str(sorted(ttf.keys()))
                fontNamesTableRec = [fontFamilyName, fontSubFamilyName, isItalic, isBold, lfWeight, uniqueFontIdentifier, fullFontName, fontFilePath, fontCopyRight, mapTableNames]
                fontNamesTable.append( fontNamesTableRec )
                break
    return fontNamesTable    
{
    "HEADER": {
        "CreatedBy": "TextShape",
        "Version": 5.123,
        "Author": "Michael Tomsia, PhD",
        "Content": "Window True Type Font Property Dump",
        "CreationDate": "18 Dec 2021   10:40:14",
        "FontName": "",
        "FontStyle": "NORMAL ANSI",
        "fontFilePath": "",
        "platEncIDSet": "",
        "platformIDSet": "",
        "languageSet": "",
        "glyphExist": "",
        "FontHeight": ""
    },
    "LOGFONT": {
        "lfFaceName": "Rockwell Condensed",
        "lfHeight": 75,
        "lfWidth": 21,
        "lfEscapement": 0,
        "lfOrientation": 0,
        "lfWeight": 400,
        "lfItalic": 0,
        "lfUnderline": 0,
        "lfStrikeOut": 0,
        "lfCharSet": 0,
        "lfOutPrecision": 3,
        "lfClipPrecision": 2,
        "lfPitchAndFamily": 1,
        "fonttypeStr": "TRUETYPE",
        "fonttype": 4
    },
    "TEXTMETRIC": {
        "tmHeight": 75,
        "tmAscent": 60,
        "tmDescent": 15,
        "tmInternalLeading": 11,
        "tmExternalLeading": 0,
        "tmAveCharWidth": 21,
        "tmMaxCharWidth": 74,
        "tmWeight": 400,
        "tmOverhang": 0,
        "tmDigitizedAspectX": 192,
        "tmDigitizedAspectY": 192,
        "tmFirstChar": 32,
        "tmLastChar": 0,
        "tmDefaultChar": 2,
        "tmBreakChar": -5,
        "tmItalic": 31,
        "tmUnderline": 0,
        "tmStruckOut": 32,
        "tmPitchAndFamily": 0,
        "tmCharSet": 0,
        "tmCharSetName": "ANSI"
    }
}

 

Regards
MichaelT

MichaelT
1 Like

Hi Mr Epeleg,

One way to do it is to scan OS font's dictionaries for common and 'legal' font files.
It is essential to realize that the outcome list might contain only some (or excess) of the list presented in the F360 test dialog box, as ADSK uses also its own proprietary font format.
Also, some system legacy fonts on Windows do not exist in (easily accessible) file form.
I am not sure about apple juice candied Unix, although many common sense similarities .... have prevailed.

Please find below some snippets of the code that might alleviate the pain of tackling the issue; however, the deeper you go, the more convoluted/intricate it is. I am attaching the dump of what is inside the belly of the typical font (a lot of s.... for sure) and an abbreviated executive summary (of Rockwell Condensed). Have fun!

 

 

# ONE OF MANY MODULE TO DEAL WITH FONTS  :hourglass_not_done::hourglass_not_done::hourglass_not_done::hourglass_not_done::hourglass_not_done:
import fontTools
from fontTools.ttLib import TTFont

# WHERE TO FIND THE FONTS
fontResourcePathsLst  = [ ['C:\\Windows\\Fonts\\','.ttf'], 
                        ['C:\\Windows\\Fonts\\','.otf'], 
                        ['C:\\Program Files (x86)\\Adobe\\Acrobat DC\\Resource\\Font\\','.otf'], 
                        ['C:\\Program Files (x86)\\Adobe\\Acrobat DC\\Resource\\CIDFont\\','.otf'],
                        ['D:\\???????\\FontAddins\\','.ttf']]

# LIST FONTS VIA OS's FILE SYSTEM
def getFontNamesTable( vDict, fontSystemFiles, fontNamesTable, charSet ):
    fontNamesTable = [['fontFamilyName', 'fontSubFamilyName', 'isItalic', 'isBold', 'lfWeight', 'uniqueFontIdentifier', 'fullFontName', 'fontFilePath', 'fontType', 'licenseType', 'mapTableNames']]

    for fontFilePath in fontSystemFiles:
        ttf                  = TTFont( fontFilePath, 0, allowVID=0, ignoreDecompileErrors=True, fontNumber=-1)  
        nameMap              = ttf['name']
        fontFamilyName       = None
        fontSubFamilyName    = None
        uniqueFontIdentifier = None
        fullFontName         = None
        isItalic             = False 
        isBold               = False
        lfWeight             = 400   
        fontType             = 0    
        for nameRecIdx in range(len(nameMap.names)):
            nameRec = nameMap.names[nameRecIdx]
            if  nameRec.nameID == Constants.FONT_FAMILY_NAME:
                fontFamilyName = str(NameRecord.toUnicode( nameRec, errors='strict'))  
            elif nameRec.nameID == Constants.FONT_COPYRIGHT_NOTICE:
                fontCopyRight =    NameRecord.toUnicode( nameRec, errors='strict') 
                if  fontCopyRight == '':
                    fontCopyRight == 'None'    
                fontCopyRight = fontCopyRight.replace( ',', '|'  )           
            elif nameRec.nameID == Constants.FONT_SUBFAMILY_NAME:
                fontSubFamilyName = NameRecord.toUnicode( nameRec, errors='strict') 
                ( isItalic, isBold, lfWeight ) = getItalicBoldWeight( fontSubFamilyName )
            elif nameRec.nameID == Constants.FONT_UNIQUE_IDENTIFIER:
                uniqueFontIdentifier = NameRecord.toUnicode( nameRec, errors='strict') 
                uniqueFontIdentifier = uniqueFontIdentifier.replace( ',', ' '  )              # Lucida and others has comma char here ????
                uniqueFontIdentifier = uniqueFontIdentifier.replace( u',', u' ') 
            elif nameRec.nameID == Constants.FONT_FULL_NAME:
                fullFontName = NameRecord.toUnicode( nameRec, errors='strict')  
            if fontFamilyName and fontSubFamilyName and uniqueFontIdentifier and fullFontName:
                mapTableNames = str(sorted(ttf.keys()))
                fontNamesTableRec = [fontFamilyName, fontSubFamilyName, isItalic, isBold, lfWeight, uniqueFontIdentifier, fullFontName, fontFilePath, fontCopyRight, mapTableNames]
                fontNamesTable.append( fontNamesTableRec )
                break
    return fontNamesTable    
{
    "HEADER": {
        "CreatedBy": "TextShape",
        "Version": 5.123,
        "Author": "Michael Tomsia, PhD",
        "Content": "Window True Type Font Property Dump",
        "CreationDate": "18 Dec 2021   10:40:14",
        "FontName": "",
        "FontStyle": "NORMAL ANSI",
        "fontFilePath": "",
        "platEncIDSet": "",
        "platformIDSet": "",
        "languageSet": "",
        "glyphExist": "",
        "FontHeight": ""
    },
    "LOGFONT": {
        "lfFaceName": "Rockwell Condensed",
        "lfHeight": 75,
        "lfWidth": 21,
        "lfEscapement": 0,
        "lfOrientation": 0,
        "lfWeight": 400,
        "lfItalic": 0,
        "lfUnderline": 0,
        "lfStrikeOut": 0,
        "lfCharSet": 0,
        "lfOutPrecision": 3,
        "lfClipPrecision": 2,
        "lfPitchAndFamily": 1,
        "fonttypeStr": "TRUETYPE",
        "fonttype": 4
    },
    "TEXTMETRIC": {
        "tmHeight": 75,
        "tmAscent": 60,
        "tmDescent": 15,
        "tmInternalLeading": 11,
        "tmExternalLeading": 0,
        "tmAveCharWidth": 21,
        "tmMaxCharWidth": 74,
        "tmWeight": 400,
        "tmOverhang": 0,
        "tmDigitizedAspectX": 192,
        "tmDigitizedAspectY": 192,
        "tmFirstChar": 32,
        "tmLastChar": 0,
        "tmDefaultChar": 2,
        "tmBreakChar": -5,
        "tmItalic": 31,
        "tmUnderline": 0,
        "tmStruckOut": 32,
        "tmPitchAndFamily": 0,
        "tmCharSet": 0,
        "tmCharSetName": "ANSI"
    }
}

 

Regards
MichaelT

MichaelT
Message 17 of 22
epeleg
in reply to: MichaelT_123

epeleg
Participant
Participant

@MichaelT_123 Thanks for your code.
My basic hypothesis is that all this work was already done by fusion so I should not need to do it again...
This is why I prefer interrogating the existing font dropdowns in the UI over going to the file system...
Though I Do think It would be nice and helpfull if Autodesk would add some directly accessible font information into the API. ( a Design.FontList would be a nice addition)
 

0 Likes

@MichaelT_123 Thanks for your code.
My basic hypothesis is that all this work was already done by fusion so I should not need to do it again...
This is why I prefer interrogating the existing font dropdowns in the UI over going to the file system...
Though I Do think It would be nice and helpfull if Autodesk would add some directly accessible font information into the API. ( a Design.FontList would be a nice addition)
 

Message 18 of 22
epeleg
in reply to: kandennti

epeleg
Participant
Participant

@kandennti , Yes, I saw this comment about CommandDefinition.execute being actually executed when the script is done on some other post.
This might be similar to document refreshes in Javascript and HTML. 
Maybe using some techniques similar to those used in that case might be also helpful here,

like using timeouts or calls to sleep of some sort to release hold of the running thread...
I am somehow failing to figure out how to get from a CommandDefinition to the Command object

0 Likes

@kandennti , Yes, I saw this comment about CommandDefinition.execute being actually executed when the script is done on some other post.
This might be similar to document refreshes in Javascript and HTML. 
Maybe using some techniques similar to those used in that case might be also helpful here,

like using timeouts or calls to sleep of some sort to release hold of the running thread...
I am somehow failing to figure out how to get from a CommandDefinition to the Command object

Message 19 of 22
kandennti
in reply to: epeleg

kandennti
Mentor
Mentor

@epeleg .

 

I don't think there is a way to get a Command object from CommandDefinition.

If it were me, I would just use the getSketchTextFonts function you created if it works.

0 Likes

@epeleg .

 

I don't think there is a way to get a Command object from CommandDefinition.

If it were me, I would just use the getSketchTextFonts function you created if it works.

Message 20 of 22
epeleg
in reply to: epeleg

epeleg
Participant
Participant

@kandennti you might find this:
https://forums.autodesk.com/t5/fusion-360-api-and-scripts/script-too-fast-for-ui-to-keep-up/m-p/6626...
interesting.
It shows a way to force the Command.Execute to execute immediately like so:

var alignCommand = ui.commandDefinitions.itemById('AlignComponentsCmd');
var nvs = adsk.core.NamedValues.create();
nvs.add('executeImmediately', adsk.core.ValueInput.createByString('true'));
alignCommand.execute(nvs);


I am assuming this is not specific to the alignCommand.

0 Likes

@kandennti you might find this:
https://forums.autodesk.com/t5/fusion-360-api-and-scripts/script-too-fast-for-ui-to-keep-up/m-p/6626...
interesting.
It shows a way to force the Command.Execute to execute immediately like so:

var alignCommand = ui.commandDefinitions.itemById('AlignComponentsCmd');
var nvs = adsk.core.NamedValues.create();
nvs.add('executeImmediately', adsk.core.ValueInput.createByString('true'));
alignCommand.execute(nvs);


I am assuming this is not specific to the alignCommand.

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

Post to forums  

Autodesk Design & Make Report