Image
ScriptName = "HS_RandomiseParticle"

HS_RandomiseParticle = {}


function HS_RandomiseParticle:Name()
	return "Randomise Particle Start"
end

function HS_RandomiseParticle:Version()
	return "1.22"
end

function HS_RandomiseParticle:Description()
	return ("Change the starting configuration for particles")
end

function HS_RandomiseParticle:Creator()
	return "HayaSidist"
end

function HS_RandomiseParticle:UILabel()
	return ("Set Random Seed")
end

function HS_RandomiseParticle:ColorizeIcon()
	return true
end



-- **************************************************
-- values not preserved across invocations
-- **************************************************

local thisUUT = "D" -- diagnostic thread
local seq
local seed
local r = {}
local r_belongs_to = ""
local seq_max = 100
local particleRandomMax = 16777216 -- 2^24
local randKey = "HS_Random"
local randVec = LM.Vector2:new_local()
local doneUpdate = false



-- **************************************************
-- values preserved across invocations
-- **************************************************



--***************************************************
-- save / load / reset preserved values
-- **************************************************


-- **************************************************
-- The guts of this script
-- **************************************************

function HS_RandomiseParticle:IsEnabled(moho)
	if moho.layer:LayerType() == MOHO.LT_PARTICLE then
		return true
	end

	return false
end


function HS_RandomiseParticle:IsRelevant(moho)

	local MohoVers = 0
	if type (MOHO.ScriptInterface.AppVersion) == "function" then
		MohoVers = tonumber((string.gsub(moho:AppVersion(), "^(%d+)(%.%d+)(%..+)", "%1%2")))
	end
	if MohoVers < 12 then 
		return false
	end

	return (moho:IsPro()) -- runs pro only -- must be last relevance test

end




-- **************************************************
-- Tool options - create and respond to tool's UI
-- **************************************************



HS_RandomiseParticle.Back  = MOHO.MSG_BASE + 30

HS_RandomiseParticle.Fwd  = MOHO.MSG_BASE + 40

HS_RandomiseParticle.Reroll  = MOHO.MSG_BASE + 50

local function setRandTable (moho)
	math.randomseed (seed)
	for i = 1, seq_max do
		r[i] = math.random(1, particleRandomMax)
	end
	r_belongs_to = moho.layer:UUID()
end

local function doInit(moho)

	local i
	local ScriptInfo = moho.layer:ScriptData()
	if (ScriptInfo:HasKey(randKey)) then
		randVec = ScriptInfo:GetVector2(randKey)
		seed = randVec.x
		seq = randVec.y
--		HS_Test_harness:tracePrint (thisUUT, "got key")
	else
--		HS_Test_harness:tracePrint (thisUUT, "no existing key")
		s = moho.layer:UUID()
		s = string.gsub (s, "%-.*", "") -- grab the first part of the uuid
		seed = tonumber (s, 16) --tonumber (e [, base])
--		seed = tonumber (s.."ZZZ", 16) --tonumber (e [, base]) --< test

		if seed == nil then
--			HS_Test_harness:tracePrint (thisUUT, "bad UUID")
			seed = math.floor(math.pi*1e5)
		else
			seed = math.floor(seed / 1e3)
		end
		seq =  1
		randVec.x = seed
		randVec.y = seq
		ScriptInfo:Set(randKey, randVec)
--		HS_Test_harness:diagnosePrint (thisUUT, "seed", s, seed)
	end
--	HS_Test_harness:diagnosePrint (thisUUT, "starting with seed", seed, "and seq", seq)

	setRandTable (moho)
	doneUpdate = false
--	self:UpdateWidgets(moho) --<< causes loop


end


function HS_RandomiseParticle:DoLayout(moho, layout)

--	HS_Test_harness:tracePrint (thisUUT, "do layout in", self:Name(), self:Version())

	self.B1 = LM.GUI.Button(" < ", self.Back)
	self.B1:SetToolTip("previous seed")
	layout:AddChild(self.B1)

	self.F1 = LM.GUI.Button(" > ", self.Fwd)
	self.F1:SetToolTip("next seed")
	layout:AddChild(self.F1)

	layout:AddPadding (LM.GUI.DEFAULT_PADDING*4)
	self.New = LM.GUI.Button("^v", self.Reroll)
	self.New:SetToolTip("new set of seeds")
	layout:AddChild(self.New)

	doInit(moho)

end

function HS_RandomiseParticle:OnMouseDown(moho, mouseEvent)
--	mouseEvent.view:SetToolTip("Shift click = <; Click = >; Ctrl-Alt Click = ^v")
end

function HS_RandomiseParticle:OnMouseMoved(moho, mouseEvent)
	mouseEvent.view:SetToolTip("Shift click = <; Click = >; Ctrl-Alt Click = ^v")
end

function HS_RandomiseParticle:OnMouseUp(moho, mouseEvent)
	local msg  = self.Fwd
	if mouseEvent.shiftKey then
		msg = self.Back
	elseif mouseEvent.altKey and mouseEvent.ctrlKey then
		msg = self.Reroll
	end
	self:HandleMessage(moho, mouseEvent.view, msg)
end

function HS_RandomiseParticle:UpdateWidgets(moho)
--	HS_Test_harness:tracePrint (thisUUT, "In update widgets with layer", moho.layer:Name(), "on frame", moho.frame)
--	HS_Test_harness:diagnosePrint (thisUUT, "r belongs to", r_belongs_to, "active layer", moho.layer:UUID())

	if r_belongs_to ~= moho.layer:UUID() then
-- 		swapping between particle layers with the tool active does not do a new "do layout"
		doInit(moho)
	end
	if doneUpdate then
		return
	end
--	HS_Test_harness:diagnosePrint (thisUUT, "chosen rand", r[seq], "from seed", seed, "and seq", seq)
	moho.document:PrepUndo(moho.layer)
	moho.layer:SetRandomSeed(r[seq])
	moho.layer:FinalizeSettings()
	randVec.x = seed
	randVec.y = seq
	moho.layer:ScriptData():Set(randKey, randVec)
--	HS_Test_harness:diagnosePrint (thisUUT, "saved seed", randVec.x, "and seq", randVec.y)
	moho.document:SetDirty()
--	MOHO.Redraw()
--	moho:UpdateUI()

	doneUpdate = true
end

--***********************************
--	message handler
--***********************************

function HS_RandomiseParticle:HandleMessage(moho, view, msg)

--	HS_Test_harness:tracePrint (thisUUT, "In message handler with ", msg-MOHO.MSG_BASE, "on frame ", moho.frame)
--	HS_Test_harness:diagnosePrint (thisUUT, "entering with seed", seed, "and seq", seq)

	if (msg == self.Back) then
		seq = seq - 1
		if seq < 1 then
			seq = seq_max
		end		
	elseif (msg == self.Fwd) then
		seq = seq + 1
		if seq > seq_max then
			seq = 1
		end		

	elseif (msg == self.Reroll) then
		moho.document:PrepUndo(moho.layer)
		seed = seed + seq
		setRandTable (moho)
		seq = 1
	else
--		HS_Test_harness:diagnosePrint (switch, "unrecognised message")

	end
--	HS_Test_harness:diagnosePrint (thisUUT, "exiting with seed", seed, "and seq", seq)

	doneUpdate = false
	MOHO.Redraw()
	moho:UpdateUI()


end



Icon
HS Randomise Particles
Listed

Script type: Tool

Uploaded: May 01 2024, 06:09

Changes the random number seed for particle generation
Randomise Particles
===================

Release notes for version 1.22

This tool is an extension to the “randomise” button on the standard particle layer tool.
It will run on the pro version only of Moho 12 or later.


Operation
==========

Activate the tool with a particle layer selected and the tool will generate a set of random numbers then apply the first of that set to the selected particle layer. It then enables the user to step forward and backwards through that set or to generate a new set.

The active set of random numbers and the actual number selected are specific to a given particle layer and are preserved across activations of the tool.

Three buttons are provided
<  selects the previous random number in the set
>  selects the next random number in the set
^v  creates a new set of random numbers

The mouse can also be used
Click          selects the next random number in the set
Shift click    selects the previous random number in the set
Ctrl-Alt click  creates a new set of random numbers


Known issues
============

None – but please report errors / bugs or requests for enhancement.


History
======

This version originally released via a different hosting platform 1st May 2018
This script, and all other scripts on this site are distributed as free software under the GNU General Public License 3.0 or later.
Downloads count: 79