Change Image Resolution Script

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Change Image Resolution Script

Post by dkwroot »

WHAT THIS SCRIPT DOES:
I created two menu scripts, "image up" and "image down". When you activate "Image Up" from the scripts menu, the script will go through every layer in your document and replace images with higher resolution versions. When you use "Image down", the script will go through every layer and replace images with lower resolution versions.

WHY IS THIS USEFUL?
When creating complex rigs, you will often find that Moho will struggle and even crash when there are too many high resolution images. It is much easier and faster to use low resolution images for animating and then swap those low resolution images to high resolution ones for the rendering process. These scripts make it easy to do that.

HOW DO I SET THIS UP?
It is very simple to set this up. All you have to do is keep two copies of each image you want to work with in the same folder location. One copy will be a smaller version of the other. Just make certain that the file names of the two images are the same, except the large copy will have (large) in its name and the smaller copy will have (small) in its name.

EXAMPLE: Face_(large).png , Face_(small).png

HOW DO I INSTALL?
Locate your custom content folder. If you don't know where it is, just open Moho and go to edit>preferences. Under the general tab, you should your custom content file location. Go into that folder and navigate to: Moho Pro>scripts>menu
Finally, unzip the scripts provided into the menu folder and you're done.


DOWNLOAD LINK: n/a
Last edited by dkwroot on Sat Oct 02, 2021 1:42 pm, edited 1 time in total.
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Change Image Resolution Script

Post by dkwroot »

I created two companion scripts. These scripts will go through your document and swap out (large) and (small) in your layer names. This is useful for those that want to build characters in moho and export them to a psd file to be edited and exported back to moho.

My method:
- First design your character in Moho. Don't place any layers into groups! Just work with vectors only.
- Make sure all of your layers have (large) somewhere in their name.
- In file>Project Settings make your render size very large. This is going to be the largest you expect to ever zoom in on your character, so make the resolution as large as you think you might need.
- Export your character to an image sequence. Set the file type to "psd" and export only from frame 1 to frame 1. This will give 1 psd file with all of your files rendered out.
- Open your psd file in Photoshop and edit your art however you want. Feel free to add new layers, just make sure (large) is in their name.
- From file>scripts export a moho file. If you don't have the moho export script, you can find it in your custom content folder: Moho Pro>Extra Files the file is named "Export Layers to Moho". Place this file inside of photoshop \Program Files\Adobe\Adobe Photoshop CS6 (64 Bit)\Presets\Scripts
- Open the moho document saved from Photoshop. Take out all of the images from the group and make sure no layers are in a group.
- Under menu scripts activate "Rename Layer (small)" This will change the (large) in layer names to (small).
- In file>Project Settings make your render size small.
- Export a psd file like before.
- Open the psd file in Photoshop and save another moho file.
- You will find a folder is exported with your moho file. This folder contains cropped versions of all of your images. Cut all of the small images out of this folder and paste them into the folder created with the large versions. Delete the moho file created from the small psd document.
- When you open the moho file created from the large images, go to menu scripts and activate "Decrease Image Resolution". All of your images will drop in resolution and be easier to animate.
-When ready to render, go to menu scripts and active "Increase image resolution". All of your images will become very high resolution!

DOWNLOAD: https://drive.google.com/open?id=0Bwzk- ... WZ3TDBRMmM
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Change Image Resolution Script

Post by dkwroot »

Wow, it turns out that it's pretty easy to write code for Photoshop. I created two photoshop scripts that will rename the (large) in names to (small) and another to do the reverse. They work really well and make it so you don't have to go back and forth or mess around with a whole lot of renaming.

Just download the script and put it in your photoshop scripts folder.

DOWNLOAD: https://drive.google.com/open?id=0Bwzk- ... 2psM0NyejQ
User avatar
synthsin75
Posts: 9935
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Change Image Resolution Script

Post by synthsin75 »

VB is pretty easy, but alas, I've never gotten around to really learning Javascript yet.
Stan
Posts: 199
Joined: Sun Apr 19, 2009 3:22 pm

Re: Change Image Resolution Script

Post by Stan »

dkwroot wrote:Wow, it turns out that it's pretty easy to write code for Photoshop.
LOL!)) Let me rephrase that as "Wow, Photoshop scripting API is in four languages, and well documented!". I have done some Photoshop scripting before, and I really understand your "wow" here.

Unfortunately, many other companies just cannot afford their scripting API to be that flexible and documented that well.
________________________________________________________________________
https://mohoscripting.com/ - Unofficial Moho Lua scripting documentation
https://mohoscripts.com/ - The best place to publish and download scripts for Moho
User avatar
jahnocli
Posts: 3471
Joined: Fri Oct 29, 2004 2:13 pm
Location: UK

Re: Change Image Resolution Script

Post by jahnocli »

Stan wrote:...Unfortunately, many other companies just cannot afford their scripting API to be that flexible and documented that well.
That's code in more ways than one...
You can't have everything. Where would you put it?
User avatar
dueyftw
Posts: 2174
Joined: Thu Sep 14, 2006 10:32 am
Location: kingston NY
Contact:

Re: Change Image Resolution Script

Post by dueyftw »

I wondering if there is a way to use the 2d mesh with the size change? So the mesh could get smaller and bigger also.

Dale
ernpchan
Posts: 154
Joined: Thu Aug 13, 2015 7:18 pm
Contact:

Re: Change Image Resolution Script

Post by ernpchan »

dkwroot,

Do you have a EULA associated with your scripts?
My opinions and comments do not represent those of my employer.
http://www.ernestpchan.com
http://www.zazzle.com/gopuggo
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Change Image Resolution Script

Post by dkwroot »

ernpchan wrote:dkwroot,

Do you have a EULA associated with your scripts?
All I ask is that no one try to sell the code or anything like that. You can use them in your personal or commercial projects without any royalties.
ernpchan
Posts: 154
Joined: Thu Aug 13, 2015 7:18 pm
Contact:

Re: Change Image Resolution Script

Post by ernpchan »

That's great but do you have an actual EULA that can be downloaded and read. For those of us in a studio/corporate setting, this is a requirement.
My opinions and comments do not represent those of my employer.
http://www.ernestpchan.com
http://www.zazzle.com/gopuggo
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Change Image Resolution Script

Post by dkwroot »

ernpchan wrote:That's great but do you have an actual EULA that can be downloaded and read. For those of us in a studio/corporate setting, this is a requirement.
No, I don't have anything like that.
User avatar
TheMinahBird
Posts: 263
Joined: Sat Jul 27, 2013 11:10 pm

Re: Change Image Resolution Script

Post by TheMinahBird »

Please tell me someone has this script and could possibly reshare it? 🥺
"Now hatred is by far the longest pleasure; men love in haste but they detest at leisure" - Lord Byron
User avatar
Lukas
Posts: 1295
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: Change Image Resolution Script

Post by Lukas »

TheMinahBird wrote: Wed Nov 15, 2023 4:52 pm Please tell me someone has this script and could possibly reshare it? 🥺
Here's my own version that does the same thing, kinda.
I'm not sure if it's in a working state and how to exactly use it, it's old and haven't used it in a long time. So you might need to dig around in the code and fix some stuff to make it work for your situation.
It might only work correctly with 1920*1080 moho files (and all might need to be imported while the moho doc is at that resolution - weird Moho quirk), as that's what we use.
If I remember correctly need to have your image next to each other in their respective folders like this:
  • my_image.png (HQ image)
  • my_image_LQ25.png (25% resolution of original file)
You might need to dig around in the code. I haven't used it in a long time.

Code: Select all

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

ScriptName = "LK_ToggleImageQuality"

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

LK_ToggleImageQuality = {}

function LK_ToggleImageQuality:Name()
	return "Toggle Image Quality LQ/HQ"
end

function LK_ToggleImageQuality:Version()
	return "0.1"
end

function LK_ToggleImageQuality:Description()
	return "Toggle Image Quality LQ/HQ"
end

function LK_ToggleImageQuality:Creator()
	return "Lukas Krepel, Frame Order"
end

function LK_ToggleImageQuality:UILabel()
	return "Toggle Image Quality LQ/HQ"
end

function LK_ToggleImageQuality:ColorizeIcon()
	return true
end

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

LK_ToggleImageQuality.layersDone = {}
LK_ToggleImageQuality.HQwidth = 1920
LK_ToggleImageQuality.HQheight = 1080
LK_ToggleImageQuality.HQtoLQ = true
-- LK_ToggleImageQuality.percentages = { "01", "02", "03", "04", "05", "10", "25" }
LK_ToggleImageQuality.percentage = "25"
LK_ToggleImageQuality.LQscale = 1/4

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

function LK_ToggleImageQuality:IsEnabled(moho)
	return true
end

function LK_ToggleImageQuality:IsRelevant(moho)
	if MohoMode ~= nil then
		return MohoMode.experimental
	else
		return true
	end
end

function LK_ToggleImageQuality:Run(moho)
	moho.document:SetDirty()
	moho.document:PrepUndo(moho.layer)

	local messages = { "i" }
	-- * Remember doc resolution:
	local docHeight = moho.document:Height()
	local docWidth = moho.document:Width()

	local layers = FO_Utilities:AllLayers(moho)

	if self.HQtoLQ then
		-- *****************
		-- ** LOW QUALITY **
		-- *****************
		table.insert(messages, "LOW QUALITY")
		for i = 1, 99 do--#self.percentages do
			self.percentage = tostring(i)
			if string.len(self.percentage) == 1 then
				self.percentage = "0"..self.percentage
			end
			self.LQscale = i/100 --tonumber(self.percentage)/100

			-- * Scale doc resolution:			
			moho.document:SetShape(self.HQwidth * self.LQscale, self.HQheight * self.LQscale)

			self.layersDone = {}
			
			for i = 1, #layers do
				local layer = layers[i]
				if (layer:LayerType() == MOHO.LT_IMAGE) then
					self:ChangeQuality(moho, layer, self.HQtoLQ)
				end
			end
			-- *
			for i = 1, #self.layersDone do
				local layer = self.layersDone[i]
				table.delete(layers, layer)
			end
			-- *
			if #self.layersDone > 0 then
				msg = "- Scaled "..#self.layersDone.." images to "..tonumber(self.percentage).."% resolution."
				table.insert(messages, msg)
			end
			-- * Reset doc resolution:
			moho.document:SetShape(docWidth, docHeight)
		end
	else
		-- ******************
		-- ** HIGH QUALITY **
		-- ******************
		table.insert(messages, "HIGH QUALITY")
		-- * Scale doc resolution:
		moho.document:SetShape(self.HQwidth, self.HQheight)
		self.layersDone = {}
		local layers = FO_Utilities:AllLayers(moho)
		for i = 1, #layers do
			local layer = layers[i]
			if (layer:LayerType() == MOHO.LT_IMAGE) then
				self:ChangeQuality(moho, layer, self.HQtoLQ)
			end
		end
		if #self.layersDone > 0 then
			msg = "- Scaled "..#self.layersDone.." images to 100% resolution."
			table.insert(messages, msg)
		end
		-- * Reset doc resolution:
		moho.document:SetShape(docWidth, docHeight)
	end
	self.HQtoLQ = not self.HQtoLQ
	moho:UpdateUI()
	-- * Reset doc resolution:
	moho.document:SetShape(docWidth, docHeight)
	-- *
	if #messages == 2 then
		table.insert(messages, "- No images were changed.")
	end
	FO_Utilities:Alert(messages)
end

-- **************************************************
-- Changing the path on an image layer
-- **************************************************
function LK_ToggleImageQuality:ChangeQuality(moho, layer, LQ)
	local imageLayer = moho:LayerAsImage(layer)
	local imagePath = imageLayer:SourceImage()
	if LQ then
		if string.match(imagePath, "_LQ") then
			return false
		end
		imagePath = string.gsub(imagePath,"%.","_LQ"..self.percentage..".")
	else
		imagePath = string.gsub(imagePath,"_LQ%d%d","")
	end
	-- * Check if path has changed:
	if (imagePath == imageLayer:SourceImage()) then
		return false
	end
	-- * Check if file exists:
	if not FO_Utilities:FileExists(moho, imagePath) then
		Debug:Log("Unable to locate image file for layer ["..layer:Name().."] File doesn't exist: ["..imagePath.."]")
		return false
	end
	imageLayer:SetSourceImage(imagePath)
	table.insert(self.layersDone, layer)
	return true
end
You'll need FO_Utilities
Post Reply