2 years ago

#69695

test-img

WeiGitGe

NameError when transfering code from Jupyter Notebook to Python file

For a study project, I'm developing a tool that creates arrangements of shapes as an stl file. The end result should be a Python program that only requires input parameters and produces the wanted stl output.

Run in a jupyter notebook, the code works fine, but when I transfer it to a .py file and try to run it I get the following error:

>> create_calibody("sphere", 4, 1, 20, 6, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'create_calibody' is not defined 

Please excuse any rudimentary errors made. My Python paths consist of more or less successfully stumbling forward.

My research so far has not provided any useful advice.

####                    Dependencies    
import numpy as np
import open3d as o3d    

def create_calibody(shape, nbr, size, resolution, orbit, incline):

    r = orbit
    res = resolution
    c = incline
    size = size


   if shape == "sphere":

        initial_mesh = o3d.geometry.TriangleMesh.create_sphere(radius = size, resolution = res)

    else:
        print( "Unknown shape. ")

    ##     Collecting mesh parameters    

    initial_vertices = np.asarray(initial_mesh.vertices)                
    initial_triangles = np.asarray(initial_mesh.triangles)              
    n_v = np.asarray(initial_mesh.vertices).shape[0]                    # collecting amount of original vertices in mesh


    ### helix function

    angle = np.linspace(0, 5, nbr)

    ##############

    new_vertices = None
    new_triangles = None
    iter_triangles = None

    for i in angle:

        x = r*np.cos(i)
        y = r*np.sin(i)
        z = c*i

        ### adding new triangles positions
        if new_triangles is None:
            new_triangles = initial_triangles
            iter_triangles = new_triangles
        
        else:
            iter_triangles = iter_triangles + n_v
            new_triangles = np.concatenate((new_triangles, iter_triangles))
    
        ### original values of inital sphere
        vx = initial_vertices[:,0]          ## x axis
        vy = initial_vertices[:,1]          ## y axis
        vz = initial_vertices[:,2]          ## z axis 

        ### adding selected orbit values to original coordinates  -> new coordinate
        vx_app = vx + x
        vy_app = vy + y
        vz_app = vz + z                     

        ### create list with new values
        v_app = []
        v_app.append(vx_app)
        v_app.append(vy_app)
        v_app.append(vz_app)

        v_app = np.asarray(v_app)
        v_app = v_app.T         
    
        if new_vertices is None:
            new_vertices = v_app

        else:
            new_vertices = np.concatenate((new_vertices, v_app))

    ############    Creating new mesh based on new coordinates  

    v = o3d.utility.Vector3dVector(new_vertices)

    t = o3d.utility.Vector3iVector(new_triangles)

    final = o3d.geometry.TriangleMesh(v, t)

    # preprocessing: computing normals

    final = o3d.geometry.TriangleMesh.compute_vertex_normals(final)
    final = o3d.geometry.TriangleMesh.compute_triangle_normals(final)

    ##############    creating new stl.file   

    o3d.io.write_triangle_mesh("new_stl.stl", final, write_ascii=False, compressed=False, 
                                        write_vertex_normals=True, write_vertex_colors=False,  
                                        write_triangle_uvs=False, print_progress=True)


##### When run in a Jupyter notebook I am able to create the wanted stl:
create_calibody("sphere", 4, 1, 20, 6, 2)

python

function

mesh

0 Answers

Your Answer

Accepted video resources