how to add ShapeColor from python script

Need help, or want to share a macro? Post here!
Prakash
Posts: 6
Joined: Wed Feb 20, 2019 6:37 am

how to add ShapeColor from python script

Postby Prakash » Wed Feb 20, 2019 7:01 am

Hello Everybody, I'm new here..

I'm using FreeCad on Windows Os and was trying to add color to a Part from python.
i know its possible from the GUI or using the python console panel like shown below.

Code: Select all

FreeCADGui.getDocument("Part1").getObject("Pad001").ShapeColor = (1.00,0.67,0.50)
but i was passing the python script to FreeCADCmd.exe
below is my code

Code: Select all

import Part
import Sketcher
import importOBJ
import Draft

App.newDocument("Part1")
doc = App.ActiveDocument

App.setActiveDocument("Part1")
App.ActiveDocument=App.getDocument("Part1")
App.activeDocument().addObject('PartDesign::Body','Body')

App.activeDocument().Body.newObject('Sketcher::SketchObject','Sketch')
App.activeDocument().Sketch.Support = (App.activeDocument().XY_Plane, [''])
App.activeDocument().Sketch.MapMode = 'FlatFace'

geoList = []
geoList.append(Part.LineSegment(App.Vector(-20.068783,10.162216,0),App.Vector(20.004869,10.162216,0)))
geoList.append(Part.LineSegment(App.Vector(20.004869,10.162216,0),App.Vector(20.004869,-9.970477,0)))
geoList.append(Part.LineSegment(App.Vector(20.004869,-9.970477,0),App.Vector(-20.068783,-9.970477,0)))
geoList.append(Part.LineSegment(App.Vector(-20.068783,-9.970477,0),App.Vector(-20.068783,10.162216,0)))
App.ActiveDocument.Sketch.addGeometry(geoList,False)


App.activeDocument().Body.newObject("PartDesign::Pad","Pad")
App.activeDocument().Pad.Profile = App.activeDocument().Sketch
App.activeDocument().Pad.Length = 10.0

App.ActiveDocument.Pad.Length = 10.000000
App.ActiveDocument.Pad.Length2 = 100.000000
App.ActiveDocument.Pad.Type = 0
App.ActiveDocument.Pad.UpToFace = None
App.ActiveDocument.Pad.Reversed = 0
App.ActiveDocument.Pad.Midplane = 0
App.ActiveDocument.Pad.Offset = 0.000000
App.ActiveDocument.recompute()

myobj = FreeCAD.getDocument("Part1").getObject("Pad001").ShapeColor = (1.0, 0.0, 1.0 )
this shows error at last line
Exception while processing file: test.py ['Part.Feature' object has no attribute 'ShapeColor']
is there any alternate way to add color/material to part via python script?

Thanks & Regards
Prakash
User avatar
microelly2
Posts: 4691
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: how to add ShapeColor from python script

Postby microelly2 » Wed Feb 20, 2019 7:58 am

You need the View object. it holds the View properties.

FreeCAD.getDocument("Part1").getObject("Pad001").Viewobject.ShapeColor = (1.0, 0.0, 1.0 )
Prakash
Posts: 6
Joined: Wed Feb 20, 2019 6:37 am

Re: how to add ShapeColor from python script

Postby Prakash » Wed Feb 20, 2019 8:11 am

Thanks for your reply
when i execute the code you shared, it says
AttributeError: 'NoneType' object has no attribute 'ShapeColor'

Code: Select all

>>x = FreeCAD.getDocument("Part1").getObject("Pad001").ViewObject
>>print type(x)
<type 'NoneType'>