Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:26:04+00:00 2026-06-17T04:26:04+00:00

Here is the code: import math with open(test.stl) as file: vertices = [map(float, line.split()[1:4])

  • 0

Here is the code:

import math
with open("test.stl") as file:
    vertices = [map(float, line.split()[1:4])
                for line in file
                if line.lstrip().startswith('vertex')]

    normals = [map(float, line.split()[2:5])
                for line in file
                if line.lstrip().startswith('facet')]

V=len(vertices)
ordering=[]
N=len(normals)

for i in range(0,N):
    p1=vertices[3*i]
    p2=vertices[3*i+1]
    p3=verticies[3*i+2]
    print p1

    x1=p1[0]
    y1=p1[1]
    z1=p1[2]

    x2=p2[0]
    y2=p2[1]
    z2=p2[2]

    x3=p3[0]
    y3=p3[1]
    z3=p3[2]

    a=[x2-x1,y2-y1,z2-z1]
    b=[x3-x1,y3-y1,z3-z1]

    a1=x2-x1
    a2=y2-y1
    a3=z2-z1
    b1=x3-x1
    b2=y3-y1
    b3=z3-z1

    normal=normals[i]

    cross_vector=[a2*b3-a3*b2,a3*b1-a1*b3,a1*b2-a2*b1]

    if cross_vector==normal:
        ordering.append([i,i+1,i+2])
    else:
        ordering.append([i,i+2,i+1])
print ordering
print cross_vector

If I try to add print p1 (or any of the other variables such as cross_vector) inside of the for loop, there aren’t any errors but no output and if I try to print them outside of the for loop it says NameError: name ‘(variable name)’ is not defined. So if none of these variables are being defined, obviously my ordering array prints as [] (blank). How can I change this. Do variables have to be declared before they are defined?

Edit: Here is the error output when the code above is run:

Traceback (most recent call last):
  File "convert.py", line 52, in <module>
    print cross_vector
NameError: name 'cross_vector' is not defined

As explained above this happens with any variable defined in the for loop, I am just using cross_vector as an example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:26:05+00:00Added an answer on June 17, 2026 at 4:26 am

    To solve the problem diagnosed by DSM, use:

    import math
    import itertools
    with open("test.stl") as file:
        i1, i2 = itertools.tee(file)
        vertices = [map(float, line.split()[1:4])
                    for line in i1
                    if line.lstrip().startswith('vertex')]
    
        normals = [map(float, line.split()[2:5])
                    for line in i2
                    if line.lstrip().startswith('facet')]
    

    You might also want to try and drop the list comprehension, and work with iterators throughout, to save on memory for large files.


    Edit:

    At present, you load the entire file into memory, and then create two more full size lists in memory. Instead, you can write it in a way that only reads from the file in memory as required. As an example, we can replace the list comprehensions with generator comprehensions:

    import math
    import itertools
    with open("test.stl") as file:
        i1, i2 = itertools.tee(file)
        vertexIter = (map(float, line.split()[1:4])
                      for line in i1
                      if line.lstrip().startswith('vertex'))
    
        normalIter = (map(float, line.split()[2:5])
                      for line in i2
                      if line.lstrip().startswith('facet'))
    

    Here, we’ve avoided using any memory at all.

    For this to be useful, you need to be able to replace your loop, from:

    for i in range(0,N):
        p1=vertices[3*i]
        p2=vertices[3*i+1]
        p3=verticies[3*i+2]
        normal = normals[i]
    
        # processing
    

    To a single iterator:

    for normal, p1, p2, p3 in myMagicIterator:
        # processing
    

    One way I can think of doing this is:

    myMagicIterator = itertools.izip(
        normalIter,
        itertools.islice(vertexIter, 0, 3),
        itertools.islice(vertexIter, 1, 3),
        itertools.islice(vertexIter, 2, 3)
    )
    

    Which is the iterator equivalent of:

    myNormalList = zip(normals, vertices[0::3], vertices[1::3], vertices[2::3])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code: import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; import java.util.Collections; import
here's the code: import java.lang.System; import java.lang.Math; public class ArrayFunHouseTwo { public static int[]
Here is some code that I wrote using Python: from math import sqrt abundant_list
Here is the code: import javax.swing.SwingUtilities; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JLabel; import java.awt.event.*;
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here is my code import org.ksoap2.*; import org.ksoap2.serialization.*; import org.ksoap2.transport.*; import android.app.Activity; import android.os.Bundle;
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
For some reason, I can't get pyglet to draw sprites. Here's my code: import
Here is some sample Python code: import re some_regex = re.compile(r\s+1\s+) result = some_regex.search(
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.