|
contact |
|
01. 912 . 272 . 2209
davekcw@yahoo.com.sg |
|
   |
|
|
|
Mel/Python Matrix - Melscripting and Python in creating a visual spirograph |
|
|
|
This project covers the procedural generation of a geometric spirograph formation, using both Melscripting and Python as means to generate data meant for Renderman. This is essentially a first step into comparing the differences between the two possible workflows and also to list down the pros and cons between the two.
Notes!
This project may differ from the initial assignment writeup written for Professor Malcolm Kesson. It would have been edited to also reflect my later experiences, clearer tutorial techniques/codes and also later advancements made to the projects as well. |
| |
|
|
Introduction |
|
|
|

|
|
For the first assignement in the class, we are required to create visual matrix of geometrical shapes in specific alignments using the Mel langauge to express the idea of asethetic form. While the idea can be expressed in various specific placements and etc etc, I would like the use the opportunity to explore the idea of the mathematical spirographics as an exploration within the mathematical limitations and the technicality of the Mel language.
What I hope to achieve is the eventually create a procdeure/UI which can create and hopefully draw out in real time the result of any spirographic formula as the user desires, while also manipulation changes such as shifts in object space rotations, scale and possibly other object based attributes.
What is a Spirograph?
As some may or may not recall, a spirograph is actually a mathematical curve which results from the position of a point about the revolution of two circles. |
|
 |
|
|
 |
|
While it is commonly known for its beautiful mathematical patterns, it is actually meant to represent the orbitat path of plantery systems and the likes of it. As a kid, it will be most familar to us as indicated in the diagram below. A singular pen, two circular jigs with gear like teethings. We simply put the pen into the hole of the inner circle while twirling the piece along the inner circumference of the larger circle. This creates a huge variation of floral like mathematical patterns. Of which I will be using to create the Mel Matrix for this assignment. |
| |
The Spirograph Formula |
| |
 |
| |
For starters, the formula required for the spirograph can seem daunting at the very beginning. To simply put, the formula is as above... To explain its concept in a easier format, it is always wise to remember that a spirograph is a point orbiting a circle within the orbit of another circle! To further dumb down the equation, we need to understand that the spirography is determined by several important factors
To illustrate the point, I have draw the diagram as shown:- |
| |
 |
| |
► The radius of "A" the which marks the extents of the system.
► The radius of "B" the point which constantly moves along the circumference of A.
► The offset position of C which eats into the orbit of A, beyond that of B (not illustrated above).
► The angle/interval of the ploted point known as the resolution which determines the smoothness of the pattern. |
| |
In the diagram on the right, we see and understand that the line drawn between the 2 origins "O" and "O¹" is eqivalent to (R - r). Much like in astrology, as we compare the movement of planets in a sidereal or synodic period as it orbits around a star, this information tells us that the formula to determine the X and Y cartesian coordinates of "O¹" would be the same as
X(O¹) , Y(O¹) = (R-r) cos ℓ , (R-r) sin ℓ
Similarly, the same formula can be said to be applicable to the X and Y cartesian coordinates of "C" in relativity to the position of "O¹" :-
X(p) , Y(p) = (R-r) cos β, (R-r) sin β |
|
 |
|
| |
Lastly, bearing in mind that the orbital distance moved by C must also equate the orbital distance moves by "O¹ just as the distance moved by one wheel must match the overal distance traveled on the road, we need to bear in mind to match the revolutions of "O" and "O¹" in a relative formula. Where the distance travelled by "O" on the circumference of "O" must match the rotation done by "C" along the orbital circumference of "O¹".
Where the arc traveled by "C" - R ℓ must equate the arc traveled by "O¹" - r(ℓ + β) . Thereby linking the angle of revolution in the final formula :-
β = (R -r) ℓ/r
Remembering that the two important factors are how the position of the "pen" relates to the main Origin and the angle of which the points should travel along the circumference of "O"'s orbital circle, we can now begin to test and plot the results of the spirograph in the Mel matrix.
|
| |
|
|
The Mel Spirograph |
|
|
|

|
|
Now that we have completed a very detailed analysis of the formula required for the Spirograph math, we need to bear in mind some crucial elements which will make the work easier and maybe perhaps harder.
The Spirograph formula gives the absolute position of the point along the spiro curve just like how we use intervals or time to find the position of the Cartesian coordinates X and Y along a Sin or a Cos curve. While in Maya or Mel, we have the luxury of not having to completely rely on such math. Yes, because we can choose to position objects in relativity or the parent > child relationships.
Since I am going to focus first on the applicable aspects of the curve, I will choose to only observe the rules of a Spirograph curve and not entirely its math. The subject of which I will probably integrate later into the script (probably causing a change in its entirety) as an expression so that I can slide my objects along the curve in a hopefully beautiful animation.
And so speaking, I have reserved the following variables for the script:-
► $Lradius - will represent the radii of the large unmoving circle.
► $Sradius - will represent the radii of the small moving circle. (for now I will also include the "P" offset in this value.
► $Linterval - will control the orbital frequency/distance traveled between each movement based on the larger circle.
► $Sinterval - will determine the number of times the smaller circle will orbit in the entire cycle of the larger circle.
The entire code for the simple simulation is as below:- |
|
 |
| |
One crucial element worth noting would be the formula (((360*$Sinterval)/$Lcount)*$i). $Lcountt represents the number of times the script would take to make an entire 360 degree revolution around the origin. Because the secondary orbit takes its interval/phase from the status of its loop with the value of $i, it would be rational and expected that the heightened value of $Lcount will greatly quicken the effects of the smaller loop.
As mentioned in the earlier formula examination, in order not to allow the smaller orbit to "slip" off, the distance traveled along its circumference must match those of the larger orbit - aka the wheel rotation must match the distance traveled on the road. Therefore, $Sinterval which determines the number of rotations that the secondary orbit will make, is multiplies by 360 degrees - example, 4 turns of a circle = 360 * 4... and etc. This value is than divided against the TOTAL NUMBER of loop frequency made as the code attempts to create the entire 360 orbit along the larger Radii. Thereby giving you the relative formula. |
| |
 |
| |
And as you can see, the manipulation of the $Linterval and $Sinterval will result in different variations of the spirographic curve, but still it would maintain its fully controllable aspects and mathematical perfection. Lastly, below you will see even more variations of the same script, by manipulation the $Linterval and $Sinterval and the $Lradius and $Sradius of spirographic math. |
| |
 |
|
The Python Spirograph |
|
|
|

|
|
One thing that we have learnt about python is that Maya itself has a very similar set of commands meant for it vis the script module maya.cmds - also widely know as maya commands. While this module bridges the differences between maya and python, its key element lies in the fact that we can easily write any bridging program for this "less restrictive" form of programming language.
Sadly, it seems that anyone interested in programming python for Maya needs also to be very well familiar with Mel itself. while is something I find ironic... but then, the exercise performed here is meant to demostrate the capabilities of controlling Maya with Python such so that Python itself can easily export scripts outwards and thus becomes a universal "bridge" between 3d editing programs such as Max and Houdini.
The Comparsion...
Since I have already explained the concept of the Spirograph Matrix, I am going to focus on the comparision between Mel and Python... The first step of my Matrix code comes of course with first destroying all objects in the scene. With the native Maya command, I am using the SelectAll command followed by a doDelete - which of course can be replace by the actual select -all and delete. But skipping that point however, while in Python, I must first import the script module called Maya and load up its attribute called cmds. Then, by calling out component attributes of the cmds, I am able to use Maya equvalent commands such as select() and delete(). |
|
 |
| |
Next, we proceed on with the loading of variables to control the use of the matrix script. However as most of the variable setup remains the same, I shall perform the comparision of the codes between a smaller section of the actual variable declaration. |
| |
 |
| |
With one look, we start to see a pattern in between Maya and Python. We can see how the simple use of commentary markers has changed from Maya's // and /* to Python's # which is similar to what we have in the RIB commands of RSL Renderman. Next, we also realize that we no longer need to use ; to seperate commands in Python after each command line as was in Mel. This is because Python itself can read special informations such as Indents and Line Breaks for its coding.
Lastly, we see that we no longer need to declare float in python. This is because Python's variable storage is by itself extremely flexible and can store any type of information as required without needing to change it variable class. To simply put, the variable itself is a universal class on its own that can store boolean, integers, floats and strings...etc. We not only need no longer declare the data type, we also need not include a $ sign to specify them either. While to me, these can be quite dangerous and inconvenient. Considering that now I have do use a int() command to create integers and will find myself confused by the numerous command names thanks to Python's customizable modules and variables that can de declare as almost any name without special markers to identify them by. Since programs do not color code self-declared modules such as maya.cmds and we will find it hard to identity variables from commands too. |
| |
 |
| |
And next with loops, we realize that Python's loop is much simpler and clean as compared to Mel Scripting. While Mel resembles the Loop statements of C++ Programming where 3 seperate codes are used to declare $i, state the limits of the loop and increment the step of $i, Python itself has a closer remsemblance to Visual Basic with a simple till till range concept. Although Python itself still allows us to setup the precise starting point, ending value and increment step, it has a cleaner look which is earlier to intrepret. Lastly, we also notice that instead of using parenthesis such as {}, python itself begins the loop statement with a : and only ends the loop through a parent / child concept in indentations just as we see bullet points in my normal letter writing. |
| |
 |
| |
Lastly, we move on to the main Spirograph generation code. At a glance, we can immediately see how much more cluttered it is compared to Mel... Well, this is after all a form of "Bridge"... While perhaps the declaration of variables and mathematically tools in Python might be cleaner and more advanced, we certainly do not expect it to perform better in Maya compared to of course Maya's own native programming language. One thing for certain, we can see how most of the commands are actually the same. How polySphere in Mel equates cmds.polySphere in python - with the exception of the declaration of flags of course...
But the key quirks likes in the odd declaration of rotation... the reason being the need to add "deg" in any written values except for zeros and variables/data. Lastlym we can also see that I have added an addition rotcount variable in the Python code to simplify the following cmds.rotate command. After all, we have to be extremely careful with the way we use indentations, spaces and line breaks in python... a small problem which can quickly cause the code to be confusing to our naked eyes.
And with that step done, the python code will now generate the same matrix as was in Mel.... Viola conversion completed. With that, I continued to add a new "procedural" header to it and completed the file...
The end result of the file can be found here. |
| |
|
|
|