# Join curves with Python by color

I'd like to group curves by color, so I'm try to join them together. I used an override from this solution. Everything works splendid inside Blender but in my external program I have

AttributeError: 'ID' object has no attribute 'data'

This is function:

def curvegroup():
for crvobj in bpy.data.objects:
if re.match(r'^curve', crvobj.data.name):
crvs = []
color = crvobj.active_material.diffuse_color
for crvobjcol in bpy.data.objects:
if re.match(r'^curve', crvobjcol.data.name):
if (crvobjcol.active_material.diffuse_color == color):
crvs.append(crvobjcol)
ctx = bpy.context.copy()
ctx['active_object'] = crvs[0]
ctx['selected_objects'] = crvs
ctx['selected_editable_bases'] = [scene.object_bases[crv.name] for crv in crvs]
bpy.ops.object.join(ctx)


I am not sure that I understand scene.object_bases so I really don't know why ID object is there and what cause this error.

Another method causes Error: EXCEPTION_ACCESS_VIOLATION

def curvegroup02():
for crvobj in bpy.context.scene.objects:
bpy.ops.object.select_all(action='DESELECT')
if re.match(r'^Curve', crvobj.name):
scene.objects.active = crvobj
color = crvobj.active_material.diffuse_color
for crvobjcol in bpy.context.scene.objects:
if re.match(r'^Curve', crvobjcol.data.name):
if (crvobjcol.active_material.diffuse_color == color):
crvobjcol.select = True

for selected in bpy.context.selected_objects:
if selected.type == 'CURVE':
if (bpy.ops.object.join.poll()):
bpy.ops.object.join()


I hope that this script may help you, just pick the curve that has the "guide" color and run the script.

import bpy
import mathutils

#set custom color

#color = mathutils.Color((1,0,0))

#or

#use active's object diffuse color

color = bpy.context.active_object.active_material.diffuse_color

for obj in bpy.data.objects:
if obj.type == 'CURVE':
curve = obj
if curve.active_material.diffuse_color == color:
curve.select = True
else:
curve.select = False

if (bpy.ops.object.join.poll()):
bpy.ops.object.join()

• I think you solution is practically same as mine above and causes me same Error: EXCEPTION_ACCESS_VIOLATION.. problem is elsewhere I think. Can't figure where. – Destrosvet Sep 3 '18 at 15:22
• yes it is more or less the same, I am not getting any related error, does this script is working although ? Can you pass the console error you get ? (looks like an exception) – cnisidis Sep 3 '18 at 16:57
• Iterate over scene.objects rather than all the curves in file (bpy.data.objects), which may or may not be linked to scene. Often safer with bound vectors to copy them, so they are no longer assoc with object (prob not case here, possibly case in question) color = ob.blah.blah.diffuse_color.copy() – batFINGER Oct 14 '18 at 5:11