help with python

i have a .py (say allmethods.py) file that has some functions

have another main.py file (this is used in logic bricks) that uses the methods from the previous file…
how do i call the methods from allmethods.py in main.py

tried to import allmethods.py

from allmethods import *
but is giving some systemError:
SystemError: NULL result without error in PyObject_Call

Odd… Haven’t seen that one before… Is ‘allmethods.py’ in the same directory as the game, or in the Blender directory (on Windows, this works)? Also, what version of Blender are you using?

I think allmethods.py is being being imported fine. I believe that the error message means that the module was imported but there was an error in it, however, the faulty function didn’t tell python what was wrong (someone please correct me if I’m wrong). It’s not a common error.

I’d start looking in allmethods.py for typo’s/errors. If you’re finding it hard to spot you could always try removing one function at a time from allmethods.py then try importing it again and seeing if it works.

Post a .blend or the allmethods.py code so we can have a look at what’s going wrong.

try
import allmethods
instead of
import allmethods.py

make sure both files are in same blend?!

Also, perhaps you should make the script files external, just to see if that’s the problem.

#allmethods.py

import bge
import serial

def init(self):
return
def currentMode(parent):
mode=parent[‘mode’]
childObj=parent[parent[‘child’]]
childObj.setParent(parent)
action=parent[‘action’]

if mode==1:
    if action==1:
        performAction="tilt"
    elif action==0:
        performAction="trans"
    
elif mode==2:
    performAction="trans"
    
elif mode==3:
    performAction="tilt"
    
return mode,childObj,performAction

def translation(empty,X,Y,Z):
if X and Y:
if Z:
X =X* 0.57735
Y = Y0.57735
Z = Z
0.57735
else:
X =X* 0.7071
Y =Y* 0.7071
elif X and Z:
X =X* 0.7071
Z =Z* 0.7071

elif Y and Z:
 Y =Y* 0.7071
 Z =Z* 0.7071	
# apply Movement
empty.applyMovement([X, Y, Z],1)
return

def tilt(self,empty,X,Y,Z):

Xc = .005

Yc = .005

Getting the rotation values.

X1 = -float(Xmovement) * Xc
Y1 = float(Ymovement) * Yc
print(“X:”,X)
print(“Y:”,Y)

Xpivot.applyRotation([0, 0, X1],0)
Ypivot.applyRotation([Y1,0, 0],1)

return

def calculation(self,empty,X,Y,Z): ### select the axis and pass only that

##### all text objects####
objAngle=obj["display angle"]
objMagStrength=obj["display strength"]
printCurrent=obj["printCurrent"]



objAngle['Text']=x

area=.09
default="0.5"
objMagStrength['Text']=default


if (default=="0"):
     for i in range(1,6):
         obj[''.join(['line',str(i)])].visible=False
elif(0<float(default)<=0.2):
    for i in range(1,4):
        obj[''.join(['line',str(i)])].visible=False     
elif(0.2<float(default)<=0.5):
    for i in range(1,2):
        obj[''.join(['line',str(i)])].visible=False     

w= 4*x*3.14
printCurrent["Text"]=.5*area*w*math.sin(w/2)

return

def selectObject(self,empty):
obj=bge.logic.getCurrentScene().objects
objCursor=obj[‘Cursor’]
objCoil=obj[‘coil’]
objMagnet=obj[‘horseshoe’]

detectCoil=objCursor.rayCast(objCoil,objCursor,2)
detectMagnet=objCursor.rayCast(objMagnet,objCursor,2)

if "coil" in detectCoil:
    empty[empty[child]].removeParent(empty)
    empty['child']="coil"
    empty['mode']=3
    
elif "horseshoe" in detectMagnet:
    empty[empty[child]].removeParent(empty)
    empty['child']="horseshoe"
    empty['mode']=3
    
return

Syntax error:
inconsistent use of tabs and spaces in indentation

in method tilt and translation after each of the if loops…
:S
indentation is correct… it works well in 2.49…
I am using 2.56…

this things spoils the indentation… it have attached the file here:

main.py

my main file calling the method.py from above

If you Go Advanced and use # you can paste code and preserve it’s format.

oh ok…
any solution to the problem? :slight_smile:

I don’t know what you are trying to do. I don’t have any knowledge of importing .py files.

It’d would probably be better to post up a sample .blend as it’s hard to work out what you’re trying to do from the code and without your setup no one can test your code. Furthermore, a little background information on what you’re trying to do would help a lot.

Any way, I can import methods.py fine, even though none of the functions will work without your setup.

I did spot on error in main.py with how you import and call functions from methods.py. Using ‘from methods import *’ makes all the functions in methods.py local to the script, so calling the functions through methods will only result in an error as methods is not defined.


from methods import *
...
mode,objSelected,performAction=currentMode(empty)
#prefixing methods is not required as currentMode is already made local by importing
# everything from methods

### Alternatively ###

import methods
...
mode,objSelected,performAction=methods.currentMode(empty)
#Here the methods prefix is needed to tell python where to find the function
# as only methods is known to the script and not it's functions

Hope this helps! :smiley:

i did try the solution u gave…it gave a error even then…
i am attaching my file here…

http://www.4shared.com/file/wJPmxtJT/final.html

thank you.

I think I’ve spotted another error. In the calculation function in methods.py you reference math, but I can’t see anywhere where math is defined in the module:


#line 95 on the link you posted
printCurrent["Text"]=.5*area*w*math.sin(w/2)

I assume that you’re accessing a method in python’s math module, in which case that’ll need to be imported by methods.py before it is used. Seeing as it’s the only method used from math it’s probably better just to import that method rather than all of math:


from math import sin
...
#Then you can access sin without the math prefix:
printCurrent["Text"]=.5*area*w*sin(w/2)

Hope this helps! :smiley:

I didn’t notice this comment earlier, but python can be a bit funny with whitespace sometimes. While a single tab and four spaces look the same on screen sometimes python considers them different. Check all the indentations to make sure they’re all the same style (place your cursor at the beginning of the first word on the line and hit backspace to see which of the two it is).

Cheers for posting the .blend, sadly I’m not going to be able to look at it until either Saturday or Sunday evening. So if the problem still exists then I’ll have a look.

Edit: A quick glance at your .blend shows that you’ve used a mix of tab and space for indents. Try making them all one or the other and see if that fixes it.

Try deleting everything from the all_methods file and seeing if it imports alright then. That would at least let you know where the problem resides (in the code, or elsewhere). Also, did you notice when the script file failed, or is this a resource module that you like to use from other projects (not written for the BGE)?

thank you…
i think that was the problem!!

never come across such a thing… i hv been using python for about 6-7 months now!

No worries. Python doesn’t care too much about what style of whitespace you use to indent, but you’ve got to use that sytle consistently throughout the script otherwise python can’t determine what’s part of the code block.

See this: http://www.secnetix.de/olli/Python/block_indentation.hawk for more on indents and whitespace in python.