0
$\begingroup$

I'm sorry in advance if my script looks really messy and not optimised, it's my first time using python or a dev language for that matters.

I've tried to setup a script that, among other things, create and assign objects to a LP collection by checking their names.

When I'm trying to unlink the object from the HP collection they were taken from, the script successfully does the task, but still return an error, here's the script :

#List all object in scene that ends with remeshed and select them
for obj in bpy.context.scene.objects: 
    if obj.name.endswith("remeshed"): 
        obj.select_set(True)          

#Create collection with selected objects
bpy.ops.collection.create(name='LP') 

#Link collection to Scene Collection
bpy.context.scene.collection.children.link(bpy.data.collections["LP"]) 

#Remove low poly objects from HP Collection
for obj in bpy.context.scene.objects: 
    if obj.name.endswith("remeshed"):
        bpy.context.view_layer.objects.active = obj
        bpy.ops.collection.objects_remove(collection = 'HP')

I've also tried to use bpy.context.scene.collection.objects.unlink(obj) withtout success even with the HP collection selected and active.

Here's the error message :

Traceback (most recent call last):
  File "<blender_console>", line 4, in <module>
  File "C:\Program Files\Blender Foundation\Blender 2.91\2.91\scripts\modules\bpy\ops.py", line 132, in __call__
    ret = _op_call(self.idname_py(), None, kw)
TypeError: Converting py args to operator properties:  enum "HP" not found in ('LP')

And as I was saying the script does what I want and unlink the object from the HP collection, but since it returns an error, it stop when running it.

Thanks in advance for all the help and advise you could give me.

$\endgroup$
0
$\begingroup$

I have enhanced and modified your code and, it works perfectly :

bl_info = {
    "name" : "Clear Collection",
    "author" : "CédricF",
    "description" : "",
    "blender" : (2, 80, 0),
    "version" : (0, 0, 1),
    "location" : "Sidebar(N)>Clean Up",
    "category" : "Clean Up"
}

import bpy

from bpy.props import PointerProperty

#----------------------------------------
#MAIN PANEL
#----------------------------------------

class CLEARCOLLECTION_Property(bpy.types.PropertyGroup):

    ClrByNameEnd : bpy.props.StringProperty(name= "All Objects Ending By", default="")
    ClrByCOLNameEnd : bpy.props.StringProperty(name= "Collection", default="")

class CLEARCOLLECTION_PT_MAINPANEL(bpy.types.Panel):
    bl_idname = "CLEARCOLLECTION_PT_CLEARBYNAMEEND"
    bl_label = "Clear By Name End"
    bl_space_type = "VIEW_3D"
    bl_region_type = "UI"
    bl_category = "Clean Up"

    def draw(self, context):
        layout = self.layout

        scene = context.scene

        CLRCTool = scene.CLRTool

        row = layout.row()
        row.prop(CLRCTool, "ClrByNameEnd", icon= "TRASH")
        
        row = layout.row()
        row.prop(CLRCTool, "ClrByCOLNameEnd", icon= "TRASH")

        layout.operator("clearcollection.clearbyend", icon= "TRASH")
        layout.operator("clearcollection.slecbyend", icon= "RESTRICT_SELECT_OFF")
        
#----------------------------------------
#CLEAR OBJECTS
#----------------------------------------

class CLEARCOLLECTION_OT_CLEAR(bpy.types.Operator):
    bl_idname = "clearcollection.clearbyend"
    bl_label = "Clear Objects"
    bl_description = "Clears objects which end with the given input"
    bl_options = {"REGISTER"}

    def execute(self, context):
    
        scene = context.scene

        CLRCTool = scene.CLRTool

        for obj in bpy.context.scene.objects: 
            if CLRCTool.ClrByNameEnd != "":
                if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                    bpy.data.objects.remove(obj)


        for obj in bpy.context.scene.objects:
            if CLRCTool.ClrByCOLNameEnd != "":
                for objc in bpy.data.collections[CLRCTool.ClrByCOLNameEnd].all_objects:
                    if CLRCTool.ClrByNameEnd != "":
                        if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                            bpy.data.objects.remove(obj)
        
        return {"FINISHED"}

#----------------------------------------
#SELECT OBJECTS
#----------------------------------------

class CLEARCOLLECTION_OT_SELEC(bpy.types.Operator):
    bl_idname = "clearcollection.slecbyend"
    bl_label = "Select"
    bl_description = "Selects objects which end with the given input"
    bl_options = {"REGISTER"}

    def execute(self, context):
    
        scene = context.scene

        CLRCTool = scene.CLRTool

        for obj in bpy.context.scene.objects: 
            if CLRCTool.ClrByNameEnd != "":
                if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                    obj.select_set(True)


        for obj in bpy.context.scene.objects:
            if CLRCTool.ClrByCOLNameEnd != "":
                for objc in bpy.data.collections[CLRCTool.ClrByCOLNameEnd].all_objects:
                    if CLRCTool.ClrByNameEnd != "":
                        if obj.name.endswith(CLRCTool.ClrByNameEnd): 
                            obj.select_set(True)
        
        return {"FINISHED"}

classes = [CLEARCOLLECTION_Property, CLEARCOLLECTION_OT_CLEAR, CLEARCOLLECTION_OT_SELEC, CLEARCOLLECTION_PT_MAINPANEL]

def register():
    
    for cls in classes :
        bpy.utils.register_class(cls)
        bpy.types.Scene.CLRTool = PointerProperty(type= CLEARCOLLECTION_Property)


def unregister():
    
    for cls in classes :
        bpy.utils.unregister_class(cls)
        #del bpy.types.Scene.CLRTool


if __name__ == "__main__":
    register()
$\endgroup$
2
  • $\begingroup$ Btw thanks for this helpful idea, this thing is sure gonna help me a lot in my workflow. $\endgroup$ – Arib.Glitch Jan 19 at 18:00
  • $\begingroup$ Glad it was of any help. I actually found my issue and used the following code: #Remove low poly objects from HP Collection col = bpy.data.collections['HP'] for obj in bpy.context.scene.objects: if obj.name.endswith("remeshed"): print(obj) obj.select_set(True) unlinkobj = col.objects.get(obj.name) col.objects.unlink(unlinkobj) But yours seems definitely more accurate and clearer. $\endgroup$ – CédricF Jan 20 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.