Page 1 of 1

### Creating objects optimally

Posted: Sat Jun 08, 2013 1:18 am
What would be the fastest way to allocate 835902500 vertices to 33144 different objects (a single object may have multiple meshes, or multiple splines)

Code: Select all

``````"""
This script reads shapefiles into blender.
"""
import csv
import os
import sys
import shapefile

def partitionParts(allPoints,partList):
components=[]
for i in range(len(partList)):
if( i != (len(partList)-1)): components.append(range(partList[i],partList[i+1]))
if( i == (len(partList)-1)): components.append(range(partList[i],len(allPoints)))
subsets=[]
for n in range(len(partList)):
subsets.append([allPoints[i] for i in components[n]])
return(subsets)

def vectorize(pointList2D):
for i in range(len(pointList2D)):
pointList2D[i]=list(pointList2D[i][0:2])
pointList2D[i].append(0.0)
pointList2D[i]=Vector(pointList2D[i]) #pointList2D is now actually a list of 3D vectors

def plot_ShapeRecord_object(shapeRecord_object):
fails={"index":[],"name":[]}
xx=shapeRecord_object
try:
name=str(xx.record[6])
cu=bpy.data.curves.new(name,'CURVE')
ob=bpy.data.objects.new(name,cu)
if len(xx.shape.parts)>1:
y=partitionParts(xx.shape.points,xx.shape.parts)
for i in y:
vectorize(i)
spline=cu.splines.new('BEZIER')
spline.use_cyclic_u=True
spline.resolution_u=1
cu.resolution_u=1
cu.render_resolution_u=1
cu.dimensions='2D'
for k in range(len(i)-1):
spline.bezier_points[k].co=i[k]
if len(xx.shape.parts)==1:
y=xx.shape.points
vectorize(y)
spline=cu.splines.new('BEZIER')
spline.use_cyclic_u=True
spline.resolution_u=1
cu.resolution_u=1
cu.render_resolution_u=1
cu.dimensions='2D'
for k in range(len(y)-1):
spline.bezier_points[k].co=y[k]
bpy.context.scene.objects.active=bpy.data.objects[name]
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[name].select=True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.curve.select_all(action='SELECT')
bpy.ops.curve.handle_type_set(type='VECTOR')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
print(name + " has been initialized.")
except:
print(name + " FAILED TO INITIALIZE.")
fails["index"].append(sf.index(xx))
fails["name"].append(name)

#######
# os.chdir('D:/Documents and Settings/******/Desktop/[blender work]/shapefile')
# sf=sf.shapeRecords()
#
#
#
# def drawShapefile(location):
"""
Call the pairs elements:
(spline_a, spline_b)
"""
import itertools
def seperate_overlaps(list_of_curve_objects,error=.00005):
for an_object in list_of_curve_objects:
try:
for pair in itertools.combinations(an_object.data.splines[:],2):
for index_a, element_a in enumerate(pair[0].bezier_points[:]):
for index_b, element_b in enumerate(pair[1].bezier_points[:]):
if element_a.co == element_b.co:
print(index_a,index_b)
direction_a=(pair[0].bezier_points[index_a-1].co-element_a.co) + (pair[0].bezier_points[index_a+1].co-element_a.co)
direction_b=(pair[1].bezier_points[index_b-1].co-element_b.co) + (pair[1].bezier_points[index_b+1].co-element_b.co)
if direction_a==direction_b:
element_a.co+=direction_a.normalized()*3
elif direction_a != direction_b:
theta=direction_a.angle(direction_b)
s=sqrt((error**2)/(2-2*cos(theta)))
element_a.co+=direction_a.normalized()*s
element_b.co+=direction_b.normalized()*s
except:
print(an_object)
print(pair)
print('\t',index_a,element_a)
print('\t',index_b,element_b)

#
location='E:/shapefiles/tl_2012_us_zcta510/tl_2012_us_zcta510.shp'
# os.chdir(location)
# sf=sf.shapeRecords()
fails={"index":[],"name":[]}
for ii in range(sf.numRecords):
xx=sf.shapeRecord(ii)
try:
name=str(xx.record[1]) #zipcodes are at 1, names are at 6
cu=bpy.data.curves.new(name,'CURVE')
ob=bpy.data.objects.new(name,cu)
if len(xx.shape.parts)>1:
y=partitionParts(xx.shape.points,xx.shape.parts)
for i in y:
vectorize(i)
spline=cu.splines.new('BEZIER')
spline.use_cyclic_u=True
spline.resolution_u=1
cu.resolution_u=1
cu.render_resolution_u=1
cu.dimensions='2D'
for k in range(len(i)-1):
spline.bezier_points[k].co=i[k]
if len(xx.shape.parts)==1:
y=xx.shape.points
vectorize(y)
spline=cu.splines.new('BEZIER')
spline.use_cyclic_u=True
spline.resolution_u=1
cu.resolution_u=1
cu.render_resolution_u=1
cu.dimensions='2D'
for k in range(len(y)-1):
spline.bezier_points[k].co=y[k]
bpy.context.scene.objects.active=bpy.data.objects[name]
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
bpy.data.objects[name].select=True
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.curve.select_all(action='SELECT')
bpy.ops.curve.handle_type_set(type='VECTOR')
bpy.ops.object.mode_set(mode='OBJECT')
bpy.ops.object.select_all(action='DESELECT')
print(name + " has been initialized.")
except:
print(name + " FAILED TO INITIALIZE.")
fails["index"].append(sf.index(xx))
fails["name"].append(name)
``````