1.选中物体,进入权重绘制模式
2.代码:
import bpy # 获取当前活动的物体 obj = bpy.context.object # 确保物体是网格类型 if obj.type != 'MESH': print("当前激活的对象不是网格类型。") #exit() # 检查名为“water”的顶点组是否存在 vertex_group_name = "water" if vertex_group_name not in obj.vertex_groups: print(f"未找到名为 '{vertex_group_name}' 的顶点组。") #exit() # 获取名为“water”的顶点组 vg = obj.vertex_groups[vertex_group_name] # 获取顶点组的权重列表,并打印顶点索引和权重 weights = [] for v in obj.data.vertices: weight = vg.weight(v.index) weights.append(weight) print(f"Vertex {v.index} has weight {weight}") # 创建或更新顶点颜色属性 color_attr_name = "water_colors" if color_attr_name not in obj.data.attributes: color_attr = obj.data.attributes.new( name=color_attr_name, type='BYTE_COLOR', domain='POINT' ) else: color_attr = obj.data.attributes[color_attr_name] # 设置顶点颜色属性,映射权重到灰度颜色 for i, weight in enumerate(weights): # 将权重映射到颜色上,创建从白色到黑色的灰度渐变 color_value = int(weight * 255) # 权重映射到0-255的范围内 color_attr.data[i].color = (color_value, color_value, color_value, 255) # RGB相同,Alpha为255 print(f"顶点组 '{vertex_group_name}' 的权重已成功转换为顶点颜色属性 '{color_attr_name}'。") # 确保视图更新以显示顶点颜色 bpy.context.view_layer.update()