Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Draw a lot of planes programmatically using a text file with all the vertices

Draw a lot of planes programmatically using a text file with all the vertices

VansFannel
Enthusiast Enthusiast
227 Views
1 Reply
Message 1 of 2

Draw a lot of planes programmatically using a text file with all the vertices

VansFannel
Enthusiast
Enthusiast

Hi,

 

I want to draw this plane:

 

VansFannel_0-1701703922178.png

The problem is that I have to draw a lot and I have the coordinates in numeric format for each vertex. In other words, I have a text file with all the vertices coordinates.

 

So, I wondering if I can do that programmatically using MEL, Python or C++.

 

Is there a way to draw a lot of planes in that way programmatically?

 

Thanks!

 

 

0 Likes
228 Views
1 Reply
Reply (1)
Message 2 of 2

FirespriteNate
Advocate
Advocate

yes, this kind of thing is very easy to do in MEL or Python. (you could do it in C++ but that's probably 100x more complicated overkill).

 

First you just have to parse your text file, then you have tokenize the data somehow, depending on how it is formatted, then you have to create the plane based on that data. Each of these is pretty straight forward.

 

Obviously we don't know your text format, so this is an example file I'll use, saved as "d:/work/plane1.txt":

 

vtx 0:  -4, 0, -5
vtx 1:  -1, 0, -5
vtx 2:  -1, 0, -3.5
vtx 3:   3, 0, -3.5 
vtx 4:   3, 0, 1 
vtx 5:   1, 0, 1 
vtx 6:   1, 0, 0 
vtx 7:  -2, 0, 0 
vtx 8:  -2, 0, 2 
vtx 9:  -4, 0, 2

 

 

and here's an example in Python (as MEL is much weaker for doing this kind of stuff, (but still completely feasible if necessary)):

 

vtxPnts = []
# first, open our plane text file 
# (if we had several we could put this in a for..loop)
with open('d:/work/plane1.txt') as f:
    for line in f.readlines():
        # the exact code here depends on your text file formatting
        vtx, coord = line.split(':')
        point = [float(x.strip()) for x in coord.split(',')]
        vtxPnts.append(point)

# now we have the point array, create the polygon "plane":
cmds.polyCreateFacet(point=vtxPnts)

 

and it's basically as simple as something like this.

 

obviously it becomes more complex if your file is in a more complex format, or JSON, or you have MULTIPLE planes or additional data in there to handle (colours, materials, etc..) but this example just shows  you how simple it is to parse a file, extracty some data and create polygons in Maya.

I hope it helps.