# Convert gpencil to curve, python, crash

I'm not well versed in scripting and I'm trying to write a script that involves converting grease pencil to curve, I have discovered that the operator

bpy.ops.gpencil.convert(type='CURVE', use_timing_data=True)


needs the 3D view context to work, however if i set the correct context Blender crashes without any crash log

i have tried these two options

import bpy

area = bpy.context.area
old_type = area.type
area.type = 'VIEW_3D'

bpy.ops.gpencil.convert(type='CURVE', use_timing_data=True)

area.type = old_type


and

    import bpy

win      = bpy.context.window
scr      = win.screen
areas3d  = [area for area in scr.areas if area.type == 'VIEW_3D']
region   = [region for region in areas3d[0].regions if region.type == 'WINDOW']

override = {'window':win,
'screen':scr,
'area'  :areas3d[0],
'region':region,
'scene' :bpy.context.scene,
}

bpy.ops.gpencil.convert(override, type='CURVE', use_timing_data=True)


How to explan the crashing and is there maybe a different approach for creating curve from grease pencil?

The custom context has an invalid region set, which results in the region data rv3d pointer being NULL in gp_camera_view_subrect() (gpencil_convert.c). In your custom context region is set to [bpy.data.screens['Scripting']...Region]. Instead of a list it should be a single bpy_struct. This can be fixed by modifying your override dictionary:

override = {'window': win,
'screen': scr,
'area'  : areas3d[0],
'region': region[0],
'scene' : bpy.context.scene}


Alternatively you could copy the current context and modify the area and region.

import bpy

context = bpy.context.copy()

for area in bpy.context.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
context['area'] = area
context['region'] = region
break
break

bpy.ops.gpencil.convert(context, type='CURVE', use_timing_data=True)