# Blender Python Script issues, can someone explain the script?

I'm trying to model Hypoid Gears for a model that I'm making. I'm doing the model in AutoCAD/Inventor, but the only decent free tutorial that I've found for modeling Hypoid Gears is meant for Blender. Most of the steps from the tutorial can be done in AutoCAD, but there's this one part where they use a Python script to transform a flat gear pattern into a cone shape for the gear. I'm having difficulty understanding how the script works since I don't know Python. Would someone be able to explain how the script is transforming the gear sketch so I can try and recreate it in CAD.

This calculator made the following script:

import bpy
from math import *

obj=bpy.context.object
mesh=obj.data
for i in range(0, len(mesh.vertices)):
vert=mesh.vertices[i]
x=vert.co[0]
y=vert.co[1]
z=vert.co[2]
R=3.970
alpha=14.031*pi/180
r=sqrt(x*x+y*y)
teta=atan2(y, x)
rnew=R+(r-R)*cos(alpha)
xnew=rnew*cos(teta)
ynew=rnew*sin(teta)
znew=(r-R)*sin(alpha)
vert.co[0]=xnew
vert.co[1]=ynew
vert.co[2]=znew

• can you give me the parametric equation needed in the beginning? Commented Aug 26, 2022 at 6:45
• how would you go about recreating it in autocad? does autocad support scripting as well? the script basically goes through all the vertices of the shape and moves them in a bowl-like shape. you could just do it in blender and export the shape as obj and import in autocad for you to continue. is that an option for you? Commented Aug 26, 2022 at 6:47

Your script basically just loops through all the vertices in the shape on the flat $$xy$$ plane (where $$z=0$$) and transforms each vertex to a new $$xyz$$ position with a z-elevation of $$alpha=14.031$$ degrees and increases the distance of each vertex from the center from $$r$$ to $$rnew$$ units (meter is the default unit in Blender).

Here's a breakdown of the script with visualization. From the top view we have a $$theta$$ calculated from $$\arctan2(y/x)$$ for each vertex. And $$r$$ can easily be calculated from the Pythagorean Theorem.

And we have $$rnew$$ calculated from $$alpha$$. Then same $$theta$$ is used to extend and calculate for $$xnew$$ & $$ynew$$ and $$znew$$ calculated from $$alpha$$. Note that $$14.031$$ degrees is equal to $$0.244887$$ radians.

Also note that $$rnew$$ and $$z$$ will vary because of hard-coded $$R=3.97$$. In this case $$z$$ is negative and $$rnew$$ is larger than $$r$$. If your gear is significantly larger you get a positive $$z$$ value and a $$rnew$$ lesser than $$r$$ value for each vertex.

You can easily create your Hypoid Gear's profile with Blender using the XYZ Math Surface. First make sure to have Extra Objects addon enabled in menu Edit > Preferences > Add-ons and search for Extra Objects then tick the checkbox.

Go to menu Add > Mesh > Math Function > XYZ Math Surface and notice that in the lower left corner if you expand the operator panel, you can input your parametric equations.

Take the parametric equations for $$x,y,z$$ you have generated from your site including $$Umin,Umax,Ustep$$

And enter the information in the operator panel. Notice this panel disappears if you click somewhere else. Just press F9 to bring it back up or add the XYZ Math Surface again.

Then go to the Scripting Tab and press New in the Text Editor and paste your script. Make sure you have selected your flat object and press the Run Script button.

Then you can export your object with File > Export > Wavefront OBJ (.obj) and import it into AutoCAD to continue modeling there.

• Outstanding answer ! Note a single simple deform modifier with the right settings will achieve the same result in 2 or 3 lines of code :) Commented Aug 26, 2022 at 12:37
• @Gorgious hey thanks! yeah. i think though he might be on the safer side if he uses the script he generated to be 100% exact since he will 3D print it. or can this be exactly reproduced with the 3 lines of code? can u post it as alternative answer? would most likely be interesting for the OP because he tries to replicate this in AutoCAD Commented Aug 26, 2022 at 12:52
• Hehe I spoke a bit too fast it's actually harder than I thought :) I didn't read the Q carefully though, re needing it for autocad. Cheers Commented Aug 26, 2022 at 15:47