# How to prevent a PShape object to close?

**URL:** <https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340>\
**Category:** Processing.py\
**Created:** [August 3, 2018, 9:15pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340 "2018-08-03T21:15:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [August 3, 2018, 9:15pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/1 "2018-08-03T21:15:33Z")

</div>

Hi everyone,

I’d like to display a `TRIANGLE_STRIP` mesh as a PShape object but when doing so the mesh “closes” on itself (lines connecting the last points to the first points of the mesh appear)

Here’s an example to be as clear as possible:

**Mesh computed in `draw()` every frame** _(working correctly but slow)_

```auto
add_library('peasycam')
scl = 10

def setup():
    global n_rows, n_cols, terrain
    size(1200, 800, P3D)
    
    cam = PeasyCam(this, 1800)
    n_cols, n_rows = width / scl, width / scl
    terrain = [[0 for e in range(n_rows)] for f in range(n_cols)]
 
    yoff = 0
    for y in range(n_rows):
        xoff = 0
        for x in range(n_cols):
            noiseDetail(8)
            ny = map(y, 0, n_rows, 0, 1)
            nx = map(x, 0, n_cols, 0, 2)
            n_value = map(noise(nx*1.6, ny), 0, 1, -200, 1000)
            terrain[x][y] = n_value
            xoff += .014
        yoff += .011

def draw():
    background(240)
    pushMatrix()
    translate(-width/2, height/2, -width/2)
    rotateX(PI/2)
        
    for y in range(n_rows-1):
        beginShape(TRIANGLE_STRIP) 
        fill(240)
        for x in range(n_cols):
            vertex(x * scl, y * scl, terrain[x][y] )
            vertex(x * scl, (y + 1) * scl, terrain[x][y + 1] )
        endShape()
        
    popMatrix()

```

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/80b7f398d68839b35e2d57545ee3f0f5e2484f29.png)

**Mesh computed in `setup()` and stored in a PShape object** _(not working correctly but fast)_

```auto
add_library('peasycam')
scl = 10

def setup():
    global mesh, n_rows, n_cols, terrain
    size(1200, 800, P3D)
    
    cam = PeasyCam(this, 1800)
    n_cols, n_rows = width / scl, width / scl
    terrain = [[0 for e in range(n_rows)] for f in range(n_cols)]
    
    yoff = 0
    for y in range(n_rows):
        xoff = 0
        for x in range(n_cols):
            noiseDetail(8)
            ny = map(y, 0, n_rows, 0, 1)
            nx = map(x, 0, n_cols, 0, 2)
            n_value = map(noise(nx*1.6, ny), 0, 1, -200, 1000)
            terrain[x][y] = n_value
            xoff += .014
        yoff += .011
     
    mesh = createShape()
    for y in range(n_rows-1):
        mesh.beginShape(TRIANGLE_STRIP) 
        mesh.fill(240)
        for x in range(n_cols):
            mesh.vertex(x * scl, y * scl, terrain[x][y] )
            mesh.vertex(x * scl, (y + 1) * scl, terrain[x][y + 1] )
        mesh.endShape()

def draw():
    background(240)
    pushMatrix()
    translate(-width/2, height/2, -width/2)
    rotateX(PI/2)
    
    shape(mesh, 0, 0)
    
    popMatrix()

```

 ![](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/1X/de183cf18e7985fbabe55016ef8a54fa403b0a7a.png)

I think the problem comes from the `endShape()` function being called _within_ the double `for` loop and not `ouside` of it but can’t seem to find a proper solution to this.

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [August 4, 2018, 5:26am UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/2 "2018-08-04T05:26:55Z")

</div>

Hi Solub!  
~~I think this is a bug. I just reproduced it on my own as well. This is from the [Daniel Shiffman’s noisy terrain](https://www.youtube.com/watch?v=IKB1hWWedMk). It appears to be an issue when you create a PShape variable and set it to TRIANGLE\_STRIP rather than just using beginShape() on its own.~~ ( **Check post below for clarification** )

~~Here’s where the bug appears:~~

```python
def setup():
    global cols,rws,scl,w,h,terrain,flying,mesh
    size(400,400,P3D)
    smooth(8)
    w = 600
    h = 600
    scl = 20
    flying = 0
    cols = w/scl
    rws = h/scl
    
    #Instantiating 2D matrix for noisy terrain values
    terrain = [[0 for j in range(rws)] for i in range(cols)]
    
    #Creating noisy values for terrain
    for y in range(cols):    
        for x in range(rws):
            terrain[x][y] = map(noise(x*0.1,y*0.1+flying),0,1,-100,100)
    
    #Creating noisy mesh
    mesh = createShape()
    for y in range(cols-1):
        mesh.beginShape(TRIANGLE_STRIP)
        for x in range(rws):
            mesh.vertex(x*scl,y*scl, terrain[x][y])
            mesh.vertex(x*scl,(y+1)*scl, terrain[x][y+1])
        mesh.endShape()
    
def draw():
    global flying
    
    #Display settings
    background(0)
    stroke(255)
    noFill()
    
    # Coordinate transformations
    translate(width/2,height/2)
    rotateX(PI/3)
    translate(-w/2.0,-h/2.0)
    
    #Calling the noisy mesh
    shape(mesh,0,0)
    
    #Flying parameter
    flying -= 0.05         

```

Here’s where it goes away if I stop using PShape as a variable.

```python
def setup():
    global cols,rws,scl,w,h,terrain,flying,mesh
    size(400,400,P3D)
    background(0)
    smooth(8)
    w = 600
    h = 600
    scl = 20
    flying = 0
    cols = w/scl
    rws = h/scl
    
    #Instantiating 2D matrix for noisy terrain values
    terrain = [[0 for j in range(rws)] for i in range(cols)]
    
    #Creating noisy values for terrain
    for y in range(cols):    
        for x in range(rws):
            terrain[x][y] = map(noise(x*0.1,y*0.1+flying),0,1,-100,100)
    
    # Coordinate transformations
    translate(width/2,height/2)
    rotateX(PI/3)
    translate(-w/2.0,-h/2.0)
    
    #Creating noisy mesh
    for y in range(cols-1):
        with beginShape(TRIANGLE_STRIP):
            for x in range(rws):
                vertex(x*scl,y*scl, terrain[x][y])
                vertex(x*scl,(y+1)*scl, terrain[x][y+1])
        
     

```

Perhaps you can report it as an issue in the [processing.py github issues](https://github.com/jdf/processing.py/).

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [August 4, 2018, 1:40pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/3 "2018-08-04T13:40:42Z")

</div>

How unfortunate ! I’ll report the issue, thank you @WakeMeAtThree.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 4, 2018, 11:13pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/4 "2018-08-04T23:13:27Z")

</div>

> [@WakeMeAtThree](#):
>
> I think this is a bug.

> [@WakeMeAtThree](#):
>
> Perhaps you can report it as an issue in the [processing.py github issues](https://github.com/jdf/processing.py/)

> [@solub](#):
>
> I’ll report the issue, …

Have you checked 1st whether a Java Mode version of the sketch would work or not before reporting it as a Python Mode exclusive bug?! 🐛

Anyways, just made a workaround version using `createShape(GROUP)` in order to separate each `beginShape(TRIANGLE_STRIP)` child’s scope: 🐍

```auto
"""
 PShape Noisy Terrain Gen (v1.2)
 mod GoToLoop (2018-Aug-04)

 Discourse.Processing.org/t/
 how-to-prevent-a-pshape-object-to-close/2340/4
"""

add_library('peasycam')

OPAQ, BLACK, WHITE = PImage.ALPHA_MASK, 0, -1
FG, BG, STROKE = WHITE, BLACK, 0x0000FF | OPAQ

SCL, MAG = 15, 200
HALF_MAG = MAG>>1
STEP, ROTATE = .2, HALF_PI

W, H = 1000, 800
CLS, RWS = W/SCL, H/SCL
COLS_RANGE, ROWS_RANGE = tuple(range(CLS)), tuple(range(RWS))

ZERO, SPC = [0.0], ' '
SCALED, FRAC = lambda n: n * SCL, lambda n: n * STEP

SCALED_XS = tuple(map(SCALED, COLS_RANGE))
SCALED_YS = tuple(map(SCALED, ROWS_RANGE))

NOISE_XS = tuple(map(FRAC, COLS_RANGE))
NOISE_YS = tuple(map(FRAC, ROWS_RANGE))

terrain = tuple(ZERO*CLS for row in ROWS_RANGE)

def setup():
    size(800, 600, P3D)
    smooth(8); stroke(STROKE); fill(FG)

    global cw, ch, cz
    cw, ch, cz = -width>>1, height>>1, -width>>1

    PeasyCam(this, width)
    renoiseTerrain(); createMesh()

def draw():
    background(BG)
    translate(cw, ch, cz); rotateX(ROTATE)
    shape(mesh, 0, 0)

def keyPressed():
    if key == SPC or key == ENTER:
        renoiseTerrain(); createMesh()

def renoiseTerrain():
    noiseSeed(int(random(-MAX_INT, MAX_INT)))

    for row, y in zip(terrain, NOISE_YS):
        for idx in COLS_RANGE:
            row[idx] = MAG*noise(NOISE_XS[idx], y) - HALF_MAG

def createMesh():
    global mesh
    mesh = createShape(GROUP)

    prevRow = terrain[0]; prevY = SCALED_YS[0]

    for row, y in zip(terrain, SCALED_YS):
        if row is prevRow: continue

        m = createShape(); mesh.addChild(m)
        m.beginShape(TRIANGLE_STRIP)

        for idx, x in enumerate(SCALED_XS):
            m.vertex(x, prevY, prevRow[idx])
            m.vertex(x, y, row[idx])

        m.endShape()
        prevRow = row; prevY = y

```

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [August 5, 2018, 5:06am UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/5 "2018-08-05T05:06:10Z")

</div>

> [@GoToLoop](#):
>
> Have you checked 1st whether a Java Mode version of the sketch would work or not before reporting it as a Python Mode exclusive bug?! 🐛

Hi GoToLoop!

Good call! 👏 I just tested it and it is also happening in Java as well. Here’s the result from Daniel Shiffman code (tweaked it a bit to be in setup only):

 ![screenShot2](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/94d4cd1c7fa3aa866fed815974593f33eb323f2f.png)

```java
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;

void setup() {
  size(600, 600, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[cols][rows];
  float yoff = flying;
  for (int y = 0; y < rows; y++) {
    float xoff = 0;
    for (int x = 0; x < cols; x++) {
      terrain[x][y] = map(noise(xoff, yoff), 0, 1, -100, 100);
      xoff += 0.2;
    }
    yoff += 0.2;
  }
  
  background(0);
  stroke(0);
  
  translate(width/2, height/2+50);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  
  for (int y = 0; y < rows-1; y++) {
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {
      vertex(x*scl, y*scl, terrain[x][y]);
      vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
    }
    endShape();
  }
}

```

Here’s the result from tweaked code to make it use `createShape()`:

 ![screenShot1](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/f/f84961c030adf16c5ad7d9b2cea588a5d29aa4a3.png)

```java
// Tweaking Code to check for a possible
// Bug in PShape from:
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/IKB1hWWedMk

int cols, rows;
int scl = 20;
int w = 2000;
int h = 1600;

float flying = 0;

float[][] terrain;

void setup() {
  size(600, 600, P3D);
  cols = w / scl;
  rows = h/ scl;
  terrain = new float[cols][rows];
  float yoff = flying;
  for (int y = 0; y < rows; y++) {
    float xoff = 0;
    for (int x = 0; x < cols; x++) {
      terrain[x][y] = map(noise(xoff, yoff), 0, 1, -100, 100);
      xoff += 0.2;
    }
    yoff += 0.2;
  }
  
  background(0);
  stroke(0);
  
  translate(width/2, height/2+50);
  rotateX(PI/3);
  translate(-w/2, -h/2);
  
  PShape mesh = createShape();
  
  for (int y = 0; y < rows-1; y++) {
    mesh.beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < cols; x++) {
      mesh.vertex(x*scl, y*scl, terrain[x][y]);
      mesh.vertex(x*scl, (y+1)*scl, terrain[x][y+1]);
    }
    mesh.endShape();
  }
  shape(mesh,0,0);
}

```

Both using Processing 3.3.7. If someone can kindly check with the later versions to confirm as I need to head to work. ~~Seems like an issue worthy to report.~~ ( **Check post below for clarification** )

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [August 5, 2018, 5:24am UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/6 "2018-08-05T05:24:14Z")

</div>

> [@WakeMeAtThree](#):
>
> Both using Processing 3.3.7. If someone can kindly check with the later versions to confirm as I need to head to work. Seems like an issue worthy to report.

What’s making me hesitant now to flat out call this a bug for sure is that [the `createShape()` example](https://processing.org/reference/createShape_.html) in processing doesn’t seem to have an issue, even after taking to 3D. Perhaps it needs more testing to see where it breaks, or maybe something is off in the original noisy terrain logic that’s calling the last and first strip of points again, making it close.

```java
//Tweaking createShape() example
//from Processing reference

void setup() {
  size(400, 400, P3D);
  translate(width/2,height/2);
  rotateX(PI/3);
  
  PShape s = createShape();
  s.beginShape(TRIANGLE_STRIP);
  s.vertex(30, 75);
  s.vertex(40, 20);
  s.vertex(50, 75);
  s.vertex(60, 20);
  s.vertex(70, 75);
  s.vertex(80, 20);
  s.vertex(90, 75);
  s.endShape();
  shape(s,0,0);
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 5, 2018, 1:00pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/7 "2018-08-05T13:00:56Z")

</div>

> [@WakeMeAtThree](#):
>
> … to flat out call this a bug for sure is that [the `createShape()` example](https://processing.org/reference/createShape_.html) in Processing doesn’t seem to have an issue, …

That example got 1 pair of **beginShape(TRIANGLE\_STRIP)** & **endShape()**'s **vertex()** block. 1️⃣

Our “terrain” sketches got a loop of **beginShape(TRIANGLE\_STRIP)** & **endShape()** pairs! ➰

BtW, we can use constant THIRD\_PI in place of `PI/3`. 😉

---

<div class="post-metadata">

**Author:** ![WakeMeAtThree](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/wakemeatthree/32/38_2.png) [@WakeMeAtThree](https://discourse.processing.org/u/WakeMeAtThree)\
**Post date:** [August 5, 2018, 1:45pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/8 "2018-08-05T13:45:40Z")

</div>

Hmm. Not sure it’s a bug now. I think @solub would just be fine by doing the following:

```python
def setup():
    global cols,rws,scl,w,h,terrain,flying,mesh
    size(400,400,P3D)
    smooth(8)
    w = 600
    h = 600
    scl = 20
    flying = 0
    cols = w/scl
    rws = h/scl
    
    #Instantiating 2D matrix for noisy terrain values
    terrain = [[0 for j in range(rws)] for i in range(cols)]
    
    #Creating noisy values for terrain
    for y in range(cols):    
        for x in range(rws):
            terrain[x][y] = map(noise(x*0.1,y*0.1+flying),0,1,-100,100)
    
    #Creating noisy mesh
    mesh = createShape(GROUP);
    for y in range(cols-1):
        m = createShape()
        m.beginShape(TRIANGLE_STRIP)
        for x in range(rws):
            m.vertex(x*scl,y*scl, terrain[x][y])
            m.vertex(x*scl,(y+1)*scl, terrain[x][y+1])
        m.endShape()
        mesh.addChild(m)
    
def draw():
    global flying
    
    #Display settings
    background(0)
    stroke(255)
    noFill()
    
    # Coordinate transformations
    translate(width/2,height/2)
    rotateX(PI/3)
    translate(-w/2.0,-h/2.0)
    
    #Calling the noisy mesh
    shape(mesh,0,0)
    
    #Flying parameter
    flying -= 0.05         

```

Which creates multiple `TRIANGLE_STRIP` PShapes and adds them to a `GROUP`. I think my initial assessment assumed that `beginShape(TRIANGLE_STRIP)` and `m.beginShape(TRIANGLE_STRIP)` would behave similarly. Perhaps someone would clarify at a later point.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 5, 2018, 1:54pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/9 "2018-08-05T13:54:42Z")

</div>

Maybe each call to the global PApplet::**beginShape()** method already adds itself to an internally pre-created **createShape(GROUP)**? 🤔

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [August 5, 2018, 3:13pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/10 "2018-08-05T15:13:49Z")

</div>

@GoToLoop, @WakeMeAtThree

> Anyways, just made a workaround version using `createShape(GROUP)` in order to separate each `beginShape(TRIANGLE_STRIP)` child’s scope

Hmm, interesting…That’s the first thing I tried before asking for help here but that solution didn’t work on my computer: Processing was crashing just after hitting the “run” button.

Anyway, thanks both of you. I’m sticking with the slower solution for the moment, computing the mesh in `draw()`  
.

Edit: the problem occurs on both version 3.3.7 and 3.4

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 5, 2018, 3:26pm UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/11 "2018-08-05T15:26:05Z")

</div>

> [@solub](#):
>
> Processing was crashing just after hitting the “run” button.

> [@solub](#):
>
> The problem occurs on both version 3.3.7 and 3.4

My sketch’s working on a 64-bit PDE 3.4 + Py Mode 3040 w/ a Win 8.1 AMD E2-3800 APU laptop. 💻

---

<div class="post-metadata">

**Author:** ![jb4x](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jb4x/32/789_2.png) [@jb4x](https://discourse.processing.org/u/jb4x)\
**Post date:** [May 5, 2019, 9:24am UTC](https://discourse.processing.org/t/how-to-prevent-a-pshape-object-to-close/2340/12 "2019-05-05T09:24:03Z")

</div>

Hi,

So I ran into the same issue.  
I couldn’t find any open issue on github so I created this one:

> <https://github.com/processing/processing/issues/5854>
