Get selected python object

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
JAndersM
Posts: 63
Joined: Tue Dec 22, 2015 1:35 pm

Get selected python object

Post by JAndersM »

I'm a bit stuck.

I want to pass the python object from the selection in the tree view to a command since I want to use some methods in the selected object.

Code: Select all

    def Activated(self):
        sel=FreeCADGui.Selection.getSelection()
        try :
            ImagePlane.makeImagePlane(sel[0]) #Trying to get a reference to the python object
        except IndexError:
            return
How?
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Get selected python object

Post by DeepSOIC »

Hi!
Briefly, this is how python features work (Well, at least, this is how I understand the situation, I may be wrong).
All python features are actually a special type of c++ feature, "App::FeaturePython" (or "Part::FeaturePython"). These have a property called Proxy, which holds the Python object. When the c++ feature is executed, for example, it calls 'execute' method of the Proxy object. And so on for the rest.


So, if you want to talk to the Python object that is the heart of the feature, you can access it through sel[0].Proxy
JAndersM
Posts: 63
Joined: Tue Dec 22, 2015 1:35 pm

Re: Get selected python object

Post by JAndersM »

DeepSOIC wrote:So, if you want to talk to the Python object that is the heart of the feature, you can access it through sel[0].Proxy
Thanks! It works.
I thought it could be Proxy but could not find any documentation.
Post Reply