CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   ParaView (https://www.cfd-online.com/Forums/paraview/)
-   -   [General] import local functions in pvpython (https://www.cfd-online.com/Forums/paraview/161061-import-local-functions-pvpython.html)

seanread October 17, 2015 17:42

import local functions in pvpython
 
So I have a script in my working directory which has functions I want to call in to pv python, but for some reason when I call them,

for example

Code:

import os
os.chdir('pathtomyscript')
from script import function

python says it can't find the module.

Does anyone know why this might be happening and how I can fix the issue?

Flowkersma October 18, 2015 17:59

If you're in a folder with myscript.py which has a function named myFunction then
Code:

from myscript import myFunction
should work.

Regards,
Mikko

seanread October 18, 2015 21:19

seems to be working today, I must have been doing something careless yesterday, but now, although I can import the function fine, it isn't recognising the things that I call from paraview.simple. For example currently I am using the following function:

Code:

def clip():

    import paraview

    FluentCaseReader1 = GetActiveSource()


    Clip1 = Clip( ClipType="Plane" )

    Clip1.Scalars = ['POINTS', 'DENSITY']

    Clip1.ClipType = "Plane"

    Clip1.ClipType.Origin = [-0.0454, -0.02, -0.002]

    Clip1.ClipType.Normal = [0.03, 0.1, 1]

    return(Clip1)

to define a clip, which works fine if I feed the body into pvpython, however when I import the function in pv python and then call the function, two strange things happen. Firstly, it saying

TypeError: clip() takes exactly 1 argument (0 given)

which is strange because clip() doesn't take any arguments.

Secondly, if I give it an arbitrary argument, say 1 for example, I get


FluentCaseReader1 = GetActiveSource()

NameError: global name 'GetActiveSource' is not defined

also, if I run the script in containing the functions in pv python and then call them they work fine...

Any suggestions?

Flowkersma October 19, 2015 04:54

The namespaces are somehow confused. Why are you importing paraview in the beginning of your function? Can you try this?

Code:

import myscript
import paraview.simple as pv

myscript.clip()

so = pv.GetActiveSource()
pv.Clip(so)


seanread October 19, 2015 06:17

Code:

>>> PA.clip()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/media/sean/data/MASTERSNASAL/Sean Read/Elderly flow paper/images/slicedata/PA.py", line 24, in clip
    DataRepresentation1 = GetDisplayProperties( FluentCaseReader1 )
NameError: global name 'GetDisplayProperties' is not defined


Code:

>>> pv.Clip(so)
<paraview.servermanager.Clip object at 0x7f8fd451dd50>

I added import paraview because it wasn't recognising the modules imported from paraview, but it didn't help. Is it something wrong with how I compiled paraview?

Flowkersma October 19, 2015 09:51

In the beginning of your script files you should import the ParaView Simple module. Simple module contains all the filters (Clip, Slice...). Either by
Code:

from paraview.simple import *
Clip(..)

or
Code:

import paraview.simple as pv
pv.Clip(...)


seanread October 19, 2015 22:04

seems to have done the trick, thanks again!

seanread December 9, 2015 19:11

hey you know this is weird, but I just did a clean install of paraview4.4 with openfoam from the debian package on ubuntu 15.10 and the same original problem with importing functions from the local folder has manifested. Do you have any thoughts as to why this might be?


All times are GMT -4. The time now is 17:22.