Macro for opaque and transparent mode

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
rebeldevil
Posts: 2
Joined: Fri Feb 17, 2017 1:29 pm

Macro for opaque and transparent mode

Post by rebeldevil »

Hello everyone!
I'm new to this forum and I would like start my participation expressing my gratitude to the FreeCAD developers for they wonderful work and results reached as for the date of this post.

I would also like to start participating providing two little but for me useful macros which allows to switch all the objects within a project from opaque to a given transparency value:

To make all solids translucent I've used the following code:

Code: Select all

doc = Gui.activeDocument()
objs = FreeCAD.ActiveDocument.Objects
	
for obj in objs:
	if obj:
		o = doc.getObject(obj.Name)
		if hasattr(o, 'Transparency'):
			o.Transparency = 50
and to make them again opaque you simply need to change the last line giving a value of 0.

I've found that code useful in conjuction with a custom toolbar created within the preferences and/or with keyboard shortcuts.
Any comment or correction on the code is welcome as I'm not really familiar with Python and its API's.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Macro for opaque and transparent mode

Post by mario52 »

hi welcome

Code: Select all

# -*- coding: utf-8 -*-
doc = Gui.activeDocument()
objs = FreeCAD.ActiveDocument.Objects
   
for obj in objs:
#   if obj:
#      o = doc.getObject(obj.Name)
#      if hasattr(o, 'Transparency'):
      obj.ViewObject.Transparency = 90

other example macro for transparency toggle 0 to 70%?

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
rebeldevil
Posts: 2
Joined: Fri Feb 17, 2017 1:29 pm

Re: Macro for opaque and transparent mode

Post by rebeldevil »

Thank you mario52. Searching within the forum didn't return topics related to the macro I was searching for so I've wrote it myself.
Anyway your suggestion are very instructive.
Post Reply