View Issue Details

IDProjectCategoryView StatusLast Update
0004328Project Tools & WebsitesGeneralpublic2021-02-06 06:36
Reportertomkcook Assigned To 
Status newResolutionopen 
Target Version0.20 
Summary0004328: Several "Scripted objects" examples don't work
DescriptionSee the examples on this wiki page.

The "Basic example" works as advertised. But the "Other more complex example", when pasted directly into the FreeCAD python console, produces a couple of triangles, not the expected octahedron.

And when it is modified as suggested in the section "Making objects selectable", only a single red line is shown in the 3D display (which is then not selectable).

I have pasted my modified version of this code into the "Additional information" section because I couldn't make the file attachment work.

I have tried this on a couple of builds from source, most recently on commit bc107eacc8e020a779b2bbcb21559283ee440268, as well as the 0.18.3 that is distributed with Ubuntu 19.10 and the 0.18.4 that is distributed with Ubuntu 20.04. None of them worked as expected, though there were minor variations in the results.
Steps To ReproduceOpen FreeCAD. Paste the attached code into the FreeCAD python console.
Additional Informationimport FreeCAD, FreeCADGui, Part
import pivy
from pivy import coin

class Octahedron:
  def __init__(self, obj):
     "Add some custom properties to our box feature"
     obj.addProperty("App::PropertyLength","Length","Octahedron","Length of the octahedron").Length=1.0
     obj.addProperty("App::PropertyLength","Width","Octahedron","Width of the octahedron").Width=1.0
     obj.addProperty("App::PropertyLength","Height","Octahedron", "Height of the octahedron").Height=1.0
     obj.addProperty("Part::PropertyPartShape","Shape","Octahedron", "Shape of the octahedron")
     obj.Proxy = self

  def execute(self, fp):
     # Define six vetices for the shape
     v1 = FreeCAD.Vector(0,0,0)
     v2 = FreeCAD.Vector(fp.Length,0,0)
     v3 = FreeCAD.Vector(0,fp.Width,0)
     v4 = FreeCAD.Vector(fp.Length,fp.Width,0)
     v5 = FreeCAD.Vector(fp.Length/2,fp.Width/2,fp.Height/2)
     v6 = FreeCAD.Vector(fp.Length/2,fp.Width/2,-fp.Height/2)
     # Make the wires/faces
     f1 = self.make_face(v1,v2,v5)
     f2 = self.make_face(v2,v4,v5)
     f3 = self.make_face(v4,v3,v5)
     f4 = self.make_face(v3,v1,v5)
     f5 = self.make_face(v2,v1,v6)
     f6 = self.make_face(v4,v2,v6)
     f7 = self.make_face(v3,v4,v6)
     f8 = self.make_face(v1,v3,v6)
     fp.Shape = solid

  # helper mehod to create the faces
  def make_face(self,v1,v2,v3):
     wire = Part.makePolygon([v1,v2,v3,v1])
     face = Part.Face(wire)
     return face

class ViewProviderOctahedron:
  def __init__(self, obj):
     "Set this object to the proxy object of the actual view provider"
     obj.addProperty("App::PropertyColor","Color","Octahedron","Color of the octahedron").Color=(1.0,0.0,0.0)
     obj.Proxy = self

  def attach(self, obj):
     "Setup the scene sub-graph of the view provider, this method is mandatory"
     self.shaded = coin.SoGroup()
     self.wireframe = coin.SoGroup()
     self.scale = coin.SoScale()
     self.color = coin.SoBaseColor()

     selectionNode = coin.SoType.fromName("SoFCSelection").createInstance()
     selectionNode.objectName.setValue(obj.Object.Name) # here obj is the ViewObject, we need its associated App Object

     style=coin.SoDrawStyle() = coin.SoDrawStyle.LINES

  def updateData(self, fp, prop):
     "If a property of the handled feature has changed we have the chance to handle this here"
     # fp is the handled feature, prop is the name of the property that has changed
     if prop == "Shape":
        s = fp.getPropertyByName("Shape")
        for i in s.Vertexes:








  def getDisplayModes(self,obj):
     "Return a list of display modes."
     return modes

  def getDefaultDisplayMode(self):
     "Return the name of the default display mode. It must be defined in getDisplayModes."
     return "Shaded"

  def setDisplayMode(self,mode):
     return mode

  def onChanged(self, vp, prop):
     "Here we can do something when a single property got changed"
     FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
     if prop == "Color":
        c = vp.getPropertyByName("Color")

  def getIcon(self):
     return """
        /* XPM */
        static const char * ViewProviderBox_xpm[] = {
        "16 16 6 1",
        " c None",
        ". c #141010",
        "+ c #615BD2",
        "@ c #C39D55",
        "# c #000000",
        "$ c #57C355",
        " ........",
        " ......++..+..",
        " .@@@@.++..++.",
        " .@@@@.++..++.",
        " .@@ .++++++.",
        " ..@@ .++..++.",
        "###@@@@ .++..++.",
        "#$$####### ",
        "#$$#$$$$$# ",
        "#$$#$$$$$# ",
        "#$$#$$$$$# ",
        " #$#$$$$$# ",
        " ##$$$$$# ",
        " ####### "};

  def __getstate__(self):
     return None

  def __setstate__(self,state):
     return None

FreeCAD InformationOS: Ubuntu 20.04 LTS (ubuntu:GNOME/ubuntu)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.20816 (Git)
Build type: Unknown
Branch: master
Hash: bc107eacc8e020a779b2bbcb21559283ee440268
Python version: 3.8.2
Qt version: 5.12.8
Coin version: 4.0.0
OCC version: 7.3.0
Locale: English/United Kingdom (en_GB)



2020-04-28 19:45

developer   ~0014386

@tomkcook : Please address your issue according our reporting guidelines by first opening a topic on our forum ("Python" subsection) so community can vet the issue and narrow down the causes. Cross-link the topic with this ticket. Thx


2020-04-29 07:59

reporter   ~0014388

@openBrain : I raised the problems described in this thread and got very little response. If that thread reaches a resolution and you want to assign me a wiki login, I'm very happy to update the documentation myself.


2020-04-30 10:52

reporter   ~0014390

Have also just verified the same result using the latest AppImage version downloaded from the FreeCAD website.


2022-03-03 13:55

administrator   ~0016886

This ticket has been migrated to GitHub as issue 6052.

Issue History

Date Modified Username Field Change
2020-04-28 15:14 tomkcook New Issue
2020-04-28 19:44 openBrain Tag Attached: #post-to-forum
2020-04-28 19:45 openBrain Status new => feedback
2020-04-28 19:45 openBrain Note Added: 0014386
2020-04-29 07:59 tomkcook Note Added: 0014388
2020-04-29 07:59 tomkcook Status feedback => new
2020-04-30 10:52 tomkcook Note Added: 0014390
2021-02-06 06:36 abdullah Target Version => 0.20