Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Blender script to add UVspheres along with curve's location and length.
def addUVsphereOnCurves(number,scale=0.1):
import bpy
import mathutils
from mathutils import Vector
objectList = []
curveList = []
lengthList = []
locationList = []
#---------Function to get length of curves------------
# Usage: After select one curve to be active, get_length(bpy.context).
def get_length(context):
obj_name_original =
# the duplicate is active, apply all transforms to get global coordinates
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
# convert to mesh
bpy.ops.object.convert(target='MESH', keep_original=False)
_data =
edge_length = 0
for edge in _data.edges:
vert0 = _data.vertices[edge.vertices[0]].co
vert1 = _data.vertices[edge.vertices[1]].co
edge_length += (vert0-vert1).length
# deal with trailing float smear
edge_length = '{:.6f}'.format(edge_length)
# stick into clipboard
context.window_manager.clipboard = edge_length
bpy.ops.object.delete() = context.scene.objects[obj_name_original] = True
#Added return to get value.
return edge_length
#---------Make object/location/length lists of curves-------------
# Object list of curves.
def makeObjectList(number):
for i in range(number):
if i < 1:
elif 1 <= i < 10:
objectList.append(["Curve.00" + str(i)])
objectList.append(["Curve.0" + str(i)])
# Make curve list because can't switch 2D to 3D. can do.
def makeCurveList():
for i in objectList:
#Switch 3D from 2D of curve not to get error in get_length.
def switch2dTo3d():
for i in curveList:
i.dimensions = '3D'
# In for loop of objectList. Firstly deselect all object. Activate one object. Select this object. Gettin length of curve, deselect it.
def executeGetLength():
for i in objectList: = i = True
lengthList.append(get_length(bpy.context)) = False
# Return from 3D to 2D.
def switch3dTo2d():
for i in curveList:
i.dimensions = '2D'
# Make locationList of curve from objectList.
def makeLocationList():
for i in objectList:
#--------Add objects where curves are----------
# Add UVsphere along with locationList. size come from lengthList.
def AddUVsphere(scale):
for i,d in enumerate(locationList):
# divide size by 10 for too large if default.
bpy.ops.mesh.primitive_uv_sphere_add(location=d, size=float(lengthList[i])*scale)
# Execution function
# Number is curve's number in order like Curve ~ Curve.065. Scale is scale of UVsphere.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment