Customization script cleanup problem when scene closed

Report crashes, strange behaviour, or apparent bugs
Post Reply
mfloyd
Posts: 21
Joined: 29 Oct 2014, 23:06

Customization script cleanup problem when scene closed

Post by mfloyd »

When a customization script is called for sim_call_type==sim_customizationscriptcall_cleanup when a scene is closed, it seems that the scene content is no longer available to perform the cleanup actions. My customization script keeps track of the handles of objects and GUIs it creates in order to remove them from the scene when needed. They are properly removed on cleanup after editing the script, but not when the scene is closed. When the scene is closed, simRemoveUI produces the error "UI does not exist" and the objects in the scene are not removed as they should. When opening the scene again the UI's and objects are still in the scene, but now the customization script no longer has the handles to them easily available to remove them like it tried to on scene close.

coppelia
Site Admin
Posts: 10337
Joined: 14 Dec 2012, 00:25

Re: Customization script cleanup problem when scene closed

Post by coppelia »

Hello,

you are right regarding the custom UIs: on scene close, they will be removed before the scripts are removed (which was a bug actually, and corrected now). But you say this also happens with scene objects? This shouldn't be the case. If you add new scene objects during simulation, then they will automatically be removed when simulation stops (you can change this behaviour in the simulation settings dialog). But upon scene close all scene objects should still be there when the clean-up procedure of customization scripts is called. Can you confirm this?

If you create dynamically objects/UI/etc. in your customization script, then erase them upon customization script clean-up, you have a problem when you save your scene: the dynamically created objects will also be saved, and restored next time you load the scene. But the customization script will re-create those objects at scene load, and you will have duplicates of them. One way to avoid this is to keep track of the created objects inside of objects, e.g.:

This can be problematic when saving your scene (will create a duplicate at scene load):

Code: Select all

if (sim_call_type==sim_customizationscriptcall_initialization) then
	h=simGetObjectAssociatedWithScript(sim_handle_self)
	cube=simCreatePureShape(0,2,{0.1,0.1,0.1},1)
end

if (sim_call_type==sim_customizationscriptcall_cleanup) then
	simRemoveObject(cube)
end
This solves the problem:

Code: Select all

if (sim_call_type==sim_customizationscriptcall_initialization) then
	h=simGetObjectAssociatedWithScript(sim_handle_self)

	-- Remove the objects I created previously:
	local allObjects=simGetObjectsInTree(sim_handle_scene)
	for i=1,#allObjects,1 do
		local data=simReadCustomDataBlock(allObjects[i],'iCreatedThatObject')
		if data then
			simRemoveObject(allObjects[i])
		end
	end

	-- Create objects and tag them as 'iCreatedThatObject':
	cube=simCreatePureShape(0,2,{0.1,0.1,0.1},1)
	simWriteCustomDataBlock(cube,'iCreatedThatObject','1')
end

if (sim_call_type==sim_customizationscriptcall_cleanup) then
	simRemoveObject(cube)
end
For custom UIs, the approach is similar, but a little bit different, since you cannot write tags to custom UIs: you will have to store their name in a scene object (using the handle doesn't work, since handles are not guaranteed to stay the same from one scene instance to the next).

Cheers

mfloyd
Posts: 21
Joined: 29 Oct 2014, 23:06

Re: Customization script cleanup problem when scene closed

Post by mfloyd »

For standard child scripts I never have the problem with removing UI's and objects on cleanup: The simulation must always be stopped before closing the scene, and the cleanup portion of the script is always called and always works. For objects created by child scripts during simulation, even if I don't remove them in cleanup, they are removed at the end of simulation as they should (as set in my simulation settings). I can't save the scene during simulation either, so the temporary objects can't be saved with the scene. So there's no problems there.

The problem I have only happens for customization scripts. It makes sense that even the 'temporary' objects in the scene created by the script are saved and reloaded with the script, even if cleanup is called when closing the scene: the scene was saved before cleanup and not again afterwards. Now that you've pointed that out, I changed the order of cleanup operations so that the the UI is removed after removing all the other shapes. I only get errors for trying to remove the UI, so it must be that the other objects are being removed correctly and only stay in the scene because they were saved with the scene.

Thanks to your suggested solution I am able to remove scene objects and the UI correctly after loading a scene now.

Post Reply