@@ -880,7 +880,31 @@ def add_leaf(position, direction, rotation, scale):
880
880
bpy .ops .object .mode_set (mode = 'OBJECT' )
881
881
bpy .ops .object .shade_smooth ()
882
882
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
+
884
908
def create_tree (position , is_twig = False ):
885
909
"""Creates a tree
886
910
@@ -979,7 +1003,18 @@ def create_tree(position, is_twig=False):
979
1003
980
1004
radius = mtree_props .radius
981
1005
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
+
983
1018
# branches generation
984
1019
print ("Generating Branches..." )
985
1020
for i in range (mtree_props .iteration + mtree_props .trunk_length ):
@@ -1017,10 +1052,22 @@ def create_tree(position, is_twig=False):
1017
1052
1018
1053
if i <= mtree_props .trunk_length :
1019
1054
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
1024
1071
1025
1072
if i <= mtree_props .bones_iterations :
1026
1073
bones .append ((Lb [0 ], len (bones ) + 2 , Lb [1 ], sortie ))
@@ -1096,14 +1143,16 @@ def create_tree(position, is_twig=False):
1096
1143
1097
1144
extremites = nextremites
1098
1145
# mesh and object creation
1146
+ mtree_props .trunk_length = save_trunk_length
1147
+ mtree_props .trunk_space = save_trunk_space
1099
1148
print ("Building Object..." )
1100
1149
1101
1150
mesh = bpy .data .meshes .new ("tree" )
1102
1151
1103
1152
mesh .from_pydata (verts , [], faces )
1104
1153
mesh .update (calc_edges = False )
1105
1154
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 ))
1107
1156
scene .objects .link (obj )
1108
1157
scene .objects .active = obj
1109
1158
obj .select = True
0 commit comments