Let's suggest new API features

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

Moderators: Víctor Paredes, Belgarath, slowtiger

User avatar
hayasidist
Posts: 3528
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Let's suggest new API features

Post by hayasidist »

Lukas wrote: Thu Aug 05, 2021 1:40 pm I did get something to work which seemed impossible before. And I imagine it's possible to maybe not have the window be local in a tool, but global in a utility? (I don't even know what I'm talking about, but hopefully one of you does) 😅
as far as I can tell, when you click on a tool its "DoLayout()" is invoked by the Moho core software, which in turn kicks off creation of the various elements ... so your code

Code: Select all


function LK_Experiment:DoLayout(moho, layout)
	if (self.experimentalWindow == nil) then
		self.experimentalWindow = LK_ExperimentDialog:new()
		self.experimentalWindow:DoModeless()
	end
end

-- ... etc
end
should be able to call the Dialog:new() even if it is in a different file -- but the LK_ExperimentDialog {} I think will need to be global so that the DoLayout can find it
Lukas wrote: Thu Aug 05, 2021 1:40 pm

Code: Select all

LK_ExperimentDialog = {} --<<< GLOBAL
function LK_ExperimentDialog:new()
	local d = LM.GUI.SimpleDialog("Experimental Window", LK_ExperimentDialog)
	local l = d:GetLayout()
-- snip
	return d
end
Haven't tried this, so apologies in advance if it goes pear-shaped...
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: Let's suggest new API features

Post by Lukas »

Thanks, I'll look into it.

The potential is huge. I tried to have it control smartbone dials, because I feel bones that are not actually part of a character, would be better of as sliders in a window you can drag anywhere. It's all very finicky and super crashy though:
Image
User avatar
hayasidist
Posts: 3528
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Let's suggest new API features

Post by hayasidist »

some headway but still a brickwall ..
the "moho" passed to the routine appears to be _G["MOHO"]

which can then be used to access the script interface table for things that would be called by (e.g.) " ScriptInterface:IsPro()"

e.g.

Code: Select all

	table.insert(MOHO.UpdateTable, HS_ForUpdate)
...

local function HS_ForUpdate(a)

	local msi = a["ScriptInterface"]
	local isPro = msi["IsPro"]
	print (type(isPro), (isPro())  --<< function, true

	print (msi["UserAppDir"]) --<< (a string)
...
BUT: when it comes to attributes, such as moho.layer, these seem to be defined as functions within a table ".get"

Code: Select all

	local attribs = msi[".get"]
	print (type(attribs)) --<< table

	for key,value in pairs(attribs) do
		print (key, value) --< list of "properties" showing that they're all of type "function"
	end
and I've so far failed to find a way to call one of those functions

e.g. local f = (a["ScriptInterface"][".get"]["frame"]()) crashes moho

any ideas??
User avatar
SimplSam
Posts: 1048
Joined: Thu Mar 13, 2014 5:09 pm
Location: London, UK
Contact:

Re: Let's suggest new API features

Post by SimplSam »

Redraw main Tool Layout (top) i.e. DoLayout()

This was touched on in an earlier post:
synthsin75 wrote:Technically possible now, but requires going to/from frame zero and back to redraw. Definitely need a tool option layout redraw function
Increasingly desperate for the ability to redraw a Tool's Toolbar and also the ability to update the Status bar text:
  1. Primarily to avoid overcrowded toolbars with contextually irrelevant features. Need the ability to have some parts of the toolbar included/omitted dependent on the sub-tool/settings selection and settings in the script code. We have the ability to enable/disable toolbar items, but this still takes up valuable real-estate.
  2. The Description / Info / Status bar - needs to be updatable at any time, so that info can be made relevant to sub-tool/settings/processing and for Message & Status updates. The ability to use color and a message timer would be a bonus.
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
davoodice2
Posts: 381
Joined: Tue Jun 15, 2021 1:14 pm

Re: Let's suggest new API features

Post by davoodice2 »

I don't know if exist now or not.
anyway

define left right and middle click for mouseEvent
create menu with right click on viewport
drawText method in GUI Graphics.
and access to other documents already are open.

I think those are needed too.
خیام اگر ز باده مستی خوش باش
با ماهرخی اگر نشستی خوش باش
چون عاقبت کار جهان نیستی است
انگار که نیستی چو هستی خوش باش
User avatar
synthsin75
Posts: 9981
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Let's suggest new API features

Post by synthsin75 »

davoodice2 wrote: Mon Nov 22, 2021 8:59 am define left right and middle click for mouseEvent
create menu with right click on viewport
drawText method in GUI Graphics.
I have an experimental context menu script that uses double click to invoke the menu. I have been using note layers, but they're way to slow. So I probably need to use Vern's GUI font.
User avatar
davoodice2
Posts: 381
Joined: Tue Jun 15, 2021 1:14 pm

Re: Let's suggest new API features

Post by davoodice2 »

Lukas wrote: Thu Aug 05, 2021 1:40 pm
hayasidist wrote: Thu Aug 05, 2021 10:10 amHowever, the "moho" that the called routine gets doesn't seem to be the same as I was expecting - the above reports t as "nil" ... maybe something wrong with the way I'm setting things up -- but right now I'm at a bit of a brick wall.
I've also been dabbling and it's all very confusing...

I also touched that brick wall a lot, and moho.layer == nil fires both true and false at the same time. (Obviously not at the same time, but the function fires twice I guess)

I did get something to work which seemed impossible before. And I imagine it's possible to maybe not have the window be local in a tool, but global in a utility? (I don't even know what I'm talking about, but hopefully one of you does) 😅

Save this as a tool:

Code: Select all

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

ScriptName = "LK_Experiment"

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

LK_Experiment = {}

LK_Experiment.BASE_STR = 2325

function LK_Experiment:Name()
	return "LK_Experiment...Name"
end

function LK_Experiment:Version()
	return "0.1"
end

function LK_Experiment:Description()
	return "LK_Experiment...Description"
end

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

function LK_Experiment:UILabel()
	return "LK_Experiment...UILabel"
end

function LK_Experiment:ColorizeIcon()
	return true
end

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

LK_Experiment.matrix = LM.Matrix:new_local()
LK_Experiment.experimentalWindow = nil

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

function LK_Experiment:OnInputDeviceEvent(moho, deviceEvent)
	--
end

function LK_Experiment:OnMouseDown(moho, mouseEvent)
	--
end

function LK_Experiment:OnMouseMoved(moho, mouseEvent)
	--
end

-- **************************************************
-- Layer Settings dialog
-- **************************************************

local LK_ExperimentDialog = {}

LK_ExperimentDialog.CHANGE = MOHO.MSG_BASE
LK_ExperimentDialog.XPOS = MOHO.MSG_BASE + 1

function LK_ExperimentDialog:new()
	local d = LM.GUI.SimpleDialog("Experimental Window", LK_ExperimentDialog)
	local l = d:GetLayout()

	l:PushH()
		d.frame = LM.GUI.DynamicText("Frame 00000", 80)
		l:AddChild(d.frame, LM.GUI.ALIGN_LEFT)
		-- * LM.GUI.Slider(length, vertical, showTicks, msg, resizingMode)
		d.slider = LM.GUI.Slider(200, false, false, LK_ExperimentDialog.CHANGE, LM_FOLLOW_LEFT)
		l:AddChild(d.slider)
	l:Pop()
	l:PushH()
		l:AddChild(LM.GUI.StaticText("Layer X translation:"))
		d.pX = LM.GUI.TextControl(0, "00.0000", LK_ExperimentDialog.XPOS, LM.GUI.FIELD_FLOAT)
		l:AddChild(d.pX, LM.GUI.ALIGN_LEFT)
	l:Pop()
	return d
end


function LK_ExperimentDialog:Update(moho)
	if (moho.layer == nil) then
		-- print ("moho.layer == nil (?!)")
		return
	end
	-- print ("moho.layer ~= nil (WHAT?)")

	self.frame:SetValue("Frame "..moho.frame)
	self.slider:SetRange(moho.document:StartFrame(), moho.document:EndFrame())
	self.slider:SetValue(moho.frame)

	local layer = moho.layer
	self.pX:SetValue(layer.fTranslation.value.x)
end

function LK_ExperimentDialog:OnOK()
	LK_Experiment.experimentalWindow = nil -- mark the window closed
end

function LK_ExperimentDialog_Update(moho)
	if (LK_Experiment.experimentalWindow) then
		LK_Experiment.experimentalWindow:Update(moho)
	end
end

-- register the layer window to be updated when changes are made
table.insert(MOHO.UpdateTable, LK_ExperimentDialog_Update)

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

LK_Experiment.CHANGE = MOHO.MSG_BASE
LK_Experiment.XPOS = MOHO.MSG_BASE + 1

function LK_Experiment:DoLayout(moho, layout)
	if (self.experimentalWindow == nil) then
		self.experimentalWindow = LK_ExperimentDialog:new()
		self.experimentalWindow:DoModeless()
	end
end

function LK_Experiment:UpdateWidgets(moho)
	if (self.experimentalWindow) then
		self.experimentalWindow:Update(moho)
	end
end

function LK_Experiment:HandleMessage(moho, view, msg)
	if (msg == self.CHANGE) then
		-- * Slide to new frame:
		local newFrame = LK_Experiment.experimentalWindow.slider:Value()
		moho:SetCurFrame(newFrame)
	end
	if (msg == self.XPOS) then
		-- * Prep undo:
		moho.document:PrepUndo(moho.layer, true)
		moho.document:SetDirty()
		-- * Key layer translation:
		local layerPos = moho.layer.fTranslation:GetValue(moho.frame)
		layerPos:Set (LK_Experiment.experimentalWindow.pX:FloatValue(), layerPos.y, layerPos.z)
		moho.layer.fTranslation:SetValue(moho.frame, layerPos)
		-- * Update timeline:
		moho:UpdateUI()
	end
end
It will popup a dialog that is able to get and set the current frame of the timeline. And also get and set the translation of the current layer.

If you select a different tool, the dialog becomes silent, until you reselect the tool again.

But... If you add handle message and update widget functions to the ones of another tool, that tool is also able to keep the window active!

Code: Select all

function LM_SomeOtherTool:HandleMessage(moho, view, msg)
	LK_Experiment:HandleMessage(moho, view, msg)

Code: Select all

function LM_SomeOtherTool:UpdateWidgets(moho)
	if (LK_Experiment.experimentalWindow) then
		LK_Experiment.experimentalWindow:Update(moho)
	end
I tested it in moho 13.5.2 . slider doesn't change any thing.are you sure code is true? I can't see LK_ExperimentDialog:HandleMessage
خیام اگر ز باده مستی خوش باش
با ماهرخی اگر نشستی خوش باش
چون عاقبت کار جهان نیستی است
انگار که نیستی چو هستی خوش باش
User avatar
Lukas
Posts: 1297
Joined: Fri Apr 09, 2010 9:00 am
Location: Netherlands
Contact:

Re: Let's suggest new API features

Post by Lukas »

davoodice2 wrote: Fri Dec 10, 2021 12:09 pmI tested it in moho 13.5.2 . slider doesn't change any thing.are you sure code is true? I can't see LK_ExperimentDialog:HandleMessage
I'm not sure, but I think the message was only forwarded to the tool because of a bug in 13.5 that has now been fixed. I think 'Moho' should still be updated in the modal window though. I don't have any time to test it any time soon though...
Post Reply