Skip to content

Instantly share code, notes, and snippets.

@iszotic iszotic/obj2gp_28.py
Last active May 29, 2019

Embed
What would you like to do?
Convert mesh/curve into grease pencil for 2.8
import bpy
from mathutils import Vector
def convert_to_GP(context, object = None, use_one_GP = True):
if object == None:
objects = context.selected_objects
else:
if isinstance(object,list):
objects = object
else:
objects = [object]
bpy.ops.object.select_all(action='DESELECT')
grease = None
for obj in objects:
if use_one_GP and not grease == None:
grease = grease
else:
bpy.ops.object.gpencil_add(type='EMPTY')
grease = context.view_layer.objects.active
if obj.type == 'MESH':
obj.select_set(True)
context.view_layer.objects.active = obj
bpy.ops.object.convert(target='CURVE')
obj.select_set(False)
context.view_layer.objects.active = grease
if len(grease.data.layers) == 0 or use_one_GP:
layer = grease.data.layers.new(name=obj.name)
else:
layer = grease.data.layers[0]
grease.data.layers.active = layer
frame = layer.frames.new(context.scene.frame_current)
if obj.type == 'MESH': #if conversion to curve failed
mesh = obj.data
for edge in mesh:
stroke = frame.strokes.new()
stroke.points.add(2)
stroke.display_mode = '3DSPACE'
stroke.material_index = 0
stroke.points[0].co = obj.matrix_world @ mesh.vertices[edge.vertices[0]].co
stroke.points[1].co = obj.matrix_world @ mesh.vertices[edge.vertices[1]].co
else:
for spline in obj.data.splines:
stroke = frame.strokes.new()
if spline.type == 'BEZIER':
points = spline.bezier_points
else:
points = spline.points
stroke.points.add(len(points))
stroke.draw_cyclic = spline.use_cyclic_u
stroke.display_mode = '3DSPACE'
stroke.material_index = 0
for i, point in enumerate(points):
stroke.points[i].co = obj.matrix_world @ point.co.xyz
#bpy.data.objects.remove(obj)
class SimpleOperator(bpy.types.Operator):
bl_idname = "object.convert_to_grease_pencil"
bl_label = "Convert Mesh/Curve to Grease Pencil"
def execute(self, context):
convert_to_GP(context)
return {'FINISHED'}
def register():
bpy.utils.register_class(SimpleOperator)
def unregister():
bpy.utils.unregister_class(SimpleOperator)
register()
@iszotic

This comment has been minimized.

Copy link
Owner Author

commented May 29, 2019

This code is more intended to use with other code, that's why it has options to introduce other objects apart from the ones in the context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.