I know that I can put Python scripts into macros (or notebooks now). But I increasingly find myself writing code for individual documents, for example to change values, recompute, export and so on. It may even be a good idea in terms of organizing/distributing files to have the script inside the FC file.
Is there any way put a Python script inside a document file? If not, regard this as a Feature suggestion
Document-Specific scripts?
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: Document-Specific scripts?
Here is an interesting related topic.
Re: Document-Specific scripts?
Besides the thread already mentioned, How to embed a Python code into the document?
I finally found these other two references that I also wanted to mention: In particular, microelly2 mentions using a StringList property. This is actually how the Draft Text object is implemented.
Code: Select all
import FreeCAD as App
obj = App.ActiveDocument.addObject("App::FeaturePython", "Textual_block")
obj.addProperty("App::PropertyStringList", "Text", "Base", "The text content displayed by this object")
Code: Select all
obj.Text="""
import FreeCAD as App
obj = App.ActiveDocument.addObject("App::FeaturePython", "Textual_block")
obj.addProperty("App::PropertyStringList", "Text", "Base", "The text content displayed by this object")
# Some functions
def bla():
blah, bloh = something()
"""
Code: Select all
print(obj.Text[0])
Code: Select all
import PySide
Text = PySide.QtGui.QTextEdit()
Text.setText(obj.Text[0])
Text.setFont(PySide.QtGui.QFont("Mono"))
Text.show()
Code: Select all
obj.addProperty("App::PropertyFileIncluded", "AttachedTextFile", "Base", "A file to save inside this document")
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.