Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. '''
  2. by Dealga McArdle, july 2011.
  3.  
  4. BEGIN GPL LICENSE BLOCK
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software Foundation,
  18. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19.  
  20. END GPL LICENCE BLOCK
  21. '''
  22.  
  23. bl_info = {
  24. 'name': 'Normalize Spline',
  25. 'author': 'Dealga McArdle (zeffii)',
  26. 'version': (0, 0, 1),
  27. 'blender': (2, 6, 7),
  28. 'location': '3d view > Tool properties > Normalize Spline',
  29. 'description': 'select a spline/curve, drag the slidersit will fillet the edge.',
  30. 'wiki_url': '',
  31. 'tracker_url': '',
  32. 'category': 'Mesh'}
  33.  
  34.  
  35. '''
  36. [ ] force to be in object mode with curve selected.
  37. [ ] take total_resolution = segments ...
  38. [ ] segment_length = (tota_length/total_res)
  39.  
  40. [ ] consume algorithm, for each edge in temporary polyline
  41.  
  42. current_position = 0.0
  43. current_node = 0
  44.  
  45. for point in polyline[1:]:
  46. if current_position + polyline
  47. if edge < segment_length:
  48. if edge longer than segment_length consume
  49.  
  50. '''
  51.  
  52.  
  53.  
  54. import math
  55.  
  56. import bpy
  57. import bgl
  58. import blf
  59. import mathutils
  60. import bpy_extras
  61.  
  62. from mathutils import Vector
  63. from mathutils.geometry import interpolate_bezier
  64. from bpy_extras.view3d_utils import location_3d_to_region_2d as loc3d2d
  65.  
  66. def get_points(spline, clean=True, res=False):
  67. cyclic = True
  68. knots = spline.bezier_points
  69.  
  70. if len(knots) < 2:
  71. return
  72.  
  73. r = (res if res else spline.resolution_u) + 1
  74. segments = len(knots)
  75.  
  76. if not spline.use_cyclic_u:
  77. cyclic = False
  78. segments -= 1
  79.  
  80. master_point_list = []
  81. for i in range(segments):
  82. inext = (i + 1) % len(knots)
  83.  
  84. knot1 = knots[i].co
  85. handle1 = knots[i].handle_right
  86. handle2 = knots[inext].handle_left
  87. knot2 = knots[inext].co
  88.  
  89. bezier = knot1, handle1, handle2, knot2, r
  90. points = interpolate_bezier(*bezier)
  91. master_point_list.extend(points)
  92.  
  93. # some clean up to remove consecutive doubles, this could be smarter...
  94. if clean:
  95. old = master_point_list
  96. good = [v for i, v in enumerate(old[:-1]) if not old[i] == old[i+1]]
  97. good.append(old[-1])
  98. return good, cyclic
  99.  
  100. return master_point_list, cyclic
  101.  
  102. def get_edge_keys(points, cyclic):
  103. num_points = len(points)
  104. edges = [[i, i+1] for i in range(num_points-1)]
  105. if cyclic:
  106. edges[-1][1] = edges[0][0]
  107.  
  108. return edges
  109.  
  110. def get_total_length(points, edge_keys):
  111. edge_length = 0
  112. for edge in edge_keys:
  113. vert0 = points[edge[0]]
  114. vert1 = points[edge[1]]
  115. edge_length += (vert0-vert1).length
  116. return edge_length
  117.  
  118.  
  119. def draw_points(context, points, size, gl_col):
  120. region = context.region
  121. rv3d = context.space_data.region_3d
  122.  
  123. this_object = context.active_object
  124. matrix_world = this_object.matrix_world
  125.  
  126. # needed for adjusting the size of gl_points
  127. bgl.glEnable(bgl.GL_POINT_SMOOTH)
  128. bgl.glPointSize(size)
  129. bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)
  130.  
  131. bgl.glBegin(bgl.GL_POINTS)
  132. bgl.glColor4f(*gl_col)
  133. for coord in points:
  134. # vector3d = matrix_world * (coord.x, coord.y, coord.z)
  135. vector3d = matrix_world * coord
  136. vector2d = loc3d2d(region, rv3d, vector3d)
  137. bgl.glVertex2f(*vector2d)
  138. bgl.glEnd()
  139.  
  140. bgl.glDisable(bgl.GL_POINT_SMOOTH)
  141. bgl.glDisable(bgl.GL_POINTS)
  142. return
  143.  
  144. def draw_callback_px(self, context):
  145.  
  146. region = context.region
  147. rv3d = context.space_data.region_3d
  148.  
  149. spline = context.active_object.data.splines[0]
  150. points, cyclic = get_points(spline, clean=True, res=False)
  151. #edge_keys = get_edge_keys(points, cyclic)
  152. #total_length = get_total_length(points, edge_keys)
  153.  
  154. draw_points(context, points, 4.2, (0.2, 0.9, 0.9, .2))
  155.  
  156. # restore opengl defaults
  157. bgl.glLineWidth(1)
  158. bgl.glDisable(bgl.GL_BLEND)
  159. bgl.glColor4f(0.0, 0.0, 0.0, 1.0)
  160. return
  161.  
  162.  
  163.  
  164. # create 4 verts, string them together to make 4 edges.
  165. if False:
  166. Verts, Edges = [], []
  167.  
  168. profile_mesh = bpy.data.meshes.new("Base_Profile_Data")
  169. profile_mesh.from_pydata(Verts, Edges, [])
  170. profile_mesh.update()
  171.  
  172. profile_object = bpy.data.objects.new("Base_Profile", profile_mesh)
  173. profile_object.data = profile_mesh
  174.  
  175. scene = bpy.context.scene
  176. scene.objects.link(profile_object)
  177. profile_object.select = True
  178.  
  179.  
  180. class OBJECT_OT_draw_fillet(bpy.types.Operator):
  181. bl_idname = "dynamic.normalize"
  182. bl_label = "Draw Normalized"
  183. bl_description = "Allows the user to dynamically set curve resolution"
  184. bl_options = {'REGISTER', 'UNDO'}
  185.  
  186. # i understand that a lot of these ifs are redundant, scheduled for
  187. # deletion. is 'RELEASE' redundant for keys?
  188.  
  189. def modal(self, context, event):
  190. context.area.tag_redraw()
  191. return {'PASS_THROUGH'}
  192.  
  193. def invoke(self, context, event):
  194.  
  195. if context.area.type == 'VIEW_3D':
  196. context.area.tag_redraw()
  197. context.window_manager.modal_handler_add(self)
  198.  
  199. # Add the region OpenGL drawing callback
  200. # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
  201. self._handle = context.region.callback_add(
  202. draw_callback_px,
  203. (self, context),
  204. 'POST_PIXEL')
  205.  
  206. return {'RUNNING_MODAL'}
  207. else:
  208. self.report({'WARNING'},
  209. "View3D not found, cannot run operator")
  210. context.area.tag_redraw()
  211. return {'CANCELLED'}
  212.  
  213.  
  214.  
  215. class UIPanel(bpy.types.Panel):
  216. bl_label = "Spline Normalize"
  217. bl_space_type = "VIEW_3D"
  218. bl_region_type = "TOOL_PROPS"
  219.  
  220. scn = bpy.types.Scene
  221.  
  222. scn.NumVerts = bpy.props.IntProperty(min=2, max=64, default=12,
  223. name="number of verts")
  224.  
  225. #scn.SplineResolution = bpy.props.IntProperty(min=2, max=64, default=12,
  226. # name="number of verts")
  227.  
  228. @classmethod
  229. def poll(self, context):
  230. # i don't really care if this is in editmode or object mode
  231. return context.object.type == 'CURVE'
  232.  
  233.  
  234. def draw(self, context):
  235. layout = self.layout
  236. scn = context.scene
  237.  
  238. row1 = layout.row(align=True)
  239. row1.prop(scn, "NumVerts", expand = True)
  240.  
  241. row2 = layout.row(align=True)
  242. row2.operator("dynamic.normalize")
  243.  
  244.  
  245. # boilerplate register stuff
  246.  
  247. def register():
  248. bpy.utils.register_module(__name__)
  249.  
  250. def unregister():
  251. bpy.utils.unregister_module(__name__)
  252.  
  253. if __name__ == "__main__":
  254. register()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement