My first python script cannot figure out error

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

My first python script cannot figure out error

Post by jfc4120 »

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: English/United States (en_US)
Installed mods: 
  * Help 1.0.3

Error is:
File "C:/Users/Owner/AppData/Roaming/FreeCAD/Macro/Rect_offset_get_angle.FCMacro", line 67, in <module>
FD=DE/sin(AF1)
<class 'NameError'>: name 'sin' is not defined
If you need whole script I will upload, meanwhile I tried a much smaller script:

Code: Select all

import FreeCAD,FreeCADGui
import Draft
import math
from PySide import QtGui

A=sin(.5)
print('Angle of fitting: ', A)
Same error:
File "C:/Users/Owner/AppData/Roaming/FreeCAD/Macro/sin_test.FCMacro", line 6, in <module>
A=sin(.5)
<class 'NameError'>: name 'sin' is not defined
Any help? I am using sin like here: https://wiki.freecadweb.org/Expressions
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: My first python script cannot figure out error

Post by edwilliams16 »

The sin function lives in the math module. You can either do

Code: Select all

import math
print(math.sin(0.1))
which brings in all of the math module, but you need to prefix, or

Code: Select all

from math import sin
print(sin(0.1))
which just imports sin into the current namespace


Expressions are not Python - it's a FreeCAD specific language. You can't put raw python into a FreeCAD file for security reasons.
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: My first python script cannot figure out error

Post by jfc4120 »

I got that part working, please see this:

In script I get:
File "C:/Users/Owner/AppData/Roaming/FreeCAD/Macro/Rect_offset_get_angle.FCMacro", line 68, in <module>
FD=DE/sin(AF1)
<class 'NameError'>: name 'AF1' is not defined
Here is script:

Code: Select all

# -*- coding: utf-8 -*-

import FreeCAD,FreeCADGui
import Draft
from math import *
from PySide import QtGui



WIRE = True
INPUTININCHES = True
if INPUTININCHES:
    convert = 25.4
else:
    convert =1.0

getDouble = QtGui.QInputDialog.getDouble
Vector, Placement = App.Vector, App.Placement
doc = App.ActiveDocument

sel = Gui.Selection.getSelectionEx()
start_point = Vector(0, 0, 0)
if sel:
    sel, = sel
    if sel.PickedPoints:
        start_point = sel.PickedPoints[-1]   

FLAG=0



WD, wdflag = getDouble(None, 'example', 'side view cheek width', 5)
WD *= convert


DE, deflag = getDouble(None, 'example', 'Length between ducts', 5)
DE *= convert

CL, clflag = getDouble(None, 'example', 'offset', 5)
CL *= convert

TCLRD, tclrdflag = getDouble(None, 'example', 'throat radius', 5)
TCLRD

if TCLRD == 0:
 TCLRD = .0001
CLRD = TCLRD + (.5 * WD)
FE=CL-(2*CLRD)

if FE < 0:
  FE = -1*FE
  FLAG = 1

if FLAG==1:
  TF1=FE/DE
  AF1=atan(FE/DE)
else:
  if FE == 0:
    FE = .001  
    TF1=DE/FE
    AF1=atan(DE/FE)

# AF1=atan(TF1)

if FLAG==1:
  FD=FE/sin(AF1)
else:
  FD=DE/sin(AF1)      <=============== this line is 68

FC=.5*FD
JF=CLRD
CF2=CLRD/FC
AF2=acos(CF2)

if FLAG==1:
  ELDG=90-(AF1+AF2)
else:
  ELDG=180-(AF1+AF2)


print('Angle of fitting: ', ELDG)

Please remember this is my first script, but I have done basic, java, lisp, etc in past.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: My first python script cannot figure out error

Post by edwilliams16 »

Code: Select all

if FLAG==1:
  TF1=FE/DE
  AF1=atan(FE/DE)
else:
  if FE == 0:
    FE = .001  
    TF1=DE/FE
    AF1=atan(DE/FE)
The second if has no else clause, which could leave AF1 undefined.

EDIT: BTW

Code: Select all

from math import *
is not recommended. You are filling up your namespace with functions likely unknown to you. Can can end up over-riding them or accidentally calling them. The bugs can be hard to find.
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: My first python script cannot figure out error

Post by jfc4120 »

Thanks for answers, for now it's solved. I didn't even know I had to convert radians to degrees. But figured it out.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: My first python script cannot figure out error

Post by onekk »

Some note, usually in Python "all upper case" variables are usually used to define constants, or costant meaning variables, I use this way in my code usually for DOC to define to a FreeCAD.ActiveDocument (that is not a costant, but is referring everytime to the same object)

Using it extensively could make code less readable (me too has started with BASIC, but now some time is passed and computer have lowercase) :D

There are some rules called PEP8 for python (searching for Puthon PEP8 will help you to find them on the official python site) that are not mandatory but will ease you in learning "good habits" like using 4 spaces as indentation and other things.

In my signature you will find my "FreeCAD scripting guide" that maybe could help you to get started, it is not finished yet, but some people have found it useful.

Regards.

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
Post Reply