2
$\begingroup$

I'm trying to piece together a script that will take a selected list of grease pencil objects and convert them to curves, one frame at a time. I'm calling upon the operator to convert the Grease Pencil object, but it's giving me a call back error.

    import bpy

##LOOP AND OFFSET THROUGH OBJECTS

# Get the current frame number
frame = bpy.context.scene.frame_current
start_frame = bpy.context.scene.frame_start

# Loop through all selected objects
for obj in bpy.context.selected_objects:
    
    # Move forward one frame
    bpy.context.scene.frame_set(frame + 0)
    
    #Convert Grease Pencil to Curve
    bpy.ops.gpencil.convert(type='CURVE', timing_mode='LINEAR', use_timing_data=False)
    
    # Set the object's render visibility property false
    obj.hide_render = True
          
    # Insert a keyframe on the render visibility property
    obj.keyframe_insert(data_path="hide_render", frame=frame -1 )
       
     # Set the object's render visibility property false
    obj.hide_render = False
      
    # Insert a keyframe on the render visibility property
    obj.keyframe_insert(data_path="hide_render", frame=frame + 0)
      
    # Set the object's render visibility property false
    obj.hide_render = True
      
    # Insert a keyframe on the render visibility property
    obj.keyframe_insert(data_path="hide_render", frame=frame + 1)
       
    # Increment the current frame number
    frame += 1
$\endgroup$
0

1 Answer 1

1
$\begingroup$

Sometimes, you need to override the context. See more here: Convert gpencil to curve, python, crash

I was also getting context error when I didn't have an active object, so make sure there is an active object when you select them, or you can even automate setting the active object.

import bpy

##LOOP AND OFFSET THROUGH OBJECTS
frame = bpy.context.scene.frame_current
start_frame = bpy.context.scene.frame_start

# override context

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[0],
            'scene' : bpy.context.scene} 


# Loop through all selected objects
for obj in bpy.context.selected_objects:
    
    # Move forward one frame
    bpy.context.scene.frame_set(frame)

    # set obj to active object
    #bpy.context.view_layer.objects.active = obj
    
    # Convert Grease Pencil to Curve
    bpy.ops.gpencil.convert(override, type='CURVE', timing_mode='LINEAR', use_timing_data=False)
   
    # Set the object's render visibility property true
    obj.hide_render = True
          
    # Insert a keyframe on the render visibility property
    obj.keyframe_insert(data_path="hide_render", frame = frame - 1)
    
    # Set the object's render visibility property false
    obj.hide_render = False
      
    # Insert a keyframe on the render visibility property
    obj.keyframe_insert(data_path="hide_render", frame = frame)
      
    # Set the object's render visibility property true
    obj.hide_render = True
      
    # Insert a keyframe on the render visibility property
    obj.keyframe_insert(data_path="hide_render", frame = frame + 1)
    
    frame += 1
$\endgroup$
4
  • 1
    $\begingroup$ Hi, thank you for your response. This gives me an "index error list index out of range" Is it my for loop causing the issue? $\endgroup$
    – Shotty
    Commented Feb 13, 2023 at 14:20
  • $\begingroup$ Seems like that would be occurring when trying to access 'areas3d[0]' or 'region[0]'. Do you know which line the error occurs at after looking at the console output? $\endgroup$ Commented Feb 13, 2023 at 18:43
  • 1
    $\begingroup$ It's occurring at line 12. Really appreciate your help. $\endgroup$
    – Shotty
    Commented Feb 13, 2023 at 20:45
  • $\begingroup$ Well, areas3d should be nonempty if there is a view_3d area. In the console section where you can enter code, import bpy first and then try the following line print(bpy.context.window.screen.areas[0].type) and then try print(bpy.context.window.screen.areas[1].type) and then print(bpy.context.window.screen.areas[2].type). Look through your areas and make sure 'VIEW_3D' is there. Mine is at the 5th index $\endgroup$ Commented Feb 13, 2023 at 21:26

Not the answer you're looking for? Browse other questions tagged .