Page 1 of 1

get missed images

Posted: Sun Nov 07, 2021 2:22 pm
by davoodice2
hi.
how to detect image layer's source is missed or not!
is there some thing like:
ImageLayer:SourceIsMissed() ? :D :D

Re: get missed images

Posted: Sun Nov 07, 2021 11:50 pm
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.

Re: get missed images

Posted: Mon Nov 08, 2021 3:26 am
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)

Re: get missed images

Posted: Mon Nov 08, 2021 5:39 am
by synthsin75
io.open can also give error messages, but os.rename is more efficient. Good one, Sam.

Re: get missed images

Posted: Mon Nov 08, 2021 9:21 am
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.

Re: get missed images

Posted: Mon Nov 08, 2021 4:48 pm
by synthsin75
Lukas has dealt with that more than I. Maybe he'll chime in.

Re: get missed images

Posted: Mon Nov 15, 2021 8:31 am
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