Function definition gives error, running code outside function works

Hi,

As the title says, I have a function sigmadef() that gives an error. The error is " object_2$2 is not callabe ", which google only links to Ladybug, but I’m not using that.

However, when I comment the function call and put the code from within the function directly in the script, it works fine.

Why does the same code not work within the function?

import Rhino
import rhinoscriptsyntax as rs
import scriptcontext
np = scriptcontext.sticky['numpy']
#activate=True;bar=100
#if activate:
#    
#    upper_limit = abs(bar)
#    Rhino.UI.StatusBar.ShowProgressMeter(0, upper_limit, "Python comp", True, True)
#    for i in range(upper_limit):
#        
#        rs.Sleep(10)
#        if i%10 == 0: Rhino.UI.StatusBar.UpdateProgressMeter(i, True)
#    
#    Rhino.UI.StatusBar.HideProgressMeter()

def lim(y,l,i):
    try:
        return y[l[i]]
    except IndexError:
        return -1
#return:    either y[l[i]] or -1

def geom(x,yi, diri):
    ii = []; dsdxi = []; dzdxi = []
    ii.append( [np.argmax(yi > ele) +(len(yi))*(yi[-1] <  ele) for ele in x] )
    ii = ii[0]
    dzdxi.append([-lim(diri,ii,ele[0]) for ele in enumerate(x)])
    dzdxi = dzdxi[0]
    dsdxi.append([(1+dzdxi[ele[0]]**2)**0.5 for ele in enumerate(x)])
    dsdxi = dsdxi[0]
    ia = ii
    if max([ele<0 for ele in yi]):
        ia.extend( [ele for ele, a in enumerate([not(ele>0) for ele in yi]) if a] )
    if not ( all( x in ia for x in range(len(yi)) ) ):
        print('not every brick is represented in linspace x')
        print x
        print ia
        exit()
    return ii,dsdxi,dzdxi
#return:    ii,dsdxi,dzdxi

def zero(i):
    zeros = np.zeros((i,len(x) ))
    return zeros
#return:    zeros
    
def new(l):
    n = len([l0,l1,l2][l])
    ii = [i0,i1,i2][l]
    h = h_old
    h[[i for i in range(len(ii)) if ii[i] == n]] = h_old[[i for i in range(len(ii)) if ii[i] == n]]+t
    return(h,n)
#return:    h,n

def section_forces(n,yi,Ai,dsdx,dzdx,h):
    weight = rho*9.81*t #N/m²
    y=yi[n] #m
    A=Ai[n] #m²/m¹ (Sum(l*b)/L)
    
    Vx = np.piecewise(x, [x<=y,x>y], [weight*A,0])
    M = weight*(y-x)*A
    M = [0 if ele < 0 else ele for ele in M]
    
    Vs = Vx/dsdx
    Ns = Vx*dzdx/dsdx
    
    W = 1/6 * h**2
    A = h
    return M,Vs,Ns,W,A
#return:    M,Vs,Ns,W,A

def sigmadef():
    dist = [[
        np.linspace( -1,1,int(h[j]/t+1) )[i]
        if int(h[j]/t)>=i
        else 0
        for j in range(len(x))]
        for i in range(lmax+1)]
    sigma_m = np.array(
        [[
        M[j]/W[j] *10**(-6) * dist[i][j]
        if h[j] != 0
        else 0
        if h[j]/t>=i
        else 0
        for j in range(len(x))]
        for i in range(lmax+1)] )
    sigma_n = np.array(
        [[
        Ns[j]/A[j] *10**(-6)
        * (h[j]/t>=i)
        if h[j] != 0
        else 0
        for j in range(len(x)) ]
        for i in range(lmax+1)] )
    tau =  np.array(
       [[
       Vs[j]*(6*dist[i][j]**2 - 3/2)/h[j] *10**(-6)
       if h[j] != 0
       else 0
       if h[j]/t>=i
       else 0
       for j in range(len(x)) ]
       for i in range(lmax+1)] )
    return dist,sigma_m,sigma_n, tau
#return:    dist, sigma_m, sigma_n, tau

#
                        ###Script begint hier

        ##directe bewerking input
L = L+lu    #float
[fb,fm,fvk,fti,ftf,deltax,pot,cure] = sm    #list of floats
print ("fb=",fb)
fy = fb*fm                          #####!!!!!
fyt = fti*ftf
fyv = fvk
y0 = [ele for ele in y0 if ele<L/2];    #list of floats
y1 = [ele for ele in y1 if ele<L/2]
y2 = [ele for ele in y2 if ele<L/2]
#t & rho is also one float number   A0,A1,A2,row are lists of floats

        ##output
seq=["layer;brick"]

        ##overige parameters

l0=[];l1=[0];l2=[0]; lmax=3

ymin = min(
min([j-i for i, j in zip(y0[:-1], y0[1:])]),
min([j-i for i, j in zip(y1[:-1], y1[1:])]),
min([j-i for i, j in zip(y2[:-1], y2[1:])]))
CHECK = (deltax < ymin)
if CHECK==False:
    print "Reduce deltax!"
    print ymin

x = np.arange(0,L/2,deltax)

i0,dsdx0,dzdx0 = geom(x,y0,Dir0)    #list of floats
i1,dsdx1,dzdx1 = geom(x,y1,Dir1)
i2,dsdx2,dzdx2 = geom(x,y2,Dir2)

sigma_M = zero(4); sigma_N = zero(4); Tau = zero(4)
sigma_m = zero(4); sigma_n = zero(4); tau = zero(4); dist = zero(4)
h_old = zero(1)[0]

[r1,r2,r3] = row
r1,r2,r3 = r1*pot/cure,r2*pot/cure,r3*pot/cure
r = zero(4)

for i in range(3):
#for i in range(len(y0)+len(y1)+len(y2)):
    
    l=0
    h,n = new(l)
    
    M,Vs,Ns,W,A = section_forces(n,y0,A0,dsdx0,dzdx0,h)
    dist = [[
        np.linspace( -1,1,int(h[j]/t+1) )[i]
        if int(h[j]/t)>=i
        else 0
        for j in range(len(x))]
        for i in range(lmax+1)]
    sigma_m = np.array(
        [[
        M[j]/W[j] *10**(-6) * dist[i][j]
        if h[j] != 0
        else 0
        if h[j]/t>=i
        else 0
        for j in range(len(x))]
        for i in range(lmax+1)] )
    sigma_n = np.array(
        [[
        Ns[j]/A[j] *10**(-6)
        * (h[j]/t>=i)
        if h[j] != 0
        else 0
        for j in range(len(x)) ]
        for i in range(lmax+1)] )
    tau =  np.array(
       [[
       Vs[j]*(6*dist[i][j]**2 - 3/2)/h[j] *10**(-6)
       if h[j] != 0
       else 0
       if h[j]/t>=i
       else 0
       for j in range(len(x)) ]
       for i in range(lmax+1)] )
#    AASigma = sigmadef() #Why don't you work!!??
    
    sigma = sigma_M + sigma_m + sigma_N + sigma_n
    h/t
    print seq
    print dist
    print sigma_m
    print sigma_n
    print tau
    sigma_comp = np.min(sigma[0])
    sigma_tens = np.max(sigma[1:4])
    tau_cum = np.max(Tau + tau)
    
    UC0 = max(sigma_comp/fy, sigma_tens/fyt, tau_cum/fyv)
    
    if len(l0)==len(y0):
        UC0=1.5
    print("UC0 " + str(UC0))
    print n
    
                                                                                      
                                                                                      
                                                                                      #Layer 1 (2nd)
                                                                                      
                                                                                      

    if n>0:
        print "second layer"
##    if ( UC0>((l2[-1]+3)/(len(y2)+2)) or l0[-1]-l1[-1]+1>3 ) and ( y0[l0[-1]]-lu>y1[l1[-1]] or len(l0)==len(y0) ):
#        l=1
#        n = min(l1[-1],len(y1)-1)
#        y=y1[n]
#        Aw=A1[n]
#        h[[i for i in range(len(i1)) if i1[i] == n]] = h[[i for i in range(len(i1)) if i1[i] == n]]+t
#        Vx = np.piecewise(x, [x<=y,x>y], [weight*Aw,0])
#        M = weight*(y-x)*Aw
#        M = [0 if ele < 0 else ele for ele in M]
#        Vs = Vx/dsdx1 #maakt als het goed is weinig uit welke dsdx hier staat
#        Ns = Vx*dzdx1/dsdx1
#        W = 1/6 * h**2
#        A = h
#        
#        print('M',M)
#        Mi,Vsi,Nsi,Wi,Ai = section_forces(n,y1,A1,dsdx1,dzdx1,h)
#        print(Mi,Nsi)
#        
#        dist = [[np.linspace(-1,1,int(h[j]/t+1))[i] if int(h[j]/t)>=i else 0 for j in range(len(x))] for i in range(lmax+1)]
#        sigma_m = np.array( [ [ M[j]/W[j] *10**(-6) * dist[i][j] if h[j] != 0 else 0 if h[j]/t>=i else 0 for j in range(len(x))] for i in range(lmax+1)] )
#        sigma_n = np.array([ [ Ns[j]/A[j] *10**(-6) if h[j] != 0 else 0 if h[j]/t>=i else 0 for j in range(len(x)) ] for i in range(lmax+1)] )
#        tau_max = 3/2 * Vs/t *10**(-6)
#        
#        sigma = sigma_M + sigma_m + sigma_N + sigma_n
#        sigma_comp = np.min(sigma[0])
#        sigma_tens = np.max(sigma[1:4])
#        tau_cum = np.max(Tau + tau)
#        
#        UC1 = max(sigma_comp/fy, sigma_tens/fyt, tau_cum/fyv)
#        if len(l1)==len(y1):
#            UC1=1.5
#        print("UC1 " + str(UC1))
#        
#                                                                                      #Layer 2 (3rd)
#        if ( UC1>((l2[-1]+3)/(len(y2)+2)) or l1[-1]-l2[-1]+1>3 ) and ( y1[l1[-1]]-lu>y2[l2[-1]] or len(l1)==len(y1) ):
#            l=2
#            n = min(l2[-1],len(y2)-1)
#            y=y2[n]
#            Aw=A2[n]
#            h[[i for i in range(len(i2)) if i2[i] == n]] = h[[i for i in range(len(i2)) if i2[i] == n]]+t
#            Vx = np.piecewise(x, [x<=y,x>y], [weight*Aw,0])
#            M = weight*(y-x)*Aw
#            M = [0 if ele < 0 else ele for ele in M]
#            Vs = Vx/dsdx2 #maakt als het goed is weinig uit welke dsdx hier staat
#            Ns = Vx*dzdx2/dsdx2
#            W = 1/6 * h**2
#            A = h
#            dist = [[np.linspace(-1,1,int(h[j]/t+1))[i] if int(h[j]/t)>=i else 0 for j in range(len(x))] for i in range(lmax+1)]
#            sigma_m = np.array( [ [ M[j]/W[j] *10**(-6) * dist[i][j] if h[j] != 0 else 0 if h[j]/t>=i else 0 for j in range(len(x))] for i in range(lmax+1)] )
#            sigma_n = np.array([ [ Ns[j]/A[j] *10**(-6) if h[j] != 0 else 0 if h[j]/t>=i else 0 for j in range(len(x)) ] for i in range(lmax+1)] )
#            tau_max = 3/2 * Vs/t *10**(-6)
#            
#            sigma = sigma_M + sigma_m + sigma_N + sigma_n
#            sigma_comp = np.min(sigma[0])
#            sigma_tens = np.max(sigma[1:4])
#            tau_cum = np.max(Tau + tau)
#            
#            UC2 = max(sigma_comp/fy, sigma_tens/fyt, tau_cum/fyv)
#            if len(l2)==len(y2):
#                UC2=1.5
#            print("UC2 " + str(UC2))
#            
#            if UC2>.95:
#                print("")
#                print("UC too big")
#                print("")
#                print("sigma_comp " + str(sigma_comp)); print("sigma_tens " + str(sigma_tens)); print("tau " + str(tau))
#                break
#                
#                
#            else:
#                if len(l2)<len(y2):
#                    l2.append(n+1)
#                seq.append(str(l) + ";" + str(n))
#                
#        else:
#            if len(l1)<len(y1):
#                l1.append(n+1)
#            seq.append(str(l) + ";" + str(n))
#            
    else:
        if len(l0)<len(y0):
            l0.append(n)
        seq.append(str(l) + ";" + str(n))
        
    h_old = h
    
    sigma_M = sigma_M + sigma_m #is correct sign for position on cross-section
    sigma_N = sigma_N + sigma_n #negative is correct
    tau_Cum = Tau + tau #positive is correct
    print "run one done"

print("                 end loop")
print
print ('maximum stresses:')

sigma = sigma_M + sigma_N
maxcomp = np.min(sigma[0])
maxtens = np.max(sigma[1:4])
maxshear = np.max(Tau)
print ("maxcomp: ", np.round(maxcomp,3))
print ("maxtens: ", np.round(maxtens,3))
print ("maxshear: ", np.round(maxshear,3))
print
print seq

layer = [ele.split(';')[0] for ele in seq]
brick = [ele.split(';')[1] for ele in seq]
print layer
print brick```

I can’t run your code to try and find the error. Could you provide a complete running sample, preferably your GH file with the variables internalized and as much as possible removed to keep just keep the code running?

From a quick read I can’t see anything directly related to the error you have. What line does it appear? Can you also post the full error message?

That line likely means you are using GH PYthon Remote:

You are creating remote numpy arrays using repeated indexing into local lists, which can slow down your code a lot on large arrays. sigmadef does that for every line. There is a simpler example and a solution to that here.

You would probably find your script easier to debug if you did not rely on global variables in functions. x should be an input variable to zero, l0, l1, l2, i0, i1, i2 to new, etc.