get missed images

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
davoodice2
Posts: 381
Joined: Tue Jun 15, 2021 1:14 pm

get missed images

Post by davoodice2 »

hi.
how to detect image layer's source is missed or not!
is there some thing like:
ImageLayer:SourceIsMissed() ? :D :D
خیام اگر ز باده مستی خوش باش
با ماهرخی اگر نشستی خوش باش
چون عاقبت کار جهان نیستی است
انگار که نیستی چو هستی خوش باش
User avatar
synthsin75
Posts: 9975
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: get missed images

Post by synthsin75 »

Check to see if ImageLayer:SourceImage() is a valid path, using io.open. Be sure to close the file, io.close(path), if it's valid.
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: get missed images

Post by SimplSam »

An alternative file test in Lua is to use os.rename, where you try to rename a file to itself. This will do nothing to the file - but will return true if the file is renamable, and nil* if the file is missing etc.

Code: Select all

if os.rename(myfile, myfile) then 
  DoSomething 
end
*Note: os.rename will also return the reason if the rename fails.

Code: Select all

result, reason = os.rename(myfile, myfile)
Moho 14.1 » Win 11 Pro 64GB » NVIDIA GTX 1080ti 11GB
Moho 14.1 » Mac mini 2012 8GB » macOS 10.15 Catalina
Tube: SimplSam


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

Re: get missed images

Post by synthsin75 »

io.open can also give error messages, but os.rename is more efficient. Good one, Sam.
User avatar
davoodice2
Posts: 381
Joined: Tue Jun 15, 2021 1:14 pm

Re: get missed images

Post by davoodice2 »

thanks and excuse me for more questions.

why when we import psd as layers, scale is always equal to 1. but when we replace it, it's actual size in viewport change?(when it is into scaled group).
I wrote a script to fix that. but with change scale of layer. and I sure it is bad. can I replace psd with code without changing scale and preserve layer size?
and I noticed when I replace a layer that is into a scaled group with moho itself, when I use undo right after replacement, size of image will fixed.
خیام اگر ز باده مستی خوش باش
با ماهرخی اگر نشستی خوش باش
چون عاقبت کار جهان نیستی است
انگار که نیستی چو هستی خوش باش
User avatar
synthsin75
Posts: 9975
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: get missed images

Post by synthsin75 »

Lukas has dealt with that more than I. Maybe he'll chime in.
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: get missed images

Post by Lukas »

Yeah, this is troublesome and I wish image Height and Width would not be related to the project Size at all but here we are. It would be nice to be able to set image Height and Width by script, but we can't.

I currently also adjust the scale of image layers, I'm not happy about messing up the files, but there's currently no other way:

Code: Select all

	-- * Check old size:
	local oldHeight = imageLayer:Height()
	imageLayer:SetSourceImage(imagePath)
	-- * Compare to new size:
	local newHeight = imageLayer:Height()
	-- * Check if difference is significant:
	local heightDifference = math.abs (oldHeight - newHeight)
	if heightDifference < 0.1 then
		heightDifference = 0
	end
	-- * Change size on frame 0 and timeline:
	if heightDifference ~= 0 then --newHeight ~= oldHeight then
		self.changedScale = true
		local heightChange = oldHeight / newHeight
		local scaleChannel = imageLayer.fScale
		local oldScale = scaleChannel:GetValue(0)
		local newScale = oldScale * heightChange
		-- * Main timeline scale channel for this image layer:
		local endKey = scaleChannel:Duration()
		local thisKey = 0
		local keyCount = scaleChannel:CountKeys()
		local keysFound = 0
		local frameNum = endKey
		while keysFound < keyCount do
			thisKey = scaleChannel:GetClosestKeyID(frameNum)
			keyFrameNum = scaleChannel:GetKeyWhen(thisKey)
			keysFound = 1 + keysFound
			-- * Scale it:
			local oldScale = scaleChannel:GetValue(keyFrameNum)
			local newScale = oldScale * heightChange
			scaleChannel:SetValue(keyFrameNum, newScale)
			frameNum = keyFrameNum - 1
		end
		-- * Do all action scale channels for this image layer too:
		for i = 0, scaleChannel:CountActions()-1 do
			local actionChannel = moho:ChannelAsAnimVec3(scaleChannel:Action(i))
			local endKey = actionChannel:Duration()
			local thisKey = 0
			local keyCount = actionChannel:CountKeys()-1 -- * -1 because we don't want to modify frame 0 in actions!
			local keysFound = 0
			local frameNum = endKey
			while keysFound < keyCount do
				thisKey = actionChannel:GetClosestKeyID(frameNum)
				keyFrameNum = actionChannel:GetKeyWhen(thisKey)
				keysFound = 1 + keysFound
				-- * Scale it:
				local oldScale = actionChannel:GetValue(keyFrameNum)
				local newScale = oldScale * heightChange
				actionChannel:SetValue(keyFrameNum, newScale)
				frameNum = keyFrameNum - 1
			end
		end
	end
Post Reply