@@ -880,7 +880,31 @@ def add_leaf(position, direction, rotation, scale):
880880    bpy .ops .object .mode_set (mode = 'OBJECT' )
881881    bpy .ops .object .shade_smooth ()
882882
883- 
883+ def  rehash_set (s , p_dist ):
884+     new_set  =  []
885+     new_set .append (s [0 ])
886+     i  =  1 
887+     while  i  <  len (s ):
888+         n_dist  =  (s [i ] -  new_set [- 1 ]).length 
889+         if  n_dist  >=  p_dist :
890+             new_set .append (new_set [- 1 ] +  p_dist / n_dist  *  (s [i ] -  new_set [- 1 ]))
891+         else :
892+             i += 1 
893+     return  new_set 
894+ 
895+ 
896+ 
897+ def  smooth_stroke (iterations ,smooth ,points ):
898+     
899+     for  i  in  range (iterations ):
900+         new_points  =  []
901+         new_points .append (points [0 ])
902+         for  j  in  range  (1 ,len (points )- 1 ):
903+             new_points .append (smooth / 2 * (points [j - 1 ]+ points [j + 1 ]) +  (1 - smooth )*  points [j ])
904+         new_points .append (points [- 1 ])
905+         points  =  new_points 
906+     return  points 
907+     
884908def  create_tree (position , is_twig = False ):
885909    """Creates a tree 
886910
@@ -979,7 +1003,18 @@ def create_tree(position, is_twig=False):
9791003
9801004    radius  =  mtree_props .radius 
9811005    extremites  =  [(extr , radius , Vector ((0 , 0 , 1 )), extr [0 ], last_bone , trunk2 , 0 )]
982- 
1006+     curr_grease_point  =  0 
1007+     using_grease  =  False 
1008+     gp  =  bpy .context .scene .grease_pencil 
1009+     grease_points  =  []
1010+     save_trunk_length  =  mtree_props .trunk_length 
1011+     save_trunk_space  =  mtree_props .trunk_space 
1012+     if  mtree_props .use_grease_pencil  and  gp  is  not None  and  gp .layers .active  is  not None  and  gp .layers .active .active_frame  is  not None  and  len (gp .layers .active .active_frame .strokes ) >  0  and  len (gp .layers .active .active_frame .strokes [0 ].points ) >  2 :
1013+         grease_points  =  rehash_set ([i .co  for  i  in  gp .layers .active .active_frame .strokes [0 ].points ], mtree_props .stroke_step_size )
1014+         grease_points  =  smooth_stroke (5 ,mtree_props .smooth_stroke ,grease_points )
1015+         mtree_props .trunk_length  =  len (grease_points ) - 2 
1016+         using_grease  =  True 
1017+     
9831018    # branches generation 
9841019    print ("Generating Branches..." )
9851020    for  i  in  range (mtree_props .iteration  +  mtree_props .trunk_length ):
@@ -1017,10 +1052,22 @@ def create_tree(position, is_twig=False):
10171052
10181053            if  i  <=  mtree_props .trunk_length :
10191054                branch_verts  =  [v  for  v  in  branch .verts ]
1020-                 ni , direction , nsi  =  join_branch (verts , faces , indexes , radius , mtree_props .trunk_space , branch_verts ,
1021-                                                  direction ,
1022-                                                  mtree_props .trunk_variation , s_index , seams2 )
1023-                 sortie  =  pos  +  direction  *  mtree_props .branch_length 
1055+                 if  not  using_grease :
1056+                     ni , direction , nsi  =  join_branch (verts , faces , indexes , radius , mtree_props .trunk_space , branch_verts ,
1057+                                                      direction ,
1058+                                                      mtree_props .trunk_variation , s_index , seams2 )
1059+                     sortie  =  pos  +  direction  *  mtree_props .branch_length 
1060+ 
1061+                 else :
1062+                     grease_dir  =  grease_points [curr_grease_point + 1 ] -  grease_points [curr_grease_point ]
1063+                     grease_length  =  grease_dir .length 
1064+                     grease_dir .normalize ()
1065+                     ni , direction , nsi  =  join_branch (verts , faces , indexes , radius ,grease_length ,
1066+                                                      branch_verts ,
1067+                                                      grease_dir ,
1068+                                                      0 , s_index , seams2 )
1069+                     sortie  =  pos  +  grease_dir  *  grease_length 
1070+                     curr_grease_point  += 1 
10241071
10251072                if  i  <=  mtree_props .bones_iterations :
10261073                    bones .append ((Lb [0 ], len (bones ) +  2 , Lb [1 ], sortie ))
@@ -1096,14 +1143,16 @@ def create_tree(position, is_twig=False):
10961143
10971144        extremites  =  nextremites 
10981145    # mesh and object creation 
1146+     mtree_props .trunk_length  =  save_trunk_length 
1147+     mtree_props .trunk_space  =  save_trunk_space 
10991148    print ("Building Object..." )
11001149
11011150    mesh  =  bpy .data .meshes .new ("tree" )
11021151
11031152    mesh .from_pydata (verts , [], faces )
11041153    mesh .update (calc_edges = False )
11051154    obj  =  bpy .data .objects .new ("tree" , mesh )
1106-     obj .location  =  position 
1155+     obj .location  =  position   if   not   using_grease   else   grease_points [ 0 ]  -   Vector (( 0 , 0 , 1 )) 
11071156    scene .objects .link (obj )
11081157    scene .objects .active  =  obj 
11091158    obj .select  =  True 
0 commit comments