I’m trying to run some Python script in Blender 3.4.1. I navigate to the Blender 3.4.1 Scripting tab, which opens the Python Console. I am able to copy my Python code and paste into the Blender Python Console. However there are coding errors which I need to copy out and work on. Unfortunately, the clipboard won’t update from the previous copied code, even when I highlight the error code and right click the mouse to copy, as well as Ctrl+c.
I have tested with fewer lines of Python code and there’s no errors, so my code needs fixing, but there’s so much data and indentations that I need to copy out, rather than type it out and make a typo.
Here’s the code I’m pasting in:
import bpy
import bmesh
import math
def create_screw_4_40():
# Screw parameters (approximate for a 4-40 screw)
thread_pitch = 0.635 # 40 threads per inch ~ 0.635 mm
major_diameter = 2.84 # Major diameter in mm
minor_diameter = 2.40 # Minor diameter in mm
screw_length = 20 # Length in mm
head_diameter = 5.5 # Approximate head diameter in mm
head_height = 2.5 # Approximate head height in mm
# Create screw shaft
bpy.ops.mesh.primitive_cylinder_add(
vertices=32, radius=minor_diameter / 2, depth=screw_length, location=(0, 0, screw_length / 2))
screw_shaft = bpy.context.object
screw_shaft.name = "Screw_Shaft"
# Create screw head
bpy.ops.mesh.primitive_cylinder_add(
vertices=32, radius=head_diameter / 2, depth=head_height, location=(0, 0, screw_length + head_height / 2))
screw_head = bpy.context.object
screw_head.name = "Screw_Head"
# Create a helix for the threads
bpy.ops.mesh.primitive_curve_add(
type='CURVE', location=(0, 0, 0))
screw_curve = bpy.context.object
screw_curve.name = "Thread_Curve"
# Convert the curve into a screw thread
screw_curve.data.dimensions = '3D'
screw_curve.data.bevel_depth = 0.3 # Approximate thread depth
screw_curve.data.bevel_resolution = 4
screw_curve.data.fill_mode = 'FULL'
screw_curve.data.use_fill_caps = False
# Add screw modifier
modifier = screw_curve.modifiers.new(name="Screw", type='SCREW')
modifier.axis = 'Z'
modifier.angle = math.radians(360 * (screw_length / thread_pitch))
modifier.steps = 100
modifier.render_steps = 32
modifier.screw_offset = thread_pitch
modifier.use_merge_vertices = False
# Join objects
bpy.ops.object.select_all(action='DESELECT')
screw_shaft.select_set(True)
screw_head.select_set(True)
bpy.context.view_layer.objects.active = screw_shaft
bpy.ops.object.join()
print("4-40 screw created successfully!")
create_screw_4_40()
The “normal” way to do it, is to open the text editor - paste your code there and then just run the code. Or is there any special reason why you want to run that in the python console?
If you run the code, you will get this error:
Python: Traceback (most recent call last):
File “/Text”, line 57, in
File “/Text”, line 27, in create_screw_4_40
File “/Applications/Blender.app/Contents/Resources/4.5/scripts/modules/bpy/ops.py”, line 109, in call
ret = _op_call(self.idname_py(), kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: Calling operator “bpy.ops.mesh.primitive_curve_add” error, could not be found
You shouldn’t use the Python Console for that. What @Blender_Fun1 suggested is the better way.
In the Text Editor (inside Blender), press the ‘New’ button to create an new text datablock.
Then paste your code in the text area, and press the ‘Run Script’ button ( ).
You’ll see the errors and outputs in the System Console (so you should also have that opened). Then make the corrections to your code until it runs flawlessly.