|
| 1 | +# ##### BEGIN GPL LICENSE BLOCK ##### |
| 2 | +# |
| 3 | +# facearea.py , a Blender addon to calculate vertex colors based on face area. |
| 4 | +# (c) 2020 Michel J. Anders (varkenvarken) |
| 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | +# |
| 20 | +# ##### END GPL LICENSE BLOCK ##### |
| 21 | + |
| 22 | +bl_info = { |
| 23 | + "name": "facearea", |
| 24 | + "author": "Michel Anders (varkenvarken)", |
| 25 | + "version": (0, 0, 202005101258), |
| 26 | + "blender": (2, 83, 0), |
| 27 | + "location": "View3D > Vertex Paint > Weights > WeightLifter", |
| 28 | + "description": "", |
| 29 | + "warning": "", |
| 30 | + "wiki_url": "", |
| 31 | + "category": "Paint"} |
| 32 | + |
| 33 | +import numpy as np |
| 34 | +import bpy |
| 35 | +import bmesh |
| 36 | + |
| 37 | + |
| 38 | +class FaceArea(bpy.types.Operator): |
| 39 | + bl_idname = "paint.facearea" |
| 40 | + bl_label = "Face Area" |
| 41 | + bl_description = "Calculate vertex colors based on face area" |
| 42 | + bl_options = {'REGISTER', 'UNDO'} |
| 43 | + |
| 44 | + |
| 45 | + @classmethod |
| 46 | + def poll(self, context): |
| 47 | + """ |
| 48 | + Only visible in vertex paint mode if the active object is a mesh and has at least one vertex color layer |
| 49 | + """ |
| 50 | + p = (context.mode == 'PAINT_VERTEX' and |
| 51 | + isinstance(context.object, bpy.types.Object) and |
| 52 | + isinstance(context.object.data, bpy.types.Mesh) and |
| 53 | + len(context.object.data.vertex_colors) |
| 54 | + ) |
| 55 | + return p |
| 56 | + |
| 57 | + def execute(self, context): |
| 58 | + bpy.ops.object.mode_set(mode='OBJECT') |
| 59 | + |
| 60 | + scene = context.scene |
| 61 | + self.ob = context.active_object |
| 62 | + mesh = context.object.data |
| 63 | + |
| 64 | + vertex_colors = mesh.vertex_colors.active.data |
| 65 | + |
| 66 | + areamap = {loop:f.area for f in mesh.polygons for loop in range(f.loop_start, f.loop_start + f.loop_total)} |
| 67 | + maxarea = max(areamap.values()) |
| 68 | + print(maxarea) |
| 69 | + for i,area in areamap.items(): |
| 70 | + w=area/maxarea |
| 71 | + vertex_colors[i].color[:3] = (w,w,w) |
| 72 | + |
| 73 | + bpy.ops.object.mode_set(mode='VERTEX_PAINT') |
| 74 | + bpy.ops.object.mode_set(mode='EDIT') |
| 75 | + bpy.ops.object.mode_set(mode='VERTEX_PAINT') |
| 76 | + |
| 77 | + return {'FINISHED'} |
| 78 | + |
| 79 | + |
| 80 | +def menu_func_vcol(self, context): |
| 81 | + self.layout.operator(FaceArea.bl_idname,icon='PLUGIN') |
| 82 | + |
| 83 | +classes = (FaceArea, ) |
| 84 | + |
| 85 | +register_classes, unregister_classes = bpy.utils.register_classes_factory(classes) |
| 86 | + |
| 87 | +def register(): |
| 88 | + register_classes() |
| 89 | + bpy.types.VIEW3D_MT_paint_vertex.append(menu_func_vcol) |
| 90 | + |
| 91 | +def unregister(): |
| 92 | + bpy.types.VIEW3D_MT_paint_vertex.remove(menu_func_vcol) |
| 93 | + unregister_classes() |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + register() |
0 commit comments