How to read configured paths from python

Hi,
as per the title I’m trying to read the configured path (for the footprint libraries).
I’m on Debian 11 with Kicad 6 from flatpack.

image

Based on the official docs I’m trying to get a hold on the kicad_common.json

settings = pcbnew.SETTINGS_MANAGER.GetUserSettingsPath()
with open(settings+'/kicad_common.json', 'r') as f:
            data = json.load(f)

but the env variables are not there.

any suggestion?

cheers
stef

They aren’t saved if they were never changed from default.
The only way to fetch them is os.getenv from within kicad

Hi @marekr,
can you elaborate the “from within kicad” part. is it not possible from within a plugin (pcbnew API)?

If you are using pcbnew module in a plugin then it runs in kicad and environment will have those vars in os.environ.

If you are using it outside of kicad in a script then it will not have those vars.

I wrote some Python codes to inspect Kicad/anything files… Some read the full contents, some only List the Path and Dir contents…etc

I made GUI’s for them to run as stand-alone that can be called from Plugin…

Naturally, programmer can grab info/contents as desired

They run from a Plugin in Kicad (screenshot)

Example of a Contents Reader

Thank you guys for all the pointers in the right direction. I adjusted my original code to search for customized path first and then, in case nothing is found there, fall back to the default location (os environment).

def init_config(self):
        
        self.fp_path = None

        # check modified paths
        settings = pcbnew.SETTINGS_MANAGER.GetUserSettingsPath()
        with open(settings+'/kicad_common.json', 'r') as f:
            data = json.load(f)
            if not (data["environment"]["vars"] is None) and "KICAD6_FOOTPRINT_DIR" in data["environment"]["vars"]:
                self.fp_path = data["environment"]["vars"]["KICAD6_FOOTPRINT_DIR"]

        # check default paths
        if self.fp_path is None:
            self.fp_path = os.getenv("KICAD6_FOOTPRINT_DIR", default=None)
        
        # no library found
        if self.fp_path is None:
            wx.LogError("Footprint library not found - Make sure the KiCad paths are properly configured.")

I have a feeling that there’s a better way to access the common settings than reading the file and drilling down the json tree, using the following member function (see API docs ):

settings = pcbnew.SETTINGS_MANAGER.GetCommonSettings()

In that case I get an error saying it expects 1 parameter (self) which doesn’t seem to align with the documentation.
any clue?

cheers
stef

pcbnew.GetSettingsManager().GetCommonSettings()
But it won’t do you any good because COMMON_SETTINGS class is not exposed in api.

aaah right. then the code above is the final one. :slightly_smiling_face:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.