meshinstance work in 10.1?

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
madrobot
Posts: 667
Joined: Mon Apr 07, 2008 3:07 pm

meshinstance work in 10.1?

Post by madrobot »

Hi All
(Long time, hope you're well. I'm well thanks, lost a little weight.... Aaaanyway)

I am running 9.5, looking to upgrade to 10.1 (10.2?)
The big question for me is, does the meshinstance script work in 10.x?
It's an important question for me, as my rigs are lousey with meshinstance.

Thankyou for your time
I love you all equally, and without favour.
Adrian
User avatar
Víctor Paredes
Site Admin
Posts: 5646
Joined: Wed Jan 26, 2005 12:18 am
Location: Barcelona/Chile
Contact:

Re: meshinstance work in 10.1?

Post by Víctor Paredes »

Just tested "syn_rl_merge_meshinstance_final_4.lua" and it seems to be working well.

Code: Select all

--[[
	MeshInstance: Instancing the positions of mesh points on vector layers
	1. Use NAME for the name of the source layer, and NAME.<any or no name>.dup for all of the other layers
	   Like this: source: arm ---> instance: "arm.anyname.dup" or "arm.dup".
	2. Set this script for all of the layers (ie. for the original and for the instances)
	3. Modify always the source layer only.
]]--

-- written by Fazek -- modified by Synthsin75 (With help from Heyvern)

function LayerScript(moho)
	local mesh= moho:Mesh()
	if (mesh == nil) then return end

	local ownLayer= moho.layer
	local name= ownLayer:Name()
	local tab
	local mi= ownLayer.FA_MeshInst
	local len= mesh:CountPoints()
	local instName = ""
	local a, b
	local refname
	local curve = nil
	local curvePointID = -1
	
	if(string.find(name, ".", 1, true) ~= nil) then
	a, b = string.find(name, ".", 1, true)

	instName = string.sub(name, b)
	--print (instName)
	end
	if (string.find(name,instName, 1, true) ~= nil) then
	--print (instName)
	end
	if (string.sub(name,-4) == ".dup") then
	local refname
--copy instance
		if (mi and ownLayer.FA_MeshInstName == name) then
	--cached
			if (mi[2] ~= moho.frame) then
				print("MeshInstance: wrong order - swap the source and the copy!")
				return
			end
			tab= mi[1]

			if (mi[3] < len) then len= mi[3] end

			for p= 1, len do
				local pt= mesh:Point(p - 1)
				pt.fPos.x= tab[p][1]
				pt.fPos.y= tab[p][2]
				--pt.SetCurvature(tab[p][3], moho.layerFrame)
				
				for j = 0, pt:CountCurves() - 1 do --added by Ramon0 from here...
					curve, curvePointID = pt:Curve(j, curvePointID)
					
					if tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)) ~= tonumber(tab[p][3]) then
						--print("Slave---"..tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)))
						--print("Master---"..tonumber(tab[p][3]))
						curve:SetCurvature(curvePointID, tab[p][3], moho.layerFrame)
					end
				end --...to here.
			end
		else
			__,__,pref = string.find(name, "(.-)%.+") --name prefix
			local __,__,sufx = string.find(name, "%..+(%..+)") --name suffix
      local __,__,sufx2 = string.find(name, ".-(%..+)") --alternate suffix
      if (sufx ~= nil) then
        refname = pref..sufx --reference name
        elseif (sufx == nil) then
        refname = pref..sufx2
       end
        
	  --print(refname)
	  
			local srcName= string.sub(refname,1,-5)
		-- recursive search for the source

			local stack= {}
			local sp= 0
			local document= moho.document

			for i= 0, document:CountLayers() - 1 do
				local layer= document:Layer(i)
				local parent= nil
				local sub= 0

				while true do

					if (layer:IsGroupType()) then
						table.insert(stack, { parent, sub - 1 })
						sp= sp + 1
						parent= moho:LayerAsGroup(layer)
						sub= parent:CountLayers()
					elseif (layer:Name() == srcName) then
						local mi= layer.FA_MeshInst

						if (mi) then

							if (mi[2] == moho.frame) then
								tab= mi[1]

								if (mi[3] < len) then len= mi[3] end

								for p= 1, len do
									local pt= mesh:Point(p - 1)
									pt.fPos.x= tab[p][1]
									pt.fPos.y= tab[p][2]
									--print(tab[p][1])
									
									for j = 0, pt:CountCurves() - 1 do --added by Ramon0 from here...
										curve, curvePointID = pt:Curve(j, curvePointID)
										if tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)) ~= tonumber(tab[p][3]) then
											--print("Slave---"..tonumber(curve:GetCurvature(curvePointID, moho.layerFrame)))
											--print("Master---"..tonumber(tab[p][3]))
											curve:SetCurvature(curvePointID, tab[p][3], moho.layerFrame)
										end
									end --...to here.
								end
								ownLayer.FA_MeshInst= mi
								ownLayer.FA_MeshInstName= refname
								return
							end
						end
					end

					if (sub > 0) then
						sub= sub - 1
						layer= parent:Layer(sub)
					else
						sub= -1

						while (sp > 0) do
							parent, sub= stack[sp][1], stack[sp][2]
							table.remove(stack)
							sp= sp - 1

							if (sub >= 0) then
								layer= parent:Layer(sub)
								break
							end
						end

						if (sub < 0) then
							break
						end
					end
				end
			end
		end
	else
--source
		tab= {}

		for p= 0, len - 1 do
			local pt= mesh:Point(p)
			local curve = nil
			local curvePointID = -1
			
			for j = 0, pt:CountCurves() - 1 do --Added by Ramon0 from here...
				curve, curvePointID = pt:Curve(j, curvePointID)			
				--table.insert(tab, { pt.fPos.x, pt.fPos.y, curve:GetCurvature(curvePointID, moho.layerFrame) })
				--for k,v in ipairs(tab) do print(k,v) end
			end --...to here.
				table.insert(tab, { pt.fPos.x, pt.fPos.y, curve:GetCurvature(curvePointID, moho.layerFrame) })
				--for k,v in ipairs(tab) do print(k,v) end
		end
	--trick: FA_MeshInst won't change in the address space (no orphans)

		if (mi and not ownLayer.FA_MeshInstName == name)
		then
		
	--cached
			mi[1]= tab
			mi[2]= moho.frame
			mi[3]= len
		else
			ownLayer.FA_MeshInst= { tab, moho.frame, len }
			ownLayer.FA_MeshInstName= name

			mi= ownLayer.FA_MeshInst
			name= name..".dup"
			

	--avoid orphaning: update all dependencies

		-- recursive search for the copy

			local stack= {}
			local sp= 0
			local document= moho.document

			for i= 0, document:CountLayers() - 1 do
				local layer= document:Layer(i)
				local parent= nil
				local sub= 0

				while true do

					if (layer:IsGroupType()) then
						table.insert(stack, { parent, sub - 1 })
						sp= sp + 1
						parent= moho:LayerAsGroup(layer)
						sub= parent:CountLayers()
					elseif (layer:LayerType() == MOHO.LT_VECTOR and layer:Name() == name) then
						layer.FA_MeshInst= mi
						layer.FA_MeshInstName= name
					end

					if (sub > 0) then
						sub= sub - 1
						layer= parent:Layer(sub)
					else
						sub= -1

						while (sp > 0) do
							parent, sub= stack[sp][1], stack[sp][2]
							table.remove(stack)
							sp= sp - 1

							if (sub >= 0) then
								layer= parent:Layer(sub)
								break
							end
						end

						if (sub < 0) then
							break
						end
					end
				end
			end
		end
	end
end
Image Image Image Image
Moho Product Manager

www.mohoanimation.com
Rigged animation supervisor in My father's dragon - Lead Moho artist in Wolfwalkers - Cartoon Saloon - My personal Youtube Channel
User avatar
madrobot
Posts: 667
Joined: Mon Apr 07, 2008 3:07 pm

Re: meshinstance work in 10.1?

Post by madrobot »

Thanks Selgin
I think the course of action for me will be to upgrade to 10, test my files, and hopefully transition to 10 from 9.5.

There appears to be a lot to gain in 10 over 9.5, for me the drawing tools will be helpful but also a big factor is the issue of appended style names appearing to have been addressed. I have built separate character rig files which I then import into scene files with respective backgrounds, and the style name bloat issue is a significant time sink (in 9.5.)

Thanks again
Adrian
Post Reply