1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| import bpy from bpy.props import (StringProperty, IntProperty, FloatVectorProperty, PointerProperty, BoolProperty, ) from bpy.types import (Panel, Operator, PropertyGroup, UIList, )
ID_COLLECTION_NAME = "ID Collection"
class ColorPropertyGroup(PropertyGroup): color: FloatVectorProperty( name="Color", subtype='COLOR', size=4, min=0.0, max=1.0, default=(0.8, 0.8, 0.8, 1.0), update=lambda self, context: self.update_object_color() ) obj_ref: PointerProperty(type=bpy.types.Object) def update_object_color(self): if self.obj_ref: self.obj_ref.color = self.color[:3] + (1.0,)
class OBJECT_UL_ObjectList(UIList): def draw_item(self, context, layout, data, item, icon, active_data, active_propname, index): if self.layout_type in {'DEFAULT', 'COMPACT'}: row = layout.row(align=True) row.label(text=item.obj.name, icon='OBJECT_DATA') row.prop(item.color, "color", text="")
class OBJECT_OT_ToggleIDCollectionVisibility(Operator): bl_idname = "object.toggle_id_collection_visibility" bl_label = "Toggle ID Collection Visibility" bl_description = "Exclude/Show ID Collection in the view layer" def execute(self, context): id_collection = bpy.data.collections.get(ID_COLLECTION_NAME) if not id_collection: self.report({'WARNING'}, "ID Collection not found!") return {'CANCELLED'} view_layer = context.view_layer found = False for layer_collection in view_layer.layer_collection.children: if layer_collection.collection == id_collection: layer_collection.exclude = not layer_collection.exclude found = True break if not found: self.report({'WARNING'}, "ID Collection not in view layer!") return {'CANCELLED'} return {'FINISHED'}
class OBJECT_OT_LinkToID(Operator): bl_idname = "object.link_to_id" bl_label = "Link to ID" bl_description = "Associate selected objects with ID sub-collection" id_number: IntProperty( name="ID Number", description="ID number to link to (1-15)", min=1, max=15, default=1 ) def execute(self, context): main_collection = bpy.data.collections.get(ID_COLLECTION_NAME) if not main_collection: main_collection = bpy.data.collections.new(ID_COLLECTION_NAME) context.scene.collection.children.link(main_collection) sub_coll_name = f"ID{self.id_number}" sub_collection = bpy.data.collections.get(sub_coll_name) if not sub_collection: sub_collection = bpy.data.collections.new(sub_coll_name) main_collection.children.link(sub_collection) for obj in context.selected_objects: self.ensure_only_in_id_collection(obj, sub_collection) self.report({'INFO'}, f"Associated objects with {sub_coll_name}") return {'FINISHED'} def ensure_only_in_id_collection(self, obj, target_collection): for collection in obj.users_collection: if collection.name.startswith("ID") and collection != target_collection: collection.objects.unlink(obj) if target_collection.name not in [c.name for c in obj.users_collection]: target_collection.objects.link(obj)
class OBJECT_OT_SetCollectionColor(Operator): bl_idname = "object.set_collection_color" bl_label = "设置集合颜色" bl_description = "Set color for all objects in the collection" color: FloatVectorProperty( name="Color", subtype='COLOR', size=4, min=0.0, max=1.0, default=(0.8, 0.8, 0.8, 1.0) ) def execute(self, context): collection_name = context.scene.my_tool.collection_name collection = bpy.data.collections.get(collection_name) if collection: for obj in collection.objects: obj.color = self.color[:3] + (1.0,) bpy.ops.object.refresh_object_list() return {'FINISHED'} def invoke(self, context, event): collection_name = context.scene.my_tool.collection_name collection = bpy.data.collections.get(collection_name) if collection and collection.objects: self.color = collection.objects[0].color[:3] + (1.0,) return context.window_manager.invoke_props_dialog(self)
class OBJECT_OT_RefreshObjectList(Operator): bl_idname = "object.refresh_object_list" bl_label = "Refresh Object List" bl_description = "Refresh the list of objects in the selected collection" def execute(self, context): scene = context.scene mytool = scene.my_tool collection = bpy.data.collections.get(mytool.collection_name) mytool.object_items.clear() if collection: for obj in collection.objects: item = mytool.object_items.add() item.obj = obj item.color.color = obj.color[:3] + (1.0,) item.color.obj_ref = obj return {'FINISHED'}
class ObjectListItem(PropertyGroup): obj: PointerProperty(type=bpy.types.Object) color: PointerProperty(type=ColorPropertyGroup)
class MyToolProperties(PropertyGroup): collection_name: StringProperty( name="Collection", description="Select a collection to modify", default="" ) object_items: bpy.props.CollectionProperty(type=ObjectListItem) active_object_index: IntProperty(default=0) hide_id_collection: BoolProperty( name="Hide ID Collection", description="Exclude ID Collection from view layer", default=False, update=lambda self, context: self.update_id_collection_visibility(context) ) def update_id_collection_visibility(self, context): id_collection = bpy.data.collections.get(ID_COLLECTION_NAME) if not id_collection: return view_layer = context.view_layer for layer_collection in view_layer.layer_collection.children: if layer_collection.collection == id_collection: layer_collection.exclude = self.hide_id_collection break
class OBJECT_PT_ColorPanel(Panel): bl_label = "Object ID 0.1 By 叱咤月海鱼鱼猫" bl_idname = "OBJECT_PT_color_panel" bl_space_type = 'VIEW_3D' bl_region_type = 'UI' bl_category = "Object ID" def draw(self, context): layout = self.layout scene = context.scene mytool = scene.my_tool row = layout.row() row.prop_search(mytool, "collection_name", bpy.data, "collections", text="Collection") row.operator("object.refresh_object_list", text="", icon='FILE_REFRESH') row = layout.row() row.operator("object.set_collection_color", text="Set Collection Color") layout.template_list("OBJECT_UL_ObjectList", "", mytool, "object_items", mytool, "active_object_index") layout.separator() box = layout.box() box.label(text="ID Channel") row = box.row() row.prop(mytool, "hide_id_collection", toggle=True, text="˃.˂这样就不影响渲染辣" if mytool.hide_id_collection else "@_@请点这里隐藏ID集合", icon='HIDE_ON' if mytool.hide_id_collection else 'HIDE_OFF') row = box.row() for i in range(1, 6): row.operator("object.link_to_id", text=f"ID{i}").id_number = i row = box.row() for i in range(6, 11): row.operator("object.link_to_id", text=f"ID{i}").id_number = i row = box.row() for i in range(11, 16): row.operator("object.link_to_id", text=f"ID{i}").id_number = i
classes = ( ColorPropertyGroup, ObjectListItem, MyToolProperties, OBJECT_UL_ObjectList, OBJECT_OT_ToggleIDCollectionVisibility, OBJECT_OT_LinkToID, OBJECT_OT_SetCollectionColor, OBJECT_OT_RefreshObjectList, OBJECT_PT_ColorPanel, )
def register(): for cls in classes: bpy.utils.register_class(cls) bpy.types.Scene.my_tool = PointerProperty(type=MyToolProperties)
def unregister(): for cls in reversed(classes): bpy.utils.unregister_class(cls) del bpy.types.Scene.my_tool
if __name__ == "__main__": register()"
|