Last active
August 29, 2015 14:08
-
-
Save squarednob/48ccf2bce9460381a9a6 to your computer and use it in GitHub Desktop.
Blender script to add UVspheres along with curve's location and length.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = context.active_object.name | |
bpy.ops.object.duplicate_move() | |
# 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 = context.active_object.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) | |
#print(edge_length) | |
# stick into clipboard | |
context.window_manager.clipboard = edge_length | |
bpy.ops.object.delete() | |
context.scene.objects.active = context.scene.objects[obj_name_original] | |
context.object.select = 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: | |
objectList.append(bpy.data.objects["Curve"]) | |
elif 1 <= i < 10: | |
objectList.append(bpy.data.objects["Curve.00" + str(i)]) | |
else: | |
objectList.append(bpy.data.objects["Curve.0" + str(i)]) | |
# Make curve list because bpy.data.objects can't switch 2D to 3D. bpy.data.curves can do. | |
def makeCurveList(): | |
for i in objectList: | |
curveList.append(bpy.data.curves[i.name]) | |
#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(): | |
bpy.ops.object.select_all(action='DESELECT') | |
for i in objectList: | |
bpy.context.scene.objects.active = i | |
i.select = True | |
lengthList.append(get_length(bpy.context)) | |
i.select = 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: | |
locationList.append(i.location) | |
#--------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 | |
makeObjectList(number) | |
makeCurveList() | |
switch2dTo3d() | |
executeGetLength() | |
switch3dTo2d() | |
makeLocationList() | |
AddUVsphere(scale) | |
# Number is curve's number in order like Curve ~ Curve.065. Scale is scale of UVsphere. | |
#addUVsphereOnCurves(number=65,scale=0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment