Create mesh from list

Create mesh from list

Anonymous
Not applicable
3,358 Views
1 Reply
Message 1 of 2

Create mesh from list

Anonymous
Not applicable

Is it possible to create a mesh in Python from a list? For example, I have a list of vertices in a list that I know will make up a cube. How do I loop over the list and creat each vertex in turn?

 

 

import maya.cmds as cmds
myCube = [
-0.5,   -0.5, 0.5,
0.5,   -0.5, 0.5,
0.5,   0.5, 0.5,
0.5,   0.5, 0.5,
-0.5,   0.5, -0.5,
0.5,   0.5, -0.5,
-0.5,   -0.5, -0.5,
0.5,   -0.5, -0.5
]

for x, y, x in myCube: # I have no idea

  create vertex (x,y,z) # This is just a guess

 

At the emoment I'm just interested in creating each vertex. I'll fill in the faces later by hand.

 

0 Likes
3,359 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable

Hi

 

AFAIK, maya does not provide a specific class to create just a bunch of vertices.

All vertices are instances of the Maya.MPoint() or Maya.MFloatPoint() classes in the Maya API.

So you cannot say something like SomeVertexClass.createVertex() and expect to see a vertex in the viewport.

However, if you know the location of you vertices, you can use other APIs to create your mesh.

For example, you could use MFnMesh class from the Maya API to create a mesh as follows.

(The Maya documentation is very detailed so please refer to the link to know details of the API)

 

# using Python API 2.0 for Maya
import maya.api.OpenMaya as om

meshFn = om.MFnMesh() # list of vertex points vertices = [om.MPoint(-0.5, -0.5, 0.5), om.MPoint(0.5, -0.5, 0.5), om.MPoint(0.5, 0.5, 0.5), om.MPoint(-0.5, 0.5, 0.5), om.MPoint(-0.5, 0.5, -0.5), om.MPoint(0.5, 0.5, -0.5), om.MPoint(0.5, -0.5, -0.5), om.MPoint(-0.5, -0.5, -0.5)] # list of number of vertices per polygon # A cube has 6 faces of 4 vertices each polygonFaces = [4] * 6 # list of vertex indices that make the # the polygons in our mesh polygonConnects = [0,1,2,3,1,6,5,2,7,6,5,4,3,2,5,4,3,0,7,4,0,1,6,7] # create the mesh meshFn.create(vertices, polygonFaces, polygonConnects )

For a simple cube like the one you provided in your question though, I wouldn't bother with the API and just use the polyCube() command to draw the cube.

 

import maya.cmds
cmds.polyCube()