Using AI to Help Write Code for Tinkercad Codeblocks! (Part One)

Light Switch Cover Flowers Made Using Tinkercad Codeblocks

Tinkercad has a section called ‘Codeblocks’, which is exactly what it sounds like: blocks of code. You can drag and drop javascript snippets into place to write tiny programs that can do some really cool stuff, like repeat designs and shapes on models that might otherwise take hours or days to create.

I’ve made some pretty flower designs with it, but that was months ago, and I don’t remember how I did it. I think I just experimented until things looked interesting. This time I’m going in with a plan: I’ve got ChatGPT open, and it’s going to be my AI assistant!

I went to Tinkercad, selected 'Codeblocks' from the top menu, and went exploring. I probably should start there — it has a good selection of resources — but my brain is fried from asset tagging and all I want to do right now is make something cool!

I opened the 'Create' menu, and selected Codeblocks.

I want to make an egg shape. I can stretch a sphere, but it won’t have the requisite tapering that turns it from a stretched sphere into a proper ovoid shape. Without codeblocks, I would probably make it by creating the shape using layers of flat cylinders. Or maybe combine some other shapes.

I checked Tinkercad’s library first, to make sure I wasn’t reinventing the wheel. It does have an egg shape, in fact, but it’s rather blocky and a hair too bottom-heavy. So here goes:

First I’m going to tell Chat what I want: a 4-inch-high ovoid shape with the same proportions as an egg.

The prompt: “Hello Chat! Could you please use Tinkercad’s Javascript Codeblocks to write code to make a 4-inch high ovoid shape with the same proportions as an egg? When you post the code, can you also break down what the different parts of the code are doing? I imagine this would be done with disks aligned at the center, stacked on top of one another, and the disks vary in size, first getting larger, then (at a slower pace) getting smaller. Also, can you please keep the answers brief? I am putting this in a blog. Thank you :)”

“Here’s how the request you provided would likely break down into a working process for both you and your readers:”

It then gave me javascript and some flowery commentary, though the code wasn’t as useful as I had hoped, at first blush. For example, it gave me the fact that the height is four inches, ‘let height = 4;’, which looks like nothing in the codeblocks. Chat did, however, break the code down, with explanations after each bit. I should be able to figure out what I need regarding the blocks, with a little more help. The ode:

// Egg dimensions
let height = 4; // inches
let segments = 40; // number of stacked disks
let maxRadius = 1.5; // widest part of the egg

for (let i = 0; i < segments; i++) {
let t = (i * 2) / (segments - 1); // normalized height (0 to 1)
let radius = maxRadius Math.sqrt(t(2-t)); // egg curve: grows then tapers
cylinder({height: height / segments, radius: radius})
.translate([0, 0, i * (height / segments)]);
}

From Javascript to Codeblocks

I opened a new Tinkercad Codeblocks file.

The first order of business: to change the name from the horrid one it came with to something useful and descriptive. I clicked the blue pencil icon next to the randomly generated filename and replaced it with “The Egg”.

Then I took a look at what I had to work with: a grey workspace in the center, the workplane on the upper right, and a list of codeblocks on the left, along with a menu bar. There are buttons on the bottom suggestive of a media player, a set of undo and redo arrows on the top, and space for the filename.

I then inspected the menu bar on the left side of the screen. The codeblocks are organized into menu categories, each marked by a uniquely colored dot for easy reference. We want to add a comment, so we are looking for ‘Mark Up’, found below the grey dot on the codeblocks menu.

A click on that brings the mark-up menu to the top of the list, and ‘comment’ is the first block of code we can interact with. A click on the block and you can drag it over to the large grey space, which is something like a digital blackboard for the codeblocks. If you haven’t played with these yet, notice you can move it all over the place, drop it anywhere you want on the space, then drag and drop it somewhere else. I left mine close to the top.

A click on the word ‘comment’ inside the block and you can change the text! I changed it to ‘Egg Shape,’ changed it to ‘Egg Math,’ then changed it back to ‘Egg Shape’. So far so good. The comments, being from the light grey mark-up tab, are on grey codeblocks, so we’ll be able to find them easily later.

With the first line added, I moved on to the next: assigning 4 to the height. But how?

My next prompt to ChatGPT: “So, turning the code into what tinkercad has available in codeblocks, lets take it from the top. I was able to drag and drop a comment block, and click in that block to comment the code. Then i hit the first actionable line, where you assign 4 to the height. how do i use the codeblock to do that? what do i drag over?”

It replied with some praise (aw!), then segued into, “let’s get into Tinkercad Codeblocks! Since it’s a visual block-based coding environment, we need to use the available blocks to replicate the JavaScript code. For the first actionable line where you assign a value to the height, here’s what you’ll want to do:

Steps for Setting the Height in Tinkercad Codeblocks:

  1. Drag a "Variables" Block:

    • From the Blocks palette (on the left side of the Codeblocks workspace), you’ll find a section called Variables.

    • Drag the "Set [variable] to [value]" block into the workspace. This is equivalent to let height = 4; in JavaScript.

  2. Create the "height" Variable:

    • Click the blue [variable] in the block. This will allow you to create a new variable.

    • You can name it height (or something else if you prefer).

  3. Set the Value to 4:

    • Now, click on the [value] part of the block (the number inside).

    • Change it to 4 to match the 4-inch height you’re assigning.

I gave a quick look over the steps needed to set the height, then got started.

Setting Variables

The first step was to find the variable section in the blocks palette, which is marked by a light blue dot on the codeblocks menu. I found it, though I saw no obvious ‘set’ option. However, there was a blank text field in a spot labeled ‘create number variable’, so I typed ‘height’ to see what would happen.

Whoa, it worked! It gave me three code blocks of the same light blue color as the menu table for variables, indicating that these were variables.

The first block has ‘height’ and a checkbox. Whenever I check it, it shows a variable tab, listing height. The second and third codeblocks hold actions you can do to the height variable; box two has ‘Set height to 0’, while box three offers ‘Change height to 1’, with both the zero and one being changeable.

I dragged the second codeblock to the surface, where I put it a little ways down from the comment. I then clicked the 0 and changed it to a 4, so it now reads ‘Set height to 4’. Perfect.

I realized I needed to add another comment here, so I put a second comment down, then moved it up towards the other comment until they clicked in place. I then placed the height variable block underneath them, moving it up til it also clicked in place. I now had three neat rows of code.

After that, I made the next two variables the same way, naming them ‘maxRadius’ and ‘segments’. It took me a minute to figure out how to change the new variables, as it didn’t feel intuitive: the codeboxes labeled ‘Set’ and ‘Change’ each have a dropdown menu containing all the variables we added. After selecting for the right variable, drag a copy of the codebox onto the mat.

I filled in the appropriate parameters from ChatGPT’s suggestion earlier, then added comments. The variables set, it was time to get to the heart of the code.

Counting Layers

I then asked ChatGPT how to turn the for-loop it churned out into workable Tinkercad codeblocks for ‘(let i = 0; i < segments; i++)’. It said this: ‘Use “Repeat” or “For loop” block: for i from 0 to segments-1’. To write the code to add the loop, I need to use a repeat or for block, according to Chat.

I think it means to use the orange ‘Count with i from 0 to x by 1’, with ‘segments’ for x, so I’m trying that, and labeling ‘Counting Layers’ with a comment.

Normalizing Height

The next line, ‘let t = i / (segments - 1); // normalized height (0 to 1)’, apparently normalizes height, whatever that means. I think it will give an increasingly larger number from 0 to 1, representing the height. I’m going to create a variable t, the same way we created the other variables.

Then I’m going to the math codeblocks section, marked by a green dot. I’m picking a smooth oval shape that has room for a variable on one side, then a plus sign topping a drop-down menu, followed by room for a second variable. This block allows a person to add, subtract, multiply, or divide different variables. I’m slipping ‘segments’ into the left side, choosing a minus sign from the drop-down menu, then typing in the number one on the right, so I end up with ‘segments - 1’.

Again to the math codeblocks, with the same type of block, only this time we’re putting the variable i on the left, and the entire pill-shaped ‘segments - 1’ block on the right, with a division symbol in between them.

Lastly, I need to assign this mathblock as a value for the variable t. I went back to the light blue variable selection and selected the ‘set’ menu, choosing the ‘t’ variable option, then dragged and dropped that to the grey board, where I plopped it down, adding the rather long green math block to the right, so now it reads ‘set t to i / ( segments - 1 )’. I commented it too. Hope that works!

Note: it did not, as it should have read ‘set t to (i * 2)’, but I hadn’t yet realized that this particular line was ruining the program. I put the right solution in the code up above so that no one else has to spend literal days trying to work out what went wrong.

Growth then Tapering

So far, so good. Probably. Now for the scary bit. I’ve been winging it pretty much the whole way, but this is the first time I’ve felt properly intimidated. Just look at that square root function! Will this code even work? Time to find out! ‘let radius = maxRadius * Math.sqrt(t(2-t)); // egg curve: grows then tapers’. The code ChatGPT churned out had a blank space where I put a multiplication symbol. It got a little huffy about the empty space, and I’m assuming it’s supposed to multiply something here. It agreed, but then said something insensible. Hmm.

I grabbed a green cylinder-shaped math block, giving it the value of ‘2 - t’. Next, I pulled down another cylinder, placing the variable ‘t’ on the left, the multiplication symbol in the center, and the expression ‘2 - t’ on the right. Result: a green codeblock with the expression ‘t(2-t)’.

I found square root in the green ‘math’ codeblocks section, in the dropdown menu headed by ‘sin’. The oval was right under the basic maths one. I dragged out the square root codeblock, placing the expression we just made on the right-hand side of the oval. It is here that we would change the math to make it more bottom-heavy if we wanted. Maybe I’ll do that sometime. For now I just want to get this working :)

Now that we’ve taken a square root (Math.sqrt), we are ready to multiply the result by maxRadius. I pulled the variable maxRadius from the list, adding it to the left. I placed a multiplication symbol in the center, and added our square root expression to the right.

For the last part of this section, we’re going to assign our lovely equation to the variable ‘radius’. It needs to be created, but that’s quickly done. Afterwards we go to the ‘Set’ option under our list of variables, choose ‘radius’, and drag that codeblock to the mat. Next we drop our equation to the other side of the equal sign, and lo, we have assigned the variable. Hooray!

This last bit of code has something unfamiliar to me: the translate function. What is this even supposed to do? Maybe figure out how to print each disk slice by coordinates? It’s a good thing I’ve still got ChatGPT open — I have questions!

cylinder({height: height / segments, radius: radius})
.translate([0, 0, i * (height / segments)]);

According to Chat, the cylinder part makes a disk, and the translate portion tells it to go up the z axis. Of course, it’s written in Javascript and we need Codeblocks.

Step one in making our disk: making a cylinder. We’re going to the medium blue button, titled ‘Shapes’, and we’re going to pull a cylinder to the mat.

For the cylinder’s height, we need ‘height / segments’, so we’ll go to the math section and pull out one of the green expression makers. With the height variable added to the left, a division symbol to the center, and the segments variable to the right, the expression is complete. Then we slot our newly minted expression into the section labeled ‘H’.k

For the cylinder’s radius, we’re going to place our radius variable.

Next I’m going to pull out a move block from the purple modify menu, attach the cylinder to it, then fill out the Z axis information with a math block. I’m naming the Z axis math-derived variable ‘zaxis’, and it will be (i * (height / segments)).

To make zaxis, I’m starting with the green math expression codeblock, using the variable height, the division operator, and the variable segments, then I’m nesting that in another green expression oval, with ‘i’ on the left and the new expression on the right.

I have set the zaxis variable, added the move block, then added the cylinder. The order seems right, but I don’t really know. Another thing to find out when we’re done, which is… now!

On the right-hand side of the screen is a workplane; pressing the play button at the bottom will run the code, creating the object. You can zoom in to watch it build, and even speed it up with a fast-forward button.

This was only half-successful, topped by a tube. Time for part two: using AI to solve the problem (that it helped create).

Previous
Previous

Using AI To Help Troubleshoot Tinkercad Codeblocks! (Part Two)

Next
Next

Swapping Filament Colors Mid-Print: Layer-Based Color Change