Math and Scripting of a Procedural Newton's Cradle Generator (Using Maya)
- dashamakarishcheva
- Feb 24, 2022
- 3 min read
Updated: Jul 7

The Maya Python Newton's Cradle exercise was extremely fun to attempt.
To challenge myself I decided to make as many of the code's elements inputtable as I could within my given time frame - so that an individual using the code could create Newton's Cradles with different sphere sizes, quantities, string lengths, etc.
This challenge created complexity, and though the code I created works it is in no way perfect. This is the breakdown of my thoughts and approach while working on this project.
The Code

This code is split into three functions. One which creates the Newton's Cradle based off of provided inputs, one which colors in the Newton's Cradle created with the specified prefix, and one which will kill the specified Newtons Cradle.
Expression

Creating keys would create unnecessary difficulties. The movement of spheres on the Newton's Cradle simply required an expression input on the sphere and string it was connected to.
The sin wave function was used to create the oscillating motion of a sphere on a string going back and forth. The group's pivot point is moved near the top of the string. On the first and last runs through of the code the created sphere group is assigned a sin expression. The degree rotation is clamped so that movement only occurs in the direction away from the center of the newton's cradle.
Container Poles

Rather than coding a formula for every single pole crated for the Newton's Cradle Container, I wanted to create a code that would create position switches for me automatically. I identified the output I needed ((x,y)s in image) and played with code until I got the result of four distinctly flipped positions. I am certain there is a better way of doing this, but I got what I needed so I accept my result.
The final is certainly a lot less lines than writing out each cylinder's creation would be.
Container Width

I created a challenge for myself when I allowed the user to submit the angle at which the string would rotate and the distance they could be moved apart from each other: When creating the container for the Newton's Cradle, I need to make the width encompass the entire cradle. How will I take the angular spread of the strings into account?

The answer was to use SOH CAH TOA to figure out the distance that the end of a string moved when it was rotated at the center. In this case the most appropriate calculation was sin, which I'd confirmed with a mathematician when looking for the solution to the following:
When bug testing the degree input the NC would break (as in image).
To figure out the problem I began printing the result from the sin calculation. What was causing the break were negative results. Placing an absolute value around the formula solved this issue.
Grouping

The code organizes the Newtons Cradle in the outliner: it groups strings and cylinders into groups, then combines them into the Cradle_Spheres. The container for the cradle is placed into it's own group, and then the entire cradle is packaged together. The assigned prefix (Dashas, in this case) for the cradle is applied to all elements created in the run of AdvancedNC(). The assignment of a prefix to the project allows for it to be easily killed, located, or replaced.
ColorNC()
This part of the code has two iterations and the one used here creates and applies an aistandardsurface shader onto all elements of the cradle created with a AdvancedNC() function (as identified with pfx). Spheres are made mtallic, whereas the strings, container, and container poles are given their own colors.