Where to put python files? And about makeBox...

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
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!
Post Reply
carrascosa

Where to put python files? And about makeBox...

Post by carrascosa »

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!
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Where to put python files? And about makeBox...

Post by wmayer »

im looking for something more like a per project basis
It's not supported to have scripts available on a per project basis. BTW, how should this 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
Did you create a sub-directory under ~/.FreeCad/Mod/ and put the files into there. Putting them directly under ~/.FreeCad/Mod doesn't work.
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?
The makePlane() function create a dumb geometry. To make a fully parametric object you have to write:

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()
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.
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Where to put python files? And about makeBox...

Post by yorik »

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)
carrascosa
Posts: 6
Joined: Thu Nov 11, 2010 4:12 pm

Re: Where to put python files? And about makeBox...

Post by carrascosa »

That's great guys, thanks a lot for your help!
BTW, how should this work?
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.
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.
Did you create a sub-directory under ~/.FreeCad/Mod/ and put the files into there. Putting them directly under ~/.FreeCad/Mod doesn't work.
Yes, this is what I did:
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"
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
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.
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!)
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Where to put python files? And about makeBox...

Post by wmayer »

Yes, this is what I did:
mkdir ~/.FreeCad/Mod/My
> ~/FreeCad/Mod/My/Init.py
Editted Init.py and added this:
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 with

Code: Select all

import myscript
myscript.myfun(1)
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: Where to put python files? And about makeBox...

Post by yorik »

carrascosa 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!)
Good catch ;)
Post Reply