DoLayout event

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
synthsin75
Posts: 10020
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: DoLayout event

Post by synthsin75 »

You can "break" out of a loop any time you want.
User avatar
KuzKuz
Posts: 507
Joined: Mon Aug 19, 2013 5:12 pm
Location: Ukraine

Re: DoLayout event

Post by KuzKuz »

MehdiZangenehBar wrote: Tue May 07, 2024 1:53 am
KuzKuz wrote: Mon May 06, 2024 8:57 pm Each panel update like this will cost you approximately 15-20 megabytes of RAM depending on the size of your project.
Are you sure it took that amount of memory? Any function to calculate that?
Searching for a layer will work much faster than creating a new one even if you have to iterate through all the layers in the project.
As for the amount of RAM used, just open a scene with characters and monitor in Task Manager how much more RAM Moho uses when creating new layers.
User avatar
MehdiZangenehBar
Posts: 55
Joined: Wed Feb 07, 2024 7:17 pm
Contact:

Re: DoLayout event

Post by MehdiZangenehBar »

KuzKuz wrote: Tue May 07, 2024 11:29 am Searching for a layer will work much faster than creating a new one even if you have to iterate through all the layers in the project.
I'm not sure, Can you prove that?
KuzKuz wrote: Tue May 07, 2024 11:29 am As for the amount of RAM used, just open a scene with characters and monitor in Task Manager how much more RAM Moho uses when creating new layers.
I did that, almost nothing.

This is a method to calculate memory usage for entire function using collectgarbage("count") and total usage is less than 10 kilo byte for most cases:

Code: Select all

-- **************************************************
-- General information about this script
-- **************************************************

ScriptName = "TestScript"

TestScript = {}

function TestScript:Name()
	return 'Name'
end

function TestScript:Version()
	return 'Version'
end

function TestScript:UILabel()
	return 'UILabel'
end

function TestScript:Creator()
	return 'Creator'
end

function TestScript:Description()
	return 'Description'
end

-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************

function TestScript:IsRelevant(moho)
	return true
end

function TestScript:IsEnabled(moho)
	return true
end

-- **************************************************
-- Variables
-- **************************************************

local suspend_update_widgets = false
local suspend_do_layout = false
local last_layer_id = 0
local last_layer_type = 0

-- **************************************************
-- Functions
-- **************************************************

function ForceDoLayout()
	m_u = collectgarbage("count")
	suspend_update_widgets = true
	suspend_do_layout = true
	local helper = MOHO.ScriptInterfaceHelper:new_local()
	local moho = helper:MohoObject()
	local drawingToolsNonZero = MOHO.MohoGlobals.DisableDrawingToolsNonZero
	if not drawingToolsNonZero then
		MOHO.MohoGlobals.DisableDrawingToolsNonZero = true
	end
	local temp_layer = nil
	if moho.layer:LayerType() == MOHO.LT_GROUP then temp_layer = moho:CreateNewLayer(MOHO.LT_UNKNOWN, false) end
	local frame = moho.frame
	if frame == 0 then
		moho:SetCurFrame(1)
		if temp_layer == nil then suspend_do_layout = false end
		moho:SetCurFrame(0)
	elseif frame ~= 0 then
		moho:SetCurFrame(0)
		if temp_layer == nil then suspend_do_layout = false end
		moho:SetCurFrame(frame)
	end
	suspend_do_layout = false
	if temp_layer ~= nil then moho:DeleteLayer(temp_layer) end
	suspend_update_widgets = false
	if not drawingToolsNonZero then
		MOHO.MohoGlobals.DisableDrawingToolsNonZero = drawingToolsNonZero
	end
	helper:delete()
	print('Memory usage: ' .. tostring(collectgarbage("count") - m_u) .. ' kb')
end

-- **************************************************
-- Events
-- **************************************************

function TestScript:OnMouseDown(moho, mouseEvent)
end

function TestScript:DoLayout(moho, layout)
	if suspend_do_layout == true then return end
	print('TestScript:DoLayout')
	last_layer_id = moho.layer:UUID()
	last_layer_type = moho.layer:LayerType()
end

function TestScript:UpdateWidgets(moho)
	if suspend_update_widgets == true then return end
	print('TestScript:UpdateWidgets')
	local layer_id = moho.layer:UUID()
	local layer_type = moho.layer:LayerType()
	if last_layer_id ~= layer_id and last_layer_type == layer_type then
		print('Layer with same type changed. -----------------------------------')
		ForceDoLayout()
		print('--------------------------------------------------------------------')
		last_layer_id = layer_id
		last_layer_type = layer_type
	end
end
Would you pleae test the code on your file and tell me the result?
Post Reply