How to set footprint 3D model settings in python interface?

Problem Summary
Can’t set pcbnew.VECTOR_FP_3DMODEL().m_Scale through the python interface with KiCad v7.0.0. Want to understand why and be pointed toward possible fixes.

Problem Details
I’m in the process of updating a set of python libraries/scripts I have for automating tasks to KiCad 7.0.0.

Right now I’m updating an abstract class for auto-generating footprints with KLC compliant parameters. I’ve been using the ‘’ swig interface but have encountered an issue with setting parameters for the footprints 3D model.

The issue seems to be that there is a class that hasn’t been ported through swig called VECTOR3D. This appears to have been exported in the interface for some 6.x releases (as pcbnew.VECTOR3D) and used to be exported as pcbnew.MODULE_3D_SETTINGS_VECTOR3D in 5.x. I’ve included some snippets to demonstrate what I mean more clearly at the bottom.

There is a class in called VECTOR_FP_3DMODEL which logically seems like it might be taking the place of VECTOR3D but setting parameters of an instance of pcbnew.VECTOR_FP_3DMODEL to this causes a type error with swig.

I’m not sure if I’m misunderstanding how the interface has now been setup or if the VECTOR3D class was forgotten when updating the swig interface and should be added back in. I’ve not got any experience with generating the swig interface and haven’t been compiling my KiCad installation but if someone could hand-hold me through getting started on that, I would be interested in contributing to the source for changes such as this if that seems like the best solution.

Python Console Demo

>>> import pcbnew
>>> pcbnew.Version()
>>> model_settings = pcbnew.FP_3DMODEL()
>>> scale = pcbnew.VECTOR_FP_3DMODEL()
>>> model_settings.m_Scale = scale
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'FP_3DMODEL_m_Scale_set', argument 2 of type 'VECTOR3D'
>>> pcbnew.VECTOR3D
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'pcbnew' has no attribute 'VECTOR3D'
>>> [entry for entry in dir(pcbnew) if "VECTOR3D" in entry]

Correlated question:

I encountered this problem while realizing that my wrl models were being imported in units of 0.1" (eg. a pin with a diameter of 1 unit would have a 2.54mm (0.1") diameter after importing). Is this expected behavior? I feel like it used to standardize to 1mm as the standard unit and 0.1" feels like an odd standard, so I just wanted to confirm this was intentional.

As for solutions from the python script (aside from just scaling down all models after importing by a constant factor of 2.54) is there any built-in method of specifying units or a default scaling?

IIRC the 3D model python bindings are quite raw. For changing the filename, I’ve had to pop the model from the internal datastructure, change the filename and push the modified model back. Take a look at Acrchive 3D model plugin code

Thanks for the response. I agree in that my assumption is that the swig interface has a necessary component missing (VECTOR3D). What I’m hoping for is a response as to why this is the case. A valid response from someone who contributes to the source might be “Oh yeah, someone removed it and no one noticed it meant you couldn’t change model offsets, scales and rotations anymore. Someone should fix it, here’s how”. I might also just go report an issue on the repo now.

For the sake of anyone else seeing this, some of the parameters of a pcbnew.FP_3DMODEL instance operate fine. It’s just the ones that require a VECTOR3D that seem to be broken (?). Dumping a python console snippet below for reference.

>>> import pcbnew
>>> model_settings = pcbnew.FP_3DMODEL()
>>> dir(model_settings)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__swig_destroy__', '__weakref__', 'm_Filename', 'm_Offset', 'm_Opacity', 'm_Rotation', 'm_Scale', 'm_Show', 'this', 'thisown']
>>> model_settings.m_Filename = "fake_file.wrl"
>>> model_settings.m_Opacity = 0.5
>>> model_settings.m_Show = True

What I’m hoping for is a response as to why this is the case. A valid response from someone who contributes to the source might be “Oh yeah, someone removed it and no one noticed it meant you couldn’t change model offsets, scales and rotations anymore. Someone should fix it, here’s how”.

I am assuming that until now, nobody required access to these fields from python. I wen through similar issue with access to the filename field. It also was not accessible from python before 5.1. And then @qu1ck added code to C++/SWIG on my request. You might ask him for help.

@qu1ck I really hate that I am “offering” your services. It feels like I am being a pimp. So if that bothers you, just let me know and I’ll stop.

1 Like

Update: I now at least see that pcbnew.VECTOR_FP_3DMODEL is actually a vector of pcbnew.FP_3DMODEL instances from this commit 9 months ago by @qu1ck

Also it seems the swig interface for pcbnew.FP_3DMODEL is being built automatically from pcbnew/footprint.h as included by the swig interface file footprint.i

So it seems like what might need to happen is linking the vector3.h file in the swig interface file.

I’m still not 100% sure how to go about doing that and if it might break something else but maybe someone else can take this and implement it.

Cheers for that, also just in case this wasn’t clear: this interface was perfectly functional at least for most versions of 5.x - I’m adapting old code I wrote for 5.x that used this for several years. Something about updates to the interface between then and now have broken the reference to the type class of FP_3DMODEL.m_Offset, FP_3DMODEL.m_Scale and FP_3DMODEL.m_Rotation which now appear to be of type VECTOR3D.

1 Like

Ok, I think I’ve nailed it down now. The VECTOR3D class appears that it used to be derived from a custom class: MODULE_3D_SETTINGS but then at some point was updated to point to an external math library. @qu1ck originally wrote in a line to rename that VECTOR3D into the swig interface but it hasn’t been updated in the swig file since moving away from MODULE_3D_SETTINGS.

I’m hoping this means the fix is fairly trivial for @qu1ck but I’m also obviously not expecting anything and am willing to help out if I can be poked in the right direction. I know that there are also complications where the devs don’t want to expose too much to the python interface because everyone gets riled up when things in the interface change but this one seems pretty safe as it’s just updating a reference that already existed and is now outdated.

Issue created in Gitlab here.


For the sake of anyone checking this / searching it in the future. This fix was incorporated into the 7.0.1 release which is already in the Ubuntu PPA - works great! Thanks again qu1ck.

1 Like