Maya Python Draw Fold Plane

by Bounding main Tian Qiu

Every bit my first post in the web log, I'd like to share some initial studies on my own Voronoi shatter plug-in. Instead of API, I only used python scripting at this signal. Only try to prove how easy information technology can be to create a Voronoi plug-in in Maya, and fifty-fifty visualize the construction of the chunks to the viewers. To add that, the cost of choosing a simple linguistic communication is the slow running/functioning of a tool. And so API is actually ever the best choice to do a user-based plug-in.

I know there're already bunch of slap-up shattering plug-ins such equally PullDownIt, DMM, FractureFX, etc. I will still get on this topic, since these scripts are somehow basic and important to a CG technician, but rare people demonstrate it. Hope this can inspire y'all doing your ain tools.

Get-go I'd like to thank Eduardo Cabrera Lachuga, my friend and an excellent Houdini TD. He introduced me the ways of the object shattering, and inspired me a lot on programming my own tools.

Bullet Shatter Script

Bullet shattering is one of the usual ways that scripters use the Voronoi algorithm into maya. The normal users volition likewise often do like this:

ane. Go to "Cut Faces Tool Options", Bank check on "Extract cut faces" and set "Excerpt get-go" to 0 0 0, then that we can actually create double edge borders on the cut edges, in gild for us to close the pigsty later.

voronoi3

2. Cutting the Faces on the object, in some random directions.

voronoi4

iii. Become to "Mesh>Make full Hole", maya will automatically create a node called "polyCloseBorder", which is going to create a face to close our border edges of each chunks. This is also the MEL command that we are going to use later.

voronoi5

iv. Select the object and Become to "Mesh > Seperate".

voronoi6

So if translating the user steps into the python scripting, it will be similar this.

voronoi8.JPG

I prepare the random points in the bounding box and cut the object based on these points, then set the random angles for polyCut to cut, imaging at that place'southward an actual airplane to cut it off.

And information technology turns out successful and fast to cut an object. But it cannot shatter multiple objects (that'south an like shooting fish in a barrel fix though), and doesn't have any textile assigned to the cut faces (will gear up this later, since information technology doesn't matter with the principal shattering function at this indicate).

voronoi9

(Script Download: dropbox.com/due south/870wopv65jpygsa/demonstrate_01.py?dl=0 )

Another important way to do the polyCut is: instead of checking on "Extract cut faces", nosotros can get-goindistinguishable the object several times every bit much as the number of points, and for each object nosotros duplicate,  practise the polyCut with but "Delete Cut Faces" checked-on. Here'another blazon of polyCut that I talk about.

voronoi10.JPG

Afterward closing borders for each duplicated cut objects, I could as well become the chunks.

But the bullet shatter script cannot create an organic kind of shatters, and that'southward why nosotros are going to consider applying the Voronoi Algorithm into our script, since Voronoi Diagram seems more similar shattered chunks.

Voronoi Algorithm

Let'southward presume there are a number of random points on a plane, and we want to know in what area, its points are closest to a certain random point. And there came out the Voronoi Diagram. To summate these areas, we have to draw out Perpendicular bisectors between any two points. And these lines define the borders of each area. The number of points is also going to be the number of chunks. This is all what we need to know for Voronoi.

voronoi00 voronoi

Perpendicular Bisector Aeroplane

For 3D Voronoi, instead of creating perpendicular bisectors, nosotros consider perpendicular bisector planes for polyCut.

Offset, I placed a number of random points inside bounding box ,  which I did it in the last bullet shatter script. Second, I need the center points of each pair of different random points, to exist the origin of the polyCut "plane". Third, I have to find a vector which is the 3d direction of the perpendicular bisector, in orderto set up the plane'southward 3d-world angle.

To find out the center indicate of each pair of different random points, we need double loops  to compare them one to anthor, and a conditional statement to make certain they are non counting themselves. Formula: V0 = (V1 + V2) / 2

voroCenter = [(vec1 + vec2)/2 for (vec1,vec2) in zip(voroTo,voroFrom)]

voronoi11

And and then I need an "aim" vector to store the each line direction of each pair of points, which has formula V0 = V1 – V2.

aim = [(vec1-vec2) for (vec1,vec2) in zippo(voroFrom,voroTo)]

To find a vector which is perpendicular to the "aim" vector, I used angleBetween to compare it to the vector <<0,0,1>>. Formula: X1X2 + Y1Y2 + Z1Z2 = 1

planeAngle = cmds.angleBetween( euler=True, v1=[0,0,1], v2=aim )

So the core script is like this:

voronoi13.JPG

And now I am going to practice the polyCut. Of the two ways that I mentioned above, really but the 2nd 1 works in this case. Let's test them out.

Method 1) In "Cut Faces Tool Options", Bank check on "Extract cut faces" and prepare "Extract offset" to 0 0 0. Close the borders. And seperate them out.

cmds.polyCut(object, ef=Truthful, df=False, eo=[0,0,0], pc=voroCenter, ro=planeAngle)

voronoi14

Later on testing… It is definitly a disaster. So this method doesn't work

voronoi15

Method 2) Firstduplicate the object several times as much as the number of points, and do the polyCut with only "Delete Cut Faces" checked-on for each object.

workingObj = cmds.duplicate(str(object))

cmds.polyCut(workingObj[0], ef=False, df=True, pc=voroCenter, ro=planeAngle)voronoi18

voronoi17

At present it works so fine. This is about to finish the core script. To add together that, it'southward also necessary  to hide the original object and proceed the chunks' visibility on, too make a group for these chunks.

cmds.setAttr(object+'.visibility',0)  # Set the original object'southward visibility off
chunksGrp = cmds.group( em=True, proper name = object + '_chunks_1′ ) # Brand an empty group

workingObj = cmds.indistinguishable(object)
cmds.setAttr(str(workingObj[0])+'.visibility',one) # Prepare the chunks object's visibility on
cmds.parent(workingObj, chunksGrp) # Parent the chunks to the group

cmds.xform(workingObj, cp=True) # Center Pivot each chunk

cmds.xform(chunksGrp, cp=True) #Center Pivot the grouping

voronoi19

And pretty much it…

voronoi20

(Script Download: dropbox.com/s/p99bvio1bm6rfra/demonstrate_04.py?dl=0)

Cut Faces Material

At this point, I consider creating a material to the cut faces. Instead of directly applying to the faces, I want to write a office tin create a cloth, and define the RGB color. So I only translate the steps we usually created materials, into the python function.

voronoi23

And then then, we only have to apply the control below to use this textile to the cut faces.

cmds.sets(cutFaces, forceElement = (surfaceMat + 'SG'), e=True)

At present I accept to ascertain the cutting faces of each chunks, which is the hardest part of this script I guess. Since computer merely reorganized face numbers, we take to figure the principle out. Thinking of the cut faces, they are the new faces on the chunks. Assuming we accept a cube which has six faces, and we polyCut them to half and one-half, with ii holes,  now each "chunk" has 5 faces (without closing the edge). Later closing the edge, each chunk has the cutting face and total half dozen faces. So newFaces = facesBeforeClosingBorder – facesAfterClosingBorder.

oriFaces = cmds.polyEvaluate(workingObj, face up=True)

cmds.polyCloseBorder(workingObj, ch=False)
aftFaces = cmds.polyEvaluate(workingObj, confront=True)

newFaces = aftFaces – oriFaces

But this newFaces is just the full number of cut faces, which is not how Maya recognized the faces like "obj.f[vi]". Just we know that the cutting face up will be the newest/largest number of the faces, after each cut.

cutFaces = ( '%d.f[ %due south ]' % (workingObj[0], (aftFaces + newFaces – i))
cmds.sets(cutFaces, forceElement = (surfaceMat + 'SG'), e=True)

Okay and so the whole script is like this:

voronoi24

And a test:

voronoi22

(Script Download: https://www.dropbox.com/s/aakrsjl2l5j6rmy/demonstrate_06.py?dl=0)

Progression Bar and Visualize the Progress

At this point, I've already institute my Maya has to generate chunks for a while, even information technology only has twenty pieces. The adjacent thing is to add a progression bar to rail our construncion of chunks and visualize information technology. To mention that, the progression bar will even deadening downward our shattering progress and make information technology even heavier, only at this bespeak, we desire to show the progress to the customers.

Here is the basic construction of the progression bar script:

cmds.progressWindow(title = "Voronoi Computing", progress = 0, status = "Shattering…", isInterruptable = True, maxValue = numPoints)

cmds.undoInfo(country = False)

# Of course you don't want user to undo any stuff in the progress of shattering

footstep = 0

for voroTo in voroPoints:

     if cmds.progressWindow(q=Truthful, isCancelled=True ): break
if cmds.progressWindow(q=Truthful, progress=True ) >= numPts: break
stride = footstep + 1

     cmds.progressWindow( edit=True, progress=footstep, status=("Shattering step %d of %d completed . . ." % (pace, numPoints)) )
cmds.refresh()

cmds.progressWindow(endProgress=1)
cmds.undoInfo(state = True)

voronoi25

And after adding this to my script, it works the way I want except the slow progression.

voronoi26

voronoi27

Script: https://www.dropbox.com/due south/k2eghg5uw92vmms/demonstrate_08.py?dl=0

In the adjacent postal service, I will prove how to brand a "crack" part and a uncomplicated GUI. And see any  improvement that can do.

(Please requite credit to the original author when you use/quote the commodity elsewhere.)

crosslinhoremill.blogspot.com

Source: https://oceanqiu.wordpress.com/2016/04/21/maya-python-how-to-create-a-voronoi-shatter-plug-in-by-only-scripting-1/

0 Response to "Maya Python Draw Fold Plane"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel