Why does my Python script "Onionize" my cone? I have created the python script found at this link. The link also includes the associated Test.csv.

The purpose of my script is simply to create two cones, but somehow, for a reason I cannot figure out, the output of Blender (see image below) is to "Onionize" my cone; my cones look like strange pointy onions.

Why?!



Code:

    import bpy, csv

from bpy.props import IntProperty
from bpy.types import (MESH_OT_primitive_circle_add, MESH_OT_primitive_cylinder_add, MESH_OT_primitive_uv_sphere_add, MESH_OT_primitive_cone_add)
#MESH_OT_primitive_cone_add.vertices = IntProperty(name="Vertices", default=32)

for area in bpy.context.screen.areas: # iterate through areas in current screen
    if area.type == 'VIEW_3D':
        for space in area.spaces: # iterate through spaces in current VIEW_3D area
            if space.type == 'VIEW_3D': # check if space is a 3D view
                space.viewport_shade = 'RENDERED' # set the viewport shading to rendered

bpy.ops.script.python_file_run(filepath="C:\\Program Files\\Blender Foundation\\Blender\\2.79\\scripts\\presets\\units_length\\meters.py")
fp = "C:/Dropbox/Blender/Test.csv"
bpy.context.scene.render.engine = 'CYCLES'

def makeMaterial(name, diffuse, specular, alpha):
    mat = bpy.data.materials.new(name)
    mat.diffuse_color = diffuse
    mat.diffuse_shader = 'LAMBERT' 
    mat.diffuse_intensity = 1.0 
    mat.specular_color = specular
    mat.specular_shader = 'COOKTORR'
    mat.specular_intensity = 0.5
    mat.alpha = alpha
    mat.ambient = 1
    return mat

def setMaterial(ob, mat):
    me = ob.data
    me.materials.append(mat)

AQ2 = makeMaterial('AQ2', (0.329411764705882,0.505882352941176,0.611764705882353), (1,1,1), 1)
AQ4 = makeMaterial('AQ4', (0.611764706,0.478431373,0.329411764705882), (1,1,1), 1)

with open( fp ) as csvfile:
    rdr = csv.reader(csvfile, delimiter=',')
    for i, row in enumerate( rdr ):
        bpy.ops.mesh.primitive_cone_add(radius1 = float(row[3]), radius2=0, depth=float(row[3])*2, view_align=False, enter_editmode=False, location = ( float(row[0]),float(row[1]),float(row[2])), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
        setMaterial(bpy.context.object, AQ4)
        active = bpy.context.active_object
        bpy.context.active_object.name = 'SAQ4'
        active.modifiers.new(name='mysubsurf', type='SUBSURF')
        active.modifiers["mysubsurf"].levels = 5
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier="mysubsurf")


        bpy.ops.mesh.primitive_cone_add(radius1 = float(row[7]), radius2=0, depth=float(row[7])*2, view_align=False, enter_editmode=False, location = ( float(row[4]),float(row[5]),float(row[6])), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))
        setMaterial(bpy.context.object, AQ2)
        active = bpy.context.active_object
        bpy.context.active_object.name = 'SAQ2'
        active.modifiers.new(name='mysubsurf', type='SUBSURF')
        active.modifiers["mysubsurf"].levels = 5
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier="mysubsurf")

    x = float(row[8]) 
    save_render = "C:/Dropbox/Blender/" + str("{:10.2f}".format(x)) + ".png"

    bpy.data.scenes["Scene"].render.filepath = save_render
    bpy.ops.render.render( write_still=True )

    # deselect all
    ##bpy.ops.object.select_all(action='DESELECT')
    # selection
    ##bpy.data.objects['SAQ2'].select = True
    # remove it
    ##bpy.ops.object.delete()
    # deselect all
    ##bpy.ops.object.select_all(action='DESELECT')
    # selection
    ##bpy.data.objects['SAQ4'].select = True
    # remove it
    ##bpy.ops.object.delete()
As commented above, the lines

active.modifiers.new(name='mysubsurf', type='SUBSURF')
active.modifiers["mysubsurf"].levels = 5
bpy.ops.object.modifier_apply(apply_as='DATA',modifier="mysubsurf")


in the code confirm that the problem is the interaction between subsurface modifier and tris in the mesh geometry. Remove or comment these lines and you will get beautiful cones.

The code can be used if the primitve added is based on quads.