Add method to PythonObject [SOLVED]

Need help, or want to share a macro? Post here!
keithsloan52
Posts: 1282
Joined: Mon Feb 27, 2012 5:31 pm

Add method to PythonObject [SOLVED]

Postby keithsloan52 » Sat May 30, 2020 9:55 am

I am unable to add a method to a class for a PythonObject what am I doing wrong?

class PObject :
def __init__(self, obj,...)

def addProp(self) :
print('test')

tst = PObject(obj,...)
tst.addProp()

<class 'AttributeError'>: 'PObject' object has no attribute 'addProp'

OS: macOS 10.15
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.16146 (Git)
Build type: Release
Branch: (HEAD detached at 0.18.4)
Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8
Python version: 3.6.7
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedKingdom (en_GB)
Last edited by keithsloan52 on Tue Jun 02, 2020 2:18 pm, edited 1 time in total.
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Add method to PythonObject

Postby onekk » Sat May 30, 2020 1:09 pm

maybe using the code tags will be a better way, the indentation is not shown, in your code, so it is difficult to see what is wrong.

It seems, that a PythonObject class must be derived from an existing FreeCAD class, to inherit some basic property, but I'm not an expert.

see maybe :

https://github.com/FreeCAD/FreeCAD/blob ... /Shapes.py

or maybe

https://github.com/FreeCAD/FreeCAD/blob ... eatures.py

or even

https://github.com/FreeCAD/FreeCAD/blob ... bspline.py

For some hint on how to make an object.

Hope it helps

Regards

Carlo D.
keithsloan52
Posts: 1282
Joined: Mon Feb 27, 2012 5:31 pm

Re: Add method to PythonObject

Postby keithsloan52 » Sat May 30, 2020 6:56 pm

onekk wrote:
Sat May 30, 2020 1:09 pm
maybe using the code tags will be a better way, the indentation is not shown, in your code, so it is difficult to see what is wrong.

It seems, that a PythonObject class must be derived from an existing FreeCAD class, to inherit some basic property, but I'm not an expert.

see maybe :

https://github.com/FreeCAD/FreeCAD/blob ... /Shapes.py

or maybe

https://github.com/FreeCAD/FreeCAD/blob ... eatures.py

or even

https://github.com/FreeCAD/FreeCAD/blob ... bspline.py

For some hint on how to make an object.

Hope it helps

Regards

Carlo D.
Well the code I tried to show is not what I have actually coded it was just an attempt to illustrate the problem.

Actual code looks like

Code: Select all

class GDMLTessellated(GDMLcommon) :
    
   def __init__(self, obj, material) :
      obj.addProperty('App::PropertyBool','editable','GDMLTessellated', \
                      'Editable').editable = False
      obj.addExtension('App::OriginGroupExtensionPython', self)
      obj.addProperty("App::PropertyEnumeration","material", \
                      "GDMLTessellated","Material")
      setMaterial(obj, material)
      self.Type = 'GDMLTessellated'
      self.Vertex = []
      self.Faces  = []
      self.Object = obj
      obj.Proxy = self

   def onChanged(self, fp, prop):
       '''Do something when a property has changed'''
       #print(fp.Label+" State : "+str(fp.State)+" prop : "+prop)
       if 'Restore' in fp.State :
          return
       
       if prop in ['material'] :
           fp.ViewObject.ShapeColor = colourMaterial(fp.material)

       if prop in ['editable'] :
           if editable == True :
              addProperties()
              
       if prop in ['lunit'] :
          self.createGeometry(fp)
   
   def addVertex(self,v) :
       self.Vertex.append(v)
              
As well as addVertex there are other functions that I wish to use.

But if in some other code I have

Code: Select all

      tess = GDMLTessellated(obj,0)
If I then do print(dir(tess))

It does not list addVertex or any of the other similar functions.

Thing is I wish to update the GDMLTessellated object tess some time after creation.
keithsloan52
Posts: 1282
Joined: Mon Feb 27, 2012 5:31 pm

Re: Add method to PythonObject

Postby keithsloan52 » Sun May 31, 2020 5:53 am

This seems to have some relevance https://forum.freecadweb.org/viewtopic. ... 1&start=10

As I don't really understand the use of proxy maybe I am missing a trick @wmayer
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Add method to PythonObject

Postby onekk » Sun May 31, 2020 10:57 am

I don't understand, completely the post of wmayer, it is true that i don't work much on the interface part.

Generally I use the exposed python functions to make objects, so the solid part will suffice my needs, (creating 3d object for 3d printing).

The "Modelling" part of FreeCAD is what I nedd, i know (not in a too deep way), as I'm partepating in some "theoric" discussion on some interface modifications, maybe from some time i will achieve a little knowledge about the GUI and App part (They are separated, as FreeCAD is nott using the internal OCCT visualisation part and is using Coin 3D to visualize the solids).

This is mostly all the interface matters I know, from now.

Regards

Carlo D.
keithsloan52
Posts: 1282
Joined: Mon Feb 27, 2012 5:31 pm

Re: Add method to PythonObject

Postby keithsloan52 » Sun May 31, 2020 1:26 pm

keithsloan52 wrote:
Sun May 31, 2020 5:53 am
This seems to have some relevance https://forum.freecadweb.org/viewtopic. ... 1&start=10

As I don't really understand the use of proxy maybe I am missing a trick @wmayer
Tried adding @expose as per the example in the post but get error expose not defined.
Is @expose a Python 2.x facility not available Python3?
onekk
Posts: 379
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Add method to PythonObject

Postby onekk » Sun May 31, 2020 3:12 pm

@expose is a python decorator, (I don't know exactly what is the purpose of a decorator) I've downloaded FreeCAD sources, that I've downloaded to study them some weeks ago, (0.19 branch, not up to date, but useful, I don't want to use much bandwidth, as I'm on pay for X GB connection, and my childrens are using it for school).

A quick analysis show that #expose is not in the actual code, and the post you cite is almost old 2013 or so on.

Try to wait some more time, generally developers are around this forum tree, and maybe if the post are populated enough some of them will answer your questions.

Regards

Carlo D.
keithsloan52
Posts: 1282
Joined: Mon Feb 27, 2012 5:31 pm

Re: Add method to PythonObject

Postby keithsloan52 » Tue Jun 02, 2020 2:17 pm

Seems the functions are available via the Proxy