Delete SME views script causing error

Delete SME views script causing error

TB_RIO
Advocate Advocate
5,958 Views
22 Replies
Message 1 of 23

Delete SME views script causing error

TB_RIO
Advocate
Advocate

I have used this script for a while in conjunction with the 'show materials for selected' in order to quickly get materials in and out of the Slate Editor. Recently (I think it corresponds with me updating to Vray 5) the clear SME views one causes the below error, and a lot of the time causes max to crash.

 

I'm a complete scripting luddite and don't know what this means unfortunately, can anyone help!?

 

Or is there a more sensible way of doing it now (I'm on 2020)

here's the script:

----------------------------------------------------------------------------------------
-- Version:		v1.0
-- Author:			Aleksey Sheremetov  - https://kotiger.co/
----------------------------------------------------------------------------------------

macroScript Delete_SME_Views
category:"Kotscript"
tooltip:"Delete SME views"
buttontext:"DeleteViews"

(
	ViewsTotal = sme.GetNumViews ()
	ViewIndex = 1
	for i in 1 to ViewsTotal do 		
	(
		view = sme.GetView (ViewIndex)
		if matchPattern view.name pattern:"*View*" then
		(
			sme.DeleteView (sme.GetViewIndex (view)) false
		)
		else
		(	
			ViewIndex += 1
		)
	)
)

 

0 Likes
5,959 Views
22 Replies
Replies (22)
Message 2 of 23

TB_RIO
Advocate
Advocate

Now (in 2021) the script to put these materials in to the SME is broken:

 

macroScript ShowMaterials category:"Rode3D" tooltip:"Show materials for selected objects"
(
	-- *** Opens the Material Editor and shows just the materials for the selected objects ***
	if (selection.count > 0) then
	(
		sme.Open()
		--POPULATE THE VIEW
		for obj in selection do
		(
			NewView.CreateNode obj.material[0,0]
		)
		--ARRANGE THE VIEW
		actionMan.executeAction 369891408 "40060"
	)
	else
	(
		messagebox "You must have at least one object selected!"
	)
)
0 Likes
Message 3 of 23

denisT.MaxDoctor
Advisor
Advisor

delete all views:

while (sme.GetNumViews() > 0) do sme.DeleteView 1 false

 

delete views whose name starts with "View ...":

for i = sme.GetNumViews() to 1 by -1 where matchpattern (sme.GetView i).name pattern:"view*" do sme.DeleteView i false

delete all views: 

0 Likes
Message 4 of 23

denisT.MaxDoctor
Advisor
Advisor

max is correct... there is no such thing as NewView

 

here is how to do what you want:

if selection.count > 0 do
(
	sme.Open()
	for i = sme.GetNumViews() to 1 by -1 where matchpattern (sme.GetView i).name pattern:"Current Materials" do sme.DeleteView i false

	id = sme.CreateView "Current Materials"
	new_view = sme.GetView id 
	for node in selection where iskindof node.material Material do new_view.CreateNode node.material [0,0]
	actionMan.executeAction 369891408 "40060"
)
Message 5 of 23

hoverpsonic
Community Visitor
Community Visitor

I've tried both scripts and they are working on empty scene but when scene has objects and materials those scripts are crashing 3ds max. 

0 Likes
Message 6 of 23

hoverpsonic
Community Visitor
Community Visitor

I've tried both scripts and they are working on empty scene but when scene has objects and materials those scripts are crashing 3ds max. 

0 Likes
Message 7 of 23

victorEFVH7
Enthusiast
Enthusiast

Hi,

 

I want to bring this thread to life again because I need to batch many maxfiles. Problem exist in both 2021+2022

I have been trying to investigate it and the issue is really strange. The code doesnt work if the slate is populate and you evaluate just the lines but when you hook it up to a rollout, create a dialog and press the button it works all fine. As a standalone evaluation it will make 3dsmax crash and tell you its known system exception in 0x0 and it doesnt tell you more than that in the listener.

 

unknown.png

 

I get this error no matter how different I write the code. Tried a few different methods with while and for loops

 

Lets say this is the default line of code that will make max crash

if not SME.isOpen() do SME.open()	
while (sme.GetNumViews() > 0) do sme.DeleteView 1 false
sme_view = sme.GetView (sme.CreateView "View1")

if run the same code through a rollout and create dialog it will work fine (however this will make it impossible to batch run this code)

rollout clearSme_ro "ClearSME"
(		
                button btn_clearsme "ClearSME_button" 
		
                on btn_clearsme pressed do (
		   if not SME.isOpen() do SME.open()	
		   while (sme.GetNumViews() > 0) do sme.DeleteView 1 false
		   sme_view = sme.GetView (sme.CreateView "View1")
		)
)

CreateDialog clearSme_ro

So I though if I  do a UI.Accessor press button it might work? But it does unfortunately crash and its impossible to figure out whats the problem.

Even tried to disable global rendering in SME, but still doesnt work.  Even delete view with actionman.executeAction will make it crash.

 

rollout clearSme_ro "ClearSME"
(		
                button btn_clearsme "ClearSME_button" 
		on btn_clearsme pressed do (
		    if not SME.isOpen() do SME.open()	
		    while (sme.GetNumViews() > 0) do sme.DeleteView 1 false
		    sme_view = sme.GetView (sme.CreateView "View1")
		)
)

CreateDialog clearSme_ro

dialogHWND =  windows.getChildHWND 0 "ClearSME"
dialogChildren = windows.getChildrenhwnd dialogHWND[1]
for child in dialogChildren where child[5] == "ClearSME_button" do 
UIAccessor.pressButton child[1]

 

@denisT.MaxDoctor  any clue what the reason behind this might be or are there other ways to batch clean unused materials in a maxfile?

0 Likes
Message 8 of 23

jon.bell
Alumni
Alumni

Hi @TB_RIO@victorEFVH7@hoverpsonic and @denisT.MaxDoctor,

 

I've asked some of our MAXScript experts to take a look at this thread and see if they can help diagnose this issue.

 

Most important, though: Chaos Group recently discovered an issue with V-Ray (since fixed) where changing some PBR maps would result in a crash. They've already issued a hotfix with VRay 5.20.01, so if you're running V-Ray, please make sure you've downloaded and installed that on your copies of 3ds Max.

 

In addition, if Max is allowing you to submit Customer Error Reports (CERs) after these crashes, please submit them, make sure your email address is included, and then post those CER numbers here so I can look them up. Thanks!



Jon A. Bell
Senior Technical Support Specialist, 3ds Max
0 Likes
Message 9 of 23

denisT.MaxDoctor
Advisor
Advisor

@victorEFVH7 wrote:

@denisT.MaxDoctor  any clue what the reason behind this might be or are there other ways to batch clean

unused materials in a maxfile?


The code that I showed above works for me on 2016 and 2020. I can't test it on 2021/22 right now, but I see some difference of how the same code works on 2016 and 2020. So, I believe that you can have a problem on 2021/22.

 

My guess that was something changed in specific materials (V-Ray for example). 

Therefore, I propose to conduct several tests with a new method:

 

 

fn sme_deleteAllViews open:off delNodes:on delRef:on delView:on = undo off
(
	if open then sme.open() else sme.close()
	
	k0 = sme.getnumviews()
	
	for k = sme.getnumviews() to 1 by -1 do
	(
		v = sme.getview k
		format "%:\n" v.name
		if delNodes do
		(
			n0 = v.getnumnodes()
			v.selectall()
			v.deleteselection()
			n1 = v.getnumnodes()
			format "\t% > nodes: %(%)\n" k n1 n0
		)
		if delRef do
		(
			r0 = refs.getreference trackViewNodes[#sme] k
			refs.replacereference trackViewNodes[#sme] k undefined
			r1 = refs.getreference trackViewNodes[#sme] k
			format "\t% > reference: %(%)\n" k r1 r0
		)
		if delView do
		(
			sme.deleteview k off
			k1 = sme.getnumviews()
			format "\t% > views: %(%)\n" k k1 k0
		)
	)
	(k0 - sme.getnumviews())
)	

 

 

1. We do it with "undo off" now because the "deleteview" operation is not undoable anyway

2. We have some options:

    1) do it with open or closed sme

    2) you can delete all nodes of the view first and delete the view after

    3) you can delete sme view reference target before deleting the view

 

Try all of these options and combinations of them ... maybe some of them will work. Maybe you have a better understanding of what exactly is causing the crash.

 

(and please let us know)  

Message 10 of 23

victorEFVH7
Enthusiast
Enthusiast

@denisT.MaxDoctor 

Works like a charm, all options works and give no error at all. Code is even successful with sme closed.

Also tried the previous code with undo off and it works like it did in previous versions of Max. So I guess max struggles with the undo.

 

Im currently batching 100 maxfiles and so far so good, lets see if I run into som error otherwise this case is closed. 

 

Thanks for the quick reply guys.

 

 

0 Likes
Message 11 of 23

denisT.MaxDoctor
Advisor
Advisor

Im currently batching 100 maxfiles and so far so good, lets see if I run into som error otherwise this case is closed.

 

If you are using batch mode (opening and closing new files), you need to open SME for every new scene (file). This is the only reliable way to update the current SME data.

0 Likes
Message 12 of 23

victorEFVH7
Enthusiast
Enthusiast

For some reason, it worked fine without opening it. But I ran into a crash after 70 files however the violation code were different this time so thats "good" and maybe its not because of the script but other reasons.

 

 

 

0 Likes
Message 13 of 23

denisT.MaxDoctor
Advisor
Advisor

It looks like your problem is with batch mode and sme. I recommend opening a new topic so as not to lump everything together.

0 Likes
Message 14 of 23

TB_RIO
Advocate
Advocate

Doesn't do anything for me on 2021/2022

0 Likes
Message 15 of 23

denisT.MaxDoctor
Advisor
Advisor

@TB_RIO wrote:

Doesn't do anything for me on 2021/2022


what are you talking about? what is not working for you?

 

0 Likes
Message 16 of 23

calberto.cart
Advocate
Advocate

Hi there, I wanted to revive this topic in order to have something to work in Max 2024 like its suppose to.

This line code that @victorEFVH7 wrote, worked for me in Max 2024.

if not SME.isOpen() do SME.open()	
while (sme.GetNumViews() > 0) do sme.DeleteView 1 false
sme_view = sme.GetView (sme.CreateView "View1")

 But when having a lot of View tabs it crashes.

Hope someone could take a look into this.

0 Likes
Message 17 of 23

calberto.cart
Advocate
Advocate

Hi there, I wanted to revive this topic in order to have something to work in Max 2024 like its suppose to.

This line code that you wrote, worked for me in Max 2024.

if not SME.isOpen() do SME.open()	
while (sme.GetNumViews() > 0) do sme.DeleteView 1 false
sme_view = sme.GetView (sme.CreateView "View1")

 But when having a lot of View tabs (it seems that more than 10) it crashes, but I dont know if its something in my file or its something in the code

0 Likes
Message 18 of 23

denisT.MaxDoctor
Advisor
Advisor

 

fn delete_all_view_tvnodes = 
(
	x = trackViewNodes[#sme]
	del = 0
	for p in getpropnames x where iskindof (v = getproperty x p) MAXTVNode do 
	(
		res = deleteTrackViewNode x v 
		del += 1
	)
	del
)

fn delete_all_sme_views = 
(
	del = 0
	while sme.GetNumViews() do 
	(
		sme.DeleteView 1 false
		del += 1
	)
	del
)

for k=1 to 1000 do sme.CreateView ("TestView" + k as string)

format "START with number views:%\n" (sme.GetNumViews()) 

del = undo off
(
	d0 = delete_all_sme_views()
	d1 = delete_all_view_tvnodes()
	
	[d0, d1]
)

format "% - END with number views:%\n" del (sme.GetNumViews()) 

 

 

To run the test for the first time, you must open the SME at least once. After that, it can be closed...

Message 19 of 23

calberto.cart
Advocate
Advocate

Thank u so much for your quick replying but....with this script 3ds max freezes and then crashes

I have tried with the SME open and closed several times, also with different files, but still crashes, doesnt work at all 😞

With less than 10 View tabs it also freezes but it cleans all, but i cannot use SME I cannot create any other view tab 

0 Likes
Message 20 of 23

denisT.MaxDoctor
Advisor
Advisor

The crash is not caused by the number of views but by their content. Certain materials or textures cause the crash.
The easiest way to test the assumption and possibly solve the problem is to attach the problematic scene.

 

Open a new thread and post an example scene to be able to reproduce the issue.

0 Likes