Notifications
Clear all

[Closed] MultiMaterial UNDO [ solved ]

I append new material to multi-material, but after undoing this operation all faces that initially used ID#1 material now suddenly seen as black in viewports and still look correctly on render.
Is there anything I can do to fix or prevent that?

Here’s the code that demonstrates the issue



	delete objects
	gc()

	b1 = convertToMesh (box lengthsegs:1 widthsegs:1 heightsegs:1)
	b2 = box lengthsegs:1 widthsegs:1 heightsegs:1 pos:[40,0,0]
	addModifier b2 (Materialmodifier materialID:2)
	meshop.attach b1 b2

	mm = Multimaterial numsubs:1
	mm.materiallist[1].diffuse = yellow
	b1.material = mm

	redrawViews() -- both first and second boxes use first material
	sleep 0.5


	mm.numsubs += 1 -- add yet another material for ID 2
	redrawViews() -- second box uses second mtl as expected

	sleep 0.5
	max undo -- emulate user undo

	-- faces with ID 2 now ignore ID 1 material and seen as black in viewports, but render correctly
gif

One thing I found to ‘fix’ it is to save & reload the scene, but that’s not a solution…
Another one is

replaceInstances mm (copy mm)
2 Replies
b1 = convertToMesh (box lengthsegs:1 widthsegs:1 heightsegs:1)
b2 = box lengthsegs:1 widthsegs:1 heightsegs:1 pos:[40,0,0]
addModifier b1 (Materialmodifier materialID:1)
addModifier b2 (Materialmodifier materialID:2)
converttomesh b1
converttomesh b2
meshop.attach b1 b2

this makes all undoable.

It is unsafe (from my practice point of view) to use a multi-material with fewer sub-materials than the material IDs used in a mesh object. I see my old scripts in the archive that check this situation and fix it in two ways: a) – fix the multi-material; or b) – fix material IDs.
There is also another “cue” to solve this problem – when you attach meshes, you also have the option of choosing what you should do with material IDs – mapping materials to IDs or IDs to material. In any case, the problem should be resolved.

I checked it in 2014 & 2020 and the issue still remains.
Undoing mm.numsubs += 1 makes object black in both.

gif

I’m not always in control of what kind of geometry it is to condense mesh IDs to match multimat. Or even if I could do something object may have stack of modifiers that make it impossible
.
What I need is an undoable append/delete for multi-material like the one you have in ‘Assign Material to Selection’ when you assign materials to subobj selection.
.
seems like wrapping it with an undo context solves it

undo on
(
	mm.numsubs += 1	
	replaceInstances mm (copy mm)
)