Image
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "SZ_AdjustLineWidth"

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

SZ_AdjustLineWidth = {}

function SZ_AdjustLineWidth:Name()
	return self:Localize("Name")
end

function SZ_AdjustLineWidth:Version()
	return "1.01"
end

function SZ_AdjustLineWidth:Description()
	return self:Localize("Description")
end

function SZ_AdjustLineWidth:Creator()
	return "Stan from 2danimator.ru"
end

function SZ_AdjustLineWidth:UILabel()
	return self:Localize("UILabel")
end

-- **************************************************
-- Recurring values
-- **************************************************

SZ_AdjustLineWidth.modifier1 = 10
SZ_AdjustLineWidth.modifier2 = 1.10
SZ_AdjustLineWidth.width = 100
SZ_AdjustLineWidth.createKeyframe = false
SZ_AdjustLineWidth.layersToChange = {}


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

function SZ_AdjustLineWidth:IsEnabled(moho)
	return true
end

function SZ_AdjustLineWidth:IsRelevant(moho)
	return true
end

function SZ_AdjustLineWidth:OnMouseDown(moho, mouseEvent)

end

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

SZ_AdjustLineWidth.MODIFIER1 = MOHO.MSG_BASE
SZ_AdjustLineWidth.ADD = MOHO.MSG_BASE+1
SZ_AdjustLineWidth.SUBTRACT = MOHO.MSG_BASE+2
SZ_AdjustLineWidth.MODIFIER2 = MOHO.MSG_BASE+3
SZ_AdjustLineWidth.DIVIDE = MOHO.MSG_BASE+4
SZ_AdjustLineWidth.MULTIPLY = MOHO.MSG_BASE+5
SZ_AdjustLineWidth.WIDTH = MOHO.MSG_BASE+6
SZ_AdjustLineWidth.APPLY_WIDTH = MOHO.MSG_BASE+7
SZ_AdjustLineWidth.KEYFRAME = MOHO.MSG_BASE+8

function SZ_AdjustLineWidth:DoLayout(moho, layout)
	layout:AddChild(LM.GUI.StaticText(self:Localize("Modifier1")))
	self.modifier1Field = LM.GUI.TextControl(0, "0000", self.MODIFIER1, LM.GUI.FIELD_UFLOAT)
	self.modifier1Field:SetWheelInc(10)
	layout:AddChild(self.modifier1Field)
	
	self.add = LM.GUI.Button(self:Localize("Add"), self.ADD)
	layout:AddChild(self.add)
	
	self.subtract = LM.GUI.Button(self:Localize("Subtract"), self.SUBTRACT)
	layout:AddChild(self.subtract)
	
	layout:AddPadding(2)
	layout:AddChild(LM.GUI.Divider(true), LM.GUI.ALIGN_FILL)
	layout:AddPadding(2)
	
	layout:AddChild(LM.GUI.StaticText(self:Localize("Modifier2")))
	self.modifier2Field = LM.GUI.TextControl(0, "0000", self.MODIFIER2, LM.GUI.FIELD_UFLOAT)
	self.modifier2Field:SetWheelInc(.1)
	layout:AddChild(self.modifier2Field)
	
	self.multiply = LM.GUI.Button(self:Localize("Multiply"), self.MULTIPLY)
	layout:AddChild(self.multiply)
	
	self.divide = LM.GUI.Button(self:Localize("Divide"), self.DIVIDE)
	layout:AddChild(self.divide)	
	
	layout:AddPadding(2)
	layout:AddChild(LM.GUI.Divider(true), LM.GUI.ALIGN_FILL)
	layout:AddPadding(2)
	
	layout:AddChild(LM.GUI.StaticText(self:Localize("SetAllTo")))
	self.widthField = LM.GUI.TextControl(0, "0000", self.WIDTH, LM.GUI.FIELD_UFLOAT)
	layout:AddChild(self.widthField)
	self.widthField:SetWheelInc(10)
	self.applyWidthButton = LM.GUI.Button(self:Localize("ApplyWidth"), self.APPLY_WIDTH)
	layout:AddChild(self.applyWidthButton)
	
	layout:AddPadding(2)
	layout:AddChild(LM.GUI.Divider(true), LM.GUI.ALIGN_FILL)
	layout:AddPadding(2)
	
	self.createKeyframeCB = LM.GUI.CheckBox(self:Localize("CreateKeyframe"), self.KEYFRAME)
	layout:AddChild(self.createKeyframeCB)
	
	layout:AddPadding(2)
	layout:AddChild(LM.GUI.Divider(true), LM.GUI.ALIGN_FILL)
	layout:AddPadding(2)
	
	self.display = LM.GUI.DynamicText(self:Localize("Shapes").."000000 "..self:Localize("Points").."000000")
	layout:AddChild(self.display)
end

function SZ_AdjustLineWidth:UpdateWidgets(moho)
	self.modifier1Field:SetValue(self.modifier1)
	self.modifier2Field:SetValue(self.modifier2)
	self.widthField:SetValue(self.width)
	self.createKeyframeCB:SetValue(self.createKeyframe)
	self:UpdateStatistics(moho)
end

function SZ_AdjustLineWidth:HandleMessage(moho, view, msg)
	self.modifier1 = self.modifier1Field:FloatValue()
	self.modifier2 = self.modifier2Field:FloatValue()
	self.width = self.widthField:FloatValue()
	if (msg==self.MODIFIER2) then
		if(self.modifier2<0.000001) then
			self.modifier2 = 0.000001
			self.modifier2Field:SetValue(0.000001)
		end
	elseif (msg==self.KEYFRAME) then
		self.createKeyframe = self.createKeyframeCB:Value()
	elseif (msg==self.ADD) then
		self:ChangeStrokeWidth(moho, "add")
	elseif (msg==self.SUBTRACT) then
		self:ChangeStrokeWidth(moho, "subtract")
	elseif (msg==self.MULTIPLY) then
		self:ChangeStrokeWidth(moho, "multiply")
	elseif (msg==self.DIVIDE) then
		self:ChangeStrokeWidth(moho, "divide")		
	elseif (msg==self.APPLY_WIDTH) then
		self:ChangeStrokeWidth(moho, "apply_width")
	end
end

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

function SZ_AdjustLineWidth:ChangeStrokeWidth(moho, action)
	moho.document:PrepMultiUndo()
	moho.document:SetDirty()
	local docH = moho.document:Height()
	self:ScanLayers(moho)
	for k, id in ipairs(self.layersToChange) do
		local vectorLayer = moho:LayerAsVector(moho.document:LayerByAbsoluteID(id))
		local frame = 0
		if self.createKeyframe then
			frame = moho.layerFrame
		end
		local mesh = vectorLayer:Mesh()		
		for q=0, mesh:CountPoints()-1 do
			local point = mesh:Point(q)
			local w = point.fWidth.value
			if(w < 0 and w ~= -1) then w = 0 end
			if (w < 0) then
				local lineWidth = 0
				for i = 0, mesh:CountShapes() - 1 do
					local shape = mesh:Shape(i)
					local pointId = mesh:PointID(point)
					if (shape.fHasOutline and shape:ContainsPoint(pointId)) then
						lineWidth = shape.fMyStyle.fLineWidth
						if ((shape.fInheritedStyle ~= nil) and shape.fInheritedStyle.fDefineLineWidth and (not shape.fMyStyle.fDefineLineWidth)) then
							lineWidth = shape.fInheritedStyle.fLineWidth
						end
						if ((shape.fInheritedStyle2 ~= nil) and shape.fInheritedStyle2.fDefineLineWidth and (not shape.fMyStyle.fDefineLineWidth)) then
							lineWidth = shape.fInheritedStyle2.fLineWidth
						end
						break
					end
				end
				point.fWidth:SetValue(frame, lineWidth)
				w = lineWidth
			end
			
			if (action=="add") then
				point.fWidth:SetValue(frame, w + (self.modifier1/100))
			elseif (action=="subtract") then
				point.fWidth:SetValue(frame, w - (self.modifier1/100))
			elseif (action=="multiply") then
				point.fWidth:SetValue(frame, w * self.modifier2)
			elseif (action=="divide" and self.modifier2 ~= 0) then
				point.fWidth:SetValue(frame, w / self.modifier2)
			elseif (action=="apply_width") then
				point.fWidth:SetValue(frame, self.width/100)
			end
		end

	end
	if self.createKeyframe then 
		moho:NewKeyframe(CHANNEL_WIDTH)
		self:UpdateWidgets(moho)
		if moho:LayerAsVector(moho.layer) then
			moho:UpdateUI()
		end
	end
end

function SZ_AdjustLineWidth:ScanLayers(moho)
	for k in pairs(self.layersToChange) do
		self.layersToChange[k] = nil
	end
	for l = 0, moho.document:CountSelectedLayers()-1 do
		local layer = moho.document:GetSelectedLayer(l)
		if moho:LayerAsVector(layer) then
			table.insert(self.layersToChange, moho.document:LayerAbsoluteID(layer))
		elseif layer:IsGroupType() then
			self:ScanGroup(moho, layer)
		end
	end
end

function SZ_AdjustLineWidth:ScanGroup(moho, group)
	local groupLayer = moho:LayerAsGroup(group)
	for i=0, groupLayer:CountLayers()-1 do
		local layer = group:Layer(i)
		if layer:IsGroupType() then
			self:ScanGroup(moho, layer) -- recursion
		elseif moho:LayerAsVector(layer) then
			table.insert(self.layersToChange, moho.document:LayerAbsoluteID(layer))
		end
	end
end 

function SZ_AdjustLineWidth:UpdateStatistics(moho)
	self:ScanLayers(moho)
	local shapes = 0
	local points = 0
	for k, id in ipairs(self.layersToChange) do
		local VectorLayer = moho:LayerAsVector(moho.document:LayerByAbsoluteID(id))
		local mesh = VectorLayer:Mesh()
		shapes = shapes + mesh:CountShapes()
		points = points + mesh:CountPoints()
	end
	self.display:SetValue(self:Localize("Shapes")..shapes.."  "..self:Localize("Points")..points)
end

-- **************************************************
-- Localization
-- **************************************************
function SZ_AdjustLineWidth:Localize(text)
	local fileWord = MOHO.Localize("/Menus/File/File=File")
	
	local phrase = {}
	
	phrase["Name"] = "Adjust Line Width"
	phrase["Description"] = "Adjust the line width for the entire selected layer(s)"
	phrase["UILabel"] = "Adjust Line Width"
	phrase["Modifier1"] = "Percentage:"
	phrase["Add"] = "Add"
	phrase["Subtract"] = "Subtract"
	phrase["Modifier2"] = "Coefficient:"
	phrase["Divide"] = "Divide"
	phrase["Multiply"] = "Multiply"
	phrase["SetAllTo"] = "Set All Width To (%):"
	phrase["ApplyWidth"] = "Apply"
	phrase["CreateKeyframe"] = "Create Keyframe"
	phrase["Shapes"] = "Shapes: "
	phrase["Points"] = "Points: "
	
	if fileWord == "Файл" then
		phrase["Name"] = "Регулировать ширину контура"
		phrase["Description"] = "Изменяет ширину контура во всех выделенных слоях"
		phrase["UILabel"] = "Регулировать ширину контура"
		phrase["Modifier1"] = "Процентное число:"
		phrase["Add"] = "Прибавить"
		phrase["Subtract"] = "Вычесть"
		phrase["Modifier2"] = "Коэффициент:"
		phrase["Multiply"] = "Умножить"
		phrase["Divide"] = "Разделить"
		phrase["SetAllTo"] = "Установить всем ширину (%):"
		phrase["ApplyWidth"] = "Применить"
		phrase["CreateKeyframe"] = "Создать ключ"
		phrase["Shapes"] = "Форм: "
		phrase["Points"] = "Точек: "
	end
	
	return phrase[text];
end

Icon
Adjust Line Width
Listed

Author: Stan View Script
Script type: Tool

Uploaded: Dec 26 2020, 23:40

Script Version: 1.01

Adjust the line width by adding/subtracting or by multiplying/dividing.

Image

Image

The tool allows to adjust the line width by adding/subtracting a value or by multiplying/dividing by a provided coefficient.
It works for the entire selected layer(s), and supports vector layers and all types of group layers (including switch, bone, etc.) with any nesting depth.
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: 1100