1

I have issue with this line of code.

import bpy

for obj in bpy.data.collections['Import'].all_objects: obj.select_set(True)
bpy.context.view_layer.objects.active = obj bpy.ops.object.material_slot_remove()

The code its working fine when I executed.

But after I made simple operator and button for this operation. And when I linked this code with button.
I get this alert 'NoneType' object has no attribute 'active_material_index' Full code bellow. I will be very grateful for helping resolving this issue


import collections
import bpy

IMPORT HANDLE

def Button_01(context):

scene=bpy.data.scenes["Scene"].name     
### SET ACTIVE COLLECTION###
collections = bpy.context.view_layer.layer_collection.children

for collection in collections:
     if collection.name == "Import":
        bpy.context.view_layer.active_layer_collection = collection

folder = Path(r"D:\import")
fbx_files = [f for f in folder.glob("**/*.fbx")]
for fbx_file in fbx_files:
    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.import_scene.fbx(filepath=str(fbx_file))
    print('HANDLE IMPORT')
for obj in bpy.context.scene.objects:
    if obj.name.lower().startswith(""):
        print("found")
        for child in obj.children:
            print(child.name)

    ###### CLEAR PARENTS ######
for obj in bpy.data.collections['Import'].all_objects:
        obj.select_set(True)
        bpy.context.view_layer.objects.active = obj
        bpy.ops.object.parent_clear(type='CLEAR_KEEP_TRANSFORM')


    ####  DELETED UN USED PARTS ###  ### ONLY FOR MESH BASED NAME ####
        collection_name = "Import"
        collection = bpy.data.collections[collection_name]
        meshes = set()

for obj in [o for o in collection.objects if o.type == 'EMPTY']:
    meshes.add( obj.data )
    bpy.data.objects.remove( obj )


def mesh_delete(name):
    for obj in bpy.context.scene.objects:
        if obj.name.startswith(f'{name}'):
            obj.select_set(True)
            bpy.data.objects.remove( obj ) 

mesh_delete('screwHead')
mesh_delete('washer')




### NAME CHANGING FROM SKU CODE TO MATERIAL ###  ### ONLY FOR SKU CODE BASED NAME ###     

def Change_name(old_name, new_name):
    for obj in bpy.data.collections['Import'].all_objects:
        bpy.ops.object.select_all(action='DESELECT')
        if obj.name.lower().startswith(old_name.lower()) :
            objects = bpy.context.scene.objects
            objName = f'{old_name}'
            obj.name = f'{new_name}'

### PURE MATERIAL SLOTS ###  
for obj in bpy.data.collections['Import'].all_objects:
    obj.select_set(True)
    bpy.context.view_layer.objects.active = obj
    bpy.context.object.active_material_index = 0
    bpy.ops.object.material_slot_remove()            


class SimpleOperator(bpy.types.Operator): bl_idname = "object.simple_operator" bl_label = "Import Handle"

def execute(self, context):
    Button_01(context)
    return {'FINISHED'}




MENU

class LayoutDemoPanel2(bpy.types.Panel): """Creates a Panel in the scene context of the properties editor""" bl_label = "DC Render Menu- Handle" bl_idname = "SCENE_PT_layout2" bl_space_type = 'PROPERTIES' bl_region_type = 'WINDOW' bl_context = "scene"

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

    # Big button
    layout.label(text="Import Handle:")
    row = layout.row()
    row.scale_y = 1.0
    row.operator("object.simple_operator", icon ="GHOST_ENABLED")               



def register(): bpy.utils.register_class(LayoutDemoPanel2) bpy.utils.register_class(SimpleOperator)

def unregister(): bpy.utils.unregister_class(LayoutDemoPanel2) bpy.utils.unregister_class(SimpleOperator)

if name == "main": register()

KamilD
  • 21
  • 3
  • 2
    I think you want o.type != 'EMPTY' instead of o.type == 'EMPTY', obj.data is None for empty objects. Or even o.type == 'MESH' since you seem to want only mesh objects – Gorgious Jan 17 '23 at 16:15
  • @Gorgious -- I love that this forum is the equivalent of walking down the hall at work to ask an expert! You guys are so appreciated! – james_t Jan 17 '23 at 18:37
  • Hello thanks for help. but still have this issue AttributeError: 'NoneType' object has no attribute 'active_material_index'

    even if Im using this ` collection_name = "Import" collection = bpy.data.collections[collection_name]

    for obj in [o for o in collection.objects if o.type == 'MESH']: obj.select_set(True) bpy.context.view_layer.objects.active = obj bpy.context.object.active_material_index = 0 bpy.ops.object.material_slot_remove() ` But if you execute this code without simple operator its working fine :(

    – KamilD Jan 17 '23 at 19:33
  • 1
    @james_t hehe thanks :) I suggest you use an override for this. remove obj.select_set(True) bpy.context.view_layer.objects.active = obj and instead use : obj.active_material_index = 0 and bpy.ops.object.material_slot_remove({'object': obj}) – Gorgious Jan 17 '23 at 20:15
  • Hello I found this parts of code. and when I added it before my script this fix my issue.
    # Set the area to the outliner
    
        area = bpy.context.area
        old_type = area.type 
        area.type = 'OUTLINER'
    
        ids = bpy.context.selected_ids
    

    and I had to put this after my code

        # Reset the area 
        area.type = old_type 
    
    – KamilD Jan 18 '23 at 13:00

0 Answers0