Hi,
my first post here. I’m a bit of a newbie both to Python and Blender.
Just got a weird issue. I have a very simple script.
It uses a Clean material function from Material Utilities (which works on 1 object at the time) and tries to call it on every object selected.
When I run just the essential code it works fine.
I made it into a package like my other scripts and tried to call form object panel.
And it doesn’t work properly. It get rid of all material slots on last selected object and others remain unchanged.
However if I just call the function bpy.ops.object.cleanmaterailslots() from script window or Quick Favorite form Hard Ops it works perfeclty fine.
Very weird, it’s just the same function called…
I’ll put some code to explain:
That’s the scripts itself
bl_info = {
"name": "Bla bla",
"author": "me",
"version": (1, 0),
"blender": (2, 80, 0),
"location": "View3D > Object",
"description": "Clean Material Slots",
"warning": "",
"wiki_url": "",
"category": "Object",
}
import bpy
from bpy.types import(
AddonPreferences,
Operator,
Panel,
PropertyGroup,
)
class OBJECT_OT_cleanmaterialslots(Operator):
bl_label = "Mech Clean Material Slots"
bl_idname = "object.cleanmaterailslots"
bl_description = "Mech Clean Material Slots"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_options = {'REGISTER', 'UNDO'}
def execute(self, context):
selObj = bpy.context.selected_objects
view_layer = bpy.context.view_layer
objList = []
for obj in selObj:
objList.append(obj)
bpy.ops.object.select_all(action='DESELECT')
for obj in objList:
obj.select_set(True)
view_layer.objects.active = obj
try:
bpy.ops.view3d.materialutilities_clean_material_slots()
except:
print('ERROR')
print('Object name: ' + obj.name)
bpy.ops.object.select_all(action='DESELECT')
return {'FINISHED'}
def menu_func(self, context):
self.layout.operator(OBJECT_OT_cleanmaterialslots.bl_idname)
That’s my init.py
import bpy
from . import panelRunningFunctionFromOutside
from . import MechPartsRenamer
from . import MechCleanMaterialSlots
bl_info = {
"name": "bla bla",
"description": "Better Tools",
"author": "me",
"version": (1, 0, 0),
"blender": (2, 80, 0),
"wiki_url": "my github url here",
"tracker_url": "my github url here/issues",
"category": "Modelling"
}
classes = (
MechPartsRenamer.OBJECT_OT_partsRenamer,
MechCleanMaterialSlots.OBJECT_OT_cleanmaterialslots,
panelRunningFunctionFromOutside.HelloWorldPanel,
)
def register():
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in reversed(classes):
bpy.utils.unregister_class(c)
if __name__ == '__main__':
register()
And that’s my panel:
import bpy
class HelloWorldPanel(bpy.types.Panel):
"""Creates a Panel in the Object properties window"""
bl_label = "Hello World Panel"
bl_idname = "OBJECT_PT_hello"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "object"
def draw(self, context):
layout = self.layout
obj = context.object
row = layout.row()
row.label(text="Better Tools!", icon='WORLD_DATA')
row = layout.row()
row.label(text="Active object: " + obj.name)
row = layout.row()
row.prop(obj, "name")
row = layout.row()
row.operator("object.parts_renamer")
row.operator("object.cleanmaterailslots")