"ViewObject" access in python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
c3kkos
Posts: 10
Joined: Thu Nov 15, 2018 7:42 am
Location: Pisa

"ViewObject" access in python

Post by c3kkos »

Hi there, i've an issue regarding python scripting with linux version of FreeCAD.

I've this simple code, exploring python-freecad capabilities:

freecad.py:

Code: Select all

# -*- coding:utf-8 -*-

FREECADPATH = '/usr/lib/freecad/lib' # path to your FreeCAD.so
import sys
sys.path.append(FREECADPATH)

import FreeCAD, FreeCADGui, Part, Draft, math, MeshPart, Mesh, Drawing
from PyQt4 import QtGui,QtCore
from FreeCAD import Base
from FreeCAD import Vector
App=FreeCAD
Gui=FreeCADGui

def main():
        base_length = 800
        base_width = 600
        thickness = 15
        name = "freecad_python_test"
        App.newDocument(name)
        App.setActiveDocument(name)
        App.ActiveDocument.addObject("Part::Box","Test_Box")
        App.ActiveDocument.Test_Box.Length=base_length
        App.ActiveDocument.Test_Box.Width=base_width
        App.ActiveDocument.Test_Box.Height=thickness
        App.ActiveDocument.Test_Box.Placement=Base.Placement(Base.Vector(0.000,0.000,0.000),Base.Rotation(0.000,0.000,0.000,1.000))
        App.ActiveDocument.Test_Box.Label='Python Generated BOX'
        [b]App.ActiveDocument.Test_Box.ViewObject.Visibility=True[/b]
        App.ActiveDocument.recompute()
        App.ActiveDocument.saveAs("/mnt/reports/"+name+".FCStd")
        print "Generating a box with lenght: " + str(base_length) + "mm  and width: " + str(base_width) + "mm. Saved in /mnt/reports/"+name+".FCStd"


main()
Everything runs fine, the file is correctly generated, but since the solid is NOT visible when the .FCStd is opened, i tried to fix with:

Code: Select all

        [b]App.ActiveDocument.Test_Box.ViewObject.Visibility=True[/b]
        
then this error raises:

FreeCAD 0.16, Libs: 0.16R
Traceback (most recent call last):
File "freepy1.py", line 33, in <module>
main()
File "freepy1.py", line 27, in main
App.ActiveDocument.Test_Box.ViewObject.Visibility=True
AttributeError: 'NoneType' object has no attribute 'Visibility'



in FreeCAD python console the operation runs fine...

I'm using Vanilla Debian for linux and this is the log on the windows machine:

OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.6712 (Git)
Build type: Release
Branch: releases/FreeCAD-0-16
Hash: da2d364457257a7a8c6fb2137cea12c45becd71a
Python version: 2.7.8
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 6.8.0.oce-0.17



PS: i'm using the old 0.16 version to match the stable FreeCAD version offered by Debian's repositories
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: "ViewObject" access in python

Post by microelly2 »

the modified script works fine for me

Code: Select all

# -*- coding:utf-8 -*-

FREECADPATH = '/usr/lib/freecad/lib' # path to your FreeCAD.so
import sys
sys.path.append(FREECADPATH)

import FreeCAD, FreeCADGui, Part, Draft, math, MeshPart, Mesh, Drawing
# from PyQt4 import QtGui,QtCore
from FreeCAD import Base
from FreeCAD import Vector
App=FreeCAD
Gui=FreeCADGui

def main():
        base_length = 800
        base_width = 600
        thickness = 15
        name = "freecad_python_test"
        App.newDocument(name)
        App.setActiveDocument(name)
        App.ActiveDocument.addObject("Part::Box","Test_Box")
        App.ActiveDocument.Test_Box.Length=base_length
        App.ActiveDocument.Test_Box.Width=base_width
        App.ActiveDocument.Test_Box.Height=thickness
        App.ActiveDocument.Test_Box.Placement=Base.Placement(Base.Vector(0.000,0.000,0.000),Base.Rotation(0.000,0.000,0.000,1.000))
        App.ActiveDocument.Test_Box.Label='Python Generated BOX'
        App.ActiveDocument.Test_Box.ViewObject.Visibility=True
        App.ActiveDocument.recompute()
#        App.ActiveDocument.saveAs("/mnt/reports/"+name+".FCStd")
        print "Generating a box with lenght: " + str(base_length) + "mm  and width: " + str(base_width) + "mm. Saved in /mnt/reports/"+name+".FCStd"


main()

c3kkos
Posts: 10
Joined: Thu Nov 15, 2018 7:42 am
Location: Pisa

Re: "ViewObject" access in python

Post by c3kkos »

Hi, thanks for quick reply!

Amazing community, it'll be great to come by, since i'm new.

I copy/pasted your modified script and the error is still present.

The suspect is now upon the linux library i got installed in my debian box.

here's the log with the exactly pasted script:

c3kkos@vmbox:~$ python2.7 test.py
FreeCAD 0.16, Libs: 0.16R
Traceback (most recent call last):
File "test.py", line 33, in <module>
main()
File "test.py", line 27, in main
App.ActiveDocument.Test_Box.ViewObject.Visibility=True
AttributeError: 'NoneType' object has no attribute 'Visibility'
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: "ViewObject" access in python

Post by ickby »

if you are in console mode without a GUI up ViewObject is none. No view means no view object. Hence you cannot set visibility from a console only freecad.
c3kkos
Posts: 10
Joined: Thu Nov 15, 2018 7:42 am
Location: Pisa

Re: "ViewObject" access in python

Post by c3kkos »

Maybe it's not needed but here's the screen of windows FreeCAD opening the python generated file, and using the console for setting the visibility TRUE (which runs fine)
screen.jpg
screen.jpg (113.5 KiB) Viewed 7870 times
c3kkos
Posts: 10
Joined: Thu Nov 15, 2018 7:42 am
Location: Pisa

Re: "ViewObject" access in python

Post by c3kkos »

ickby wrote: Thu Nov 15, 2018 9:55 am if you are in console mode without a GUI up ViewObject is none. No view means no view object. Hence you cannot set visibility from a console only freecad.
Understood.

Since ViewObject is inside App container (not Gui) i thinked it was accessible from there.

Also, i found that solution here in forum inside an older thread

here:

https://forum.freecadweb.org/viewtopic. ... =4&t=16088




So, what can i do to have the solid visible by default in a python generated .FCStd?
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: "ViewObject" access in python

Post by bernd »

you could check if FreeCAD GUI is up and only than set visibility of view object

Code: Select all

if FreeCAD.GuiUp:
User avatar
bernd
Veteran
Posts: 12851
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: "ViewObject" access in python

Post by bernd »

ah I might missunderstood ... You would like to create the FreeCAD file in console mode but the objects should be visible if the file isopen in FreeCAD Gui. OK my snippet does nof help here ...
c3kkos
Posts: 10
Joined: Thu Nov 15, 2018 7:42 am
Location: Pisa

Re: "ViewObject" access in python

Post by c3kkos »

bernd wrote: Thu Nov 15, 2018 5:06 pm ah I might missunderstood ... You would like to create the FreeCAD file in console mode but the objects should be visible if the file isopen in FreeCAD Gui. OK my snippet does nof help here ...
Yes, that's right!

To be honest, i can't think the reason why (and of course there's one....) the visibility is off by default when i compile a .FCStd within a python script.

Maybe this will mark a distinction for those generated by script and those drawed "by hand" using GUI..
Post Reply