-- ************************************************** -- Provide Moho with the name of this script object -- ************************************************** ScriptName = "DR_randomSwitch" -- ************************************************** -- General information about this script -- ************************************************** DR_randomSwitch = {} function DR_randomSwitch:Name() return "Random Switches" end function DR_randomSwitch:Version() return "1.0" end function DR_randomSwitch:Description() return "Create random switches for the selected Switch Layer" end function DR_randomSwitch:Creator() return "David Rylander, 2005" end function DR_randomSwitch:UILabel() return("Random Switches...") end -- ************************************************** -- Recurring values -- ************************************************** DR_randomSwitch.range_start = 1 DR_randomSwitch.range_end = 72 DR_randomSwitch.interval_min = 2 DR_randomSwitch.interval_max = 4 DR_randomSwitch.alwaysfirst = true -- ************************************************** -- Random dialog -- ************************************************** local DR_randomDialog = {} function DR_randomDialog:new(moho) local d = LM.GUI.SimpleDialog("Random Switches", DR_randomDialog) local l = d:GetLayout() d.moho = moho l:PushH() l:PushV() l:AddChild(LM.GUI.StaticText("Range, from frame:"), LM.GUI.ALIGN_LEFT) l:AddChild(LM.GUI.StaticText("Range, to frame:"), LM.GUI.ALIGN_LEFT) l:AddChild(LM.GUI.StaticText("Interval, min:"), LM.GUI.ALIGN_LEFT) l:AddChild(LM.GUI.StaticText("Interval, max:"), LM.GUI.ALIGN_LEFT) l:Pop() l:PushV() d.range_start = LM.GUI.TextControl(0, "0000", 0, LM.GUI.FIELD_UINT) l:AddChild(d.range_start) d.range_end = LM.GUI.TextControl(0, "0000", 0, LM.GUI.FIELD_UINT) l:AddChild(d.range_end) d.interval_min = LM.GUI.TextControl(0, "0000", 0, LM.GUI.FIELD_UINT) l:AddChild(d.interval_min) d.interval_max = LM.GUI.TextControl(0, "0000", 0, LM.GUI.FIELD_UINT) l:AddChild(d.interval_max) l:Pop() l:Pop() d.alwaysfirst = LM.GUI.CheckBox("Always key first keyframe in range.") l:AddChild(d.alwaysfirst, LM.GUI.ALIGN_LEFT) return d end function DR_randomDialog:UpdateWidgets() self.range_start:SetValue(DR_randomSwitch.range_start) self.range_end:SetValue(DR_randomSwitch.range_end) self.interval_min:SetValue(DR_randomSwitch.interval_min) self.interval_max:SetValue(DR_randomSwitch.interval_max) self.alwaysfirst:SetValue(DR_randomSwitch.alwaysfirst) end function DR_randomDialog:OnOK() DR_randomSwitch.range_start = self.range_start:IntValue() DR_randomSwitch.range_end = self.range_end:IntValue() DR_randomSwitch.interval_min = self.interval_min:IntValue() DR_randomSwitch.interval_max = self.interval_max:IntValue() DR_randomSwitch.alwaysfirst = self.alwaysfirst:Value() end -- ************************************************** -- The guts of this script -- ************************************************** function DR_randomSwitch:Run(moho) -- check if the layer is a switch layer if (moho.layer:LayerType() ~= MOHO.LT_SWITCH) then LM.GUI.Alert(LM.GUI.ALERT_INFO, "The selected layer is not a Switch Layer.", nil, nil, "OK", nil, nil) return end local dlog = DR_randomDialog:new(moho) if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then return end moho.document:SetDirty() moho.document:PrepUndo(moho.layer) local switchLayer =moho:LayerAsSwitch(moho.layer) local switch = switchLayer:SwitchValues() --Check for switches funkar inte alltid! lista ut vad som är fel! for frameI = DR_randomSwitch.range_start, DR_randomSwitch.range_end do --check for switches if switch:HasKey(frameI) then local deletekeys = LM.GUI.Alert(LM.GUI.ALERT_WARNING, "The Switch Layer has switch keys in the selected range.", "These will be deleted if you proceed.", nil, "OK", "CANCEL", nil) if deletekeys == 1 then return end elseif deletekeyes == 0 then end do break end end for frameC = DR_randomSwitch.range_start, DR_randomSwitch.range_end do switch:DeleteKey(frameC) end math.randomseed( os.time() ) math.random(); math.random(); math.random() frejm = (DR_randomSwitch.range_start) oldname = 2 while frejm < (DR_randomSwitch.range_end) do if DR_randomSwitch.alwaysfirst == false then --not key first frame frejm = frejm + temp_interval end temp_interval = math.random(DR_randomSwitch.interval_min, DR_randomSwitch.interval_max) antal = switchLayer:CountLayers() while name == oldname do name = switchLayer:Layer(math.random(antal) -1):Name() end switchLayer:SwitchValues():SetValue((frejm), (name)) if DR_randomSwitch.alwaysfirst == true then --key first frame frejm = frejm + temp_interval end oldname = name moho:NewKeyframe(CHANNEL_SWITCH) MOHO.Redraw() end end