Hi, first: your project is awsome, congratulations!
Second: I have some questions, I hope you can give me some hints... im using freecad 0.10, I think is rev 3501 or near, I took it from the braches and compiled it.
-Despite the large amount of wiki pages (6+) on how to script in freecad with python i could not find where am I suposed to leave my python scripts so that freecad can find them, either by auto-importing them, or via a import on the console. Its not that there are no places to do so, I could leave my files anywhere and add it to PYTHONPATH, but that wouldn't be a elegant solution... im looking for something more like a per project basis, for example: im designing a engine in freecad and I would like to have some code snippets and scripts for that project only... where do I put them?
I tryied putting files in ~/.FreeCad/Mod/ and they are loaded by freecad, but symbols defined in those files seem unavailable from the python console (ie, defined a function f, and that fuction can be called from the console).
-Another thing, I tried this example:
import Part
mycube = Part.makeBox(2,2,2)
Part.show(mycube)
and found that its not at all equivalent to have pressed the box button in the GUI. For instance, the box created with the button have "Height" , "Lenght" and "Width" and the one scripted doesn't.
If I want to have exactly the same outcome has having pressed the button, but in a python script, how can I do that?
Thank you!
Where to put python files? And about makeBox...
Forum rules
and Helpful information
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help
Also, be nice to others! Read the FreeCAD code of conduct!
Also, be nice to others! Read the FreeCAD code of conduct!
Re: Where to put python files? And about makeBox...
It's not supported to have scripts available on a per project basis. BTW, how should this work?im looking for something more like a per project basis
Did you create a sub-directory under ~/.FreeCad/Mod/ and put the files into there. Putting them directly under ~/.FreeCad/Mod doesn't work.I tryied putting files in ~/.FreeCad/Mod/ and they are loaded by freecad, but symbols defined in those files seem unavailable from the python console
The makePlane() function create a dumb geometry. To make a fully parametric object you have to write:and found that its not at all equivalent to have pressed the box button in the GUI. For instance, the box created with the button have "Height" , "Lenght" and "Width" and the one scripted doesn't. If I want to have exactly the same outcome has having pressed the button, but in a python script, how can I do that?
Code: Select all
doc=App.newDocument()
box=doc.addObject("Part::Box","Box")
box.Height=2.0
box.Length=2.0
box.Width=2.0
doc.recompute()
Re: Where to put python files? And about makeBox...
Maybe this could be an elegant solution:
- make a "ProjectLoader" folder in .FreeCAD/Mod
- Make an Init.py file in it (which will be executed when FreeCAD starts)
- in that file, scan your projects path, and add all project folders to the sys.path list
Sorry you didn't find the needed info in the doc, and thanks for the hint... I'll insert that somewhere (how to add a folder in the Mod path, and about Init.py and InitGui.py files)
- make a "ProjectLoader" folder in .FreeCAD/Mod
- Make an Init.py file in it (which will be executed when FreeCAD starts)
- in that file, scan your projects path, and add all project folders to the sys.path list
Sorry you didn't find the needed info in the doc, and thanks for the hint... I'll insert that somewhere (how to add a folder in the Mod path, and about Init.py and InitGui.py files)
-
- Posts: 6
- Joined: Thu Nov 11, 2010 4:12 pm
Re: Where to put python files? And about makeBox...
That's great guys, thanks a lot for your help!
And that document should be somehow attached to a regular CAD document, so that you can work with them together.
There are many IDE's that handle this idea of project, most of them in a overcomplicated fashion... but I found the idea of project as a set of files quite neat.
mkdir ~/.FreeCad/Mod/My
> ~/FreeCad/Mod/My/Init.py
Editted Init.py and added this:
And then in the freecad console:
>>> myfun(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'myfun' is not defined
>>> My.myfun(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'My' is not defined
Well, I was thinking in something like having a new type of document that is a text file and has python code on it... yeah, it would be replicating the function of an IDE, but maybe an existing IDE can be embedded and thus made more modular.BTW, how should this work?
And that document should be somehow attached to a regular CAD document, so that you can work with them together.
There are many IDE's that handle this idea of project, most of them in a overcomplicated fashion... but I found the idea of project as a set of files quite neat.
Yes, this is what I did:Did you create a sub-directory under ~/.FreeCad/Mod/ and put the files into there. Putting them directly under ~/.FreeCad/Mod doesn't work.
mkdir ~/.FreeCad/Mod/My
> ~/FreeCad/Mod/My/Init.py
Editted Init.py and added this:
Code: Select all
print "Loaded"
def myfun(x):
return str(x)+" is a banana"
>>> myfun(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'myfun' is not defined
>>> My.myfun(1)
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'My' is not defined
This is worth its weight in gold! I don't remember if this was in the scripting pages, but it definitively ought to be! (not just in the macro sections!)BTW, in the preferences under General > Macro you can switch on "Show script command in python console" to see what internal python code gets executed.
Re: Where to put python files? And about makeBox...
Ah, that's wrong! The files init.py and InitGui.py are two special files which get executed at startup, they are not imported. Generally you shouldn't put code into these two files which you need somewhere else. Simply make a new python file e.g. myscript.py, put the code into there and load it withYes, this is what I did:
mkdir ~/.FreeCad/Mod/My
> ~/FreeCad/Mod/My/Init.py
Editted Init.py and added this:
Code: Select all
import myscript
myscript.myfun(1)
Re: Where to put python files? And about makeBox...
Good catchcarrascosa wrote:This is worth its weight in gold! I don't remember if this was in the scripting pages, but it definitively ought to be! (not just in the macro sections!)