Get array of vertex's from loaded 3D Model?

So, I was wondering if there is a function or a library which allows you to access an array of vertex’s from a loaded object.

For instance if you used the LoadShape() function to import a .obj file of a cube, you could then do something to get all the vertex points of this cube as an array?

Many thanks

I don’t know about this

but loadShape() / Reference / Processing.org

gives you PShape / Reference / Processing.org

then either

or you have to implement: getChild() / Reference / Processing.org

can you provide an example obj file?

Chrisir

I was able to fix it!

  1. I meant loadShape() not loadModel() - To used to using p5.js and all its terms so apologies for that!

  2. Thank you for pushing me over to the PShape, especially the getChild() function. Turns out when an object file is loaded in each face of the object is a child PShape of the object. Was able to make the code bellow which takes an imported obj model and returns a PVector array of each vertex:

PShape inputModel;
PVector[] vexArray;
int vertexCounter = 0;

void setup() {
  size(500,500,P3D);
  inputModel = loadShape("complexCubeTri.obj");//Change your obj file here
  
  // Works out the number of Vertex's in overall object
  for (int i = 0; i < inputModel.getChildCount(); i++) {
    for (int j = 0; j < inputModel.getChild(i).getVertexCount(); j++) {
      vertexCounter += 1;
    }
  }
  
  // Creates new array the size of the number of vertex's in the object
  vexArray = new PVector[vertexCounter];
  
  // Add all vertex's to new array
  int iter = 0;
  for (int i = 0; i < inputModel.getChildCount(); i++) {
    for (int j = 0; j < inputModel.getChild(i).getVertexCount(); j++) {
      PVector newVex = inputModel.getChild(i).getVertex(j);
      vexArray[iter] = newVex;
      iter+=1;
    }
  }
}

void draw() {
  viewingSettings(); //Settings to help view the object
  
  beginShape(TRIANGLE);
  for (int i = 0; i < vexArray.length; i++) { // Iterate through all vertex's in array
    vertex(vexArray[i].x,vexArray[i].y,vexArray[i].z);
  }
  endShape();
}

void viewingSettings() {
  background(230);
  translate(width/2,height/2);
  rotateX(radians(frameCount/10));
  rotateZ(radians(frameCount/20));
  scale(10);
  strokeWeight(0.1);
}

‘vexArray’ is where all the vertex’s are stored in this case!

When importing an object I recommend either finding or changing it to have triangulated faces so each side is made of a maximum of 3 vertex’s, it seems to render better. Unfortunately this process also repeats vectors for each side so I will need to see about fixing that now!

Many thanks again,
Keir

1 Like