Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import bpy
def dupliOnCurve(curve_length_effect=None, replace=None):
When you want to duplicate an object on object: Simply do "object > make links > object data".
The function is optimised for an object to duplicate on curves, althoubh it can be used to duplicate on objects.
1. Multiple selecte curves, and lastly select an object as an object to be duplicated.
2. Paste the code on text editor which can be found in scripting layout.
How intense the length of curve affect the scale of duplicated object. 0 is no effect.
Default is 0.
If True, the selected curves will be removed, and only the duplicated objects reimans.
If False, the selected curves won't be removed.
# Default value of curve_length_rate is 0.
if curve_length_effect == None:
curve_length_effect = 0
curve_length_effect = curve_length_effect
# Default value of replace is False.
if replace == True:
replace == True
replace = False
# Name of lastly selected active object to be duplicated.
active_object_name =
# Name list of selected curves/objects, except for lastly selected active object.
selected_object_names = [ for obj in bpy.context.selected_objects if != active_object_name]
# List of edge lengths for curves/objects.
edge_length_list = []
# Convert the selected curves into objects, and add the edge lenght to edge_length_list.
for selected_object_name in selected_object_names:
# Asign each selected object.
selected_object =[selected_object_name]
# If replace == False, the original curve/object is duplicated.
# (it's not duplication of selected object on curves, just for keeping original curves).
if replace == False:
# If it's curve, convert the curve into object.
if selected_object.type == "CURVE":
# calculate the lenght of edge, then append it to edge_length_list.
if curve_length_effect != 0:
edge_length = _getLength(selected_object)
# Calculate average edge length.
avarage_edge_length = sum(edge_length_list)/len(edge_length_list)
# Re-select and change their scale relfecting edge length.
for i, selected_object_name in enumerate(selected_object_names):
selected_object =[selected_object_name] = True
# Effect for scale = curve_length_effect * (its edge length - average edge lenght) - average edge length.
scale_rate = curve_length_effect * (edge_length_list[i] - avarage_edge_length) / avarage_edge_length
# When avarage_edge_length is 0, set scale_rate=0.
except ZeroDivisionError:
scale_rate = 0
# Change scaled of selected object into the same as scale of duplicated object + scale_rate.
selected_object.scale.x =[active_object_name].scale.x + scale_rate
selected_object.scale.y =[active_object_name].scale.y + scale_rate
selected_object.scale.z =[active_object_name].scale.z + scale_rate
# Re-activate the object which was selected as active object at first. =[active_object_name]
# Duplicate active object on the curves by linking the data to them.
# Duplicate curves to keep original curves.
def _makeDupli(selected_object):
bpy.ops.object.select_all(action='DESELECT') = True
# Make duplication.
# Convert curve into mesh.
def _curveToMesh(selected_object):
# Select and active only the curve per loop.
bpy.ops.object.select_all(action='DESELECT') = True = selected_object
# Convert curve into mesh.
bpy.ops.object.convert(target='MESH', keep_original=False)
# Calculate edge length of a curve.
def _getLength(selected_object):
bpy.ops.object.select_all(action='DESELECT') = True = selected_object
# Temporary duplicate it.
# Change origin to center to calculate the lenght easily.
bpy.ops.object.transform_apply(location=True, rotation=True, scale=True)
# Calculate the edge length.
mesh_data =
edge_length = 0
for edge in mesh_data.edges:
vert0 = mesh_data.vertices[edge.vertices[0]].co
vert1 = mesh_data.vertices[edge.vertices[1]].co
edge_length += (vert0-vert1).length
# When the length cannot be calculated, set 0.
edge_length = 0
# Delete temporal duplicated object.
return edge_length
# Execute.
dupliOnCurve(curve_length_effect=0.5, replace=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment