Page 1 of 1

Graphics class help

Posted: Wed Apr 18, 2018 12:43 pm
by dkwroot
Does anyone have any good resources on using the graphics class? I'm trying to draw a bunch of circles on the viewport, but I'm struggling to figure out how this works. Specifically, I'm trying to use LM_Graphics:FillCircle(center, radius) to draw a bunch of vector points onto the viewport, but I keep getting errors.

At the moment, I'm using this:

Code: Select all

	
        local g = moho.view:Graphics()
	g:Push()
	local m = g:CurrentTransform()
	m:Invert()
	g:ApplyMatrix(m)
	g:SetColor(MOHO.MohoGlobals.SelCol)
	g:BeginShape()
	for i,k in ipairs(points) do
	g:FillCircle(k, 10)
	end
	g:EndShape()
	g:Pop()
	moho.view:RefreshView()
	moho.view:DrawMe()
where points is a table of vectors, but it doesn't work.

Re: Graphics class help

Posted: Wed Apr 18, 2018 4:36 pm
by synthsin75
Since 1 is the height of the project (if I remember correctly), 10 is way too large a FillCircle radius. And you probably need to define the matrix object and set or transform it ( http://mohoscripting.com/index.php?show ... =LM_Matrix ), instead of trying to assign it a value.

Code: Select all

	local points = {}
	local mesh = moho:Mesh()
	for i=0, mesh:CountPoints()-1 do
		table.insert(points, mesh:Point(i).fPos)
	end
	local g = view:Graphics()
	local matrix = LM.Matrix:new_local()
	moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
	g:Push()
		g:ApplyMatrix(matrix)
		g:SetColor(MOHO.MohoGlobals.SelCol)
		for i,k in ipairs(points) do
			g:FillCircle(k, .1)
		end
	g:Pop()
	moho.view:RefreshView()
	moho.view:DrawMe()

Re: Graphics class help

Posted: Wed Apr 18, 2018 4:58 pm
by dkwroot
It says it can't index global 'view' (a nil value). I tried using moho.view instead, but it doesn't draw anything.

Re: Graphics class help

Posted: Wed Apr 18, 2018 5:04 pm
by synthsin75
The function it's in needs to be supplied "view" or "moho.view" as an argument.

Re: Graphics class help

Posted: Wed Apr 18, 2018 5:12 pm
by dkwroot
I passed view into the function, but it's still throwing an error. It says the same thing about view being a nil. This is the simple function I'm trying to run:

Code: Select all


function DR_TEST:Run(moho,view)
   local points = {}
   local mesh = moho:Mesh()
   for i=0, mesh:CountPoints()-1 do
      table.insert(points, mesh:Point(i).fPos)
   end
   local g = view:Graphics()
   local matrix = LM.Matrix:new_local()
   moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
   g:Push()
      g:ApplyMatrix(matrix)
      g:SetColor(MOHO.MohoGlobals.SelCol)
      for i,k in ipairs(points) do
         g:FillCircle(k, .1)
      end
   g:Pop()
   moho.view:RefreshView()
   moho.view:DrawMe()	
end

Right now, I'm just trying to get it to do something, anything. I've been looking for documentation on how the viewport is organized, but all I see are functions in the api will little to no information on how it's organized or supposed to be called. For the most part, I've just been trying to copy and paste whatever I can find from other scripts, but it doesn't appear to work. I'm clearly doing something wrong, I just don't know what.

Re: Graphics class help

Posted: Wed Apr 18, 2018 5:27 pm
by synthsin75
I'm not sure you can do that in a menu/button script, since these do not remain active, but only run once and end (so they don't remain in control of the view). My above code works in a tool script.

Re: Graphics class help

Posted: Wed Apr 18, 2018 5:43 pm
by dkwroot
What function are you running it in, specifically? Is there a tool script function that will run without user input? I'm using the run function in a tool script, but it doesn't draw.

Re: Graphics class help

Posted: Wed Apr 18, 2018 6:33 pm
by synthsin75
A tool's DrawMe function will run it without input.

Re: Graphics class help

Posted: Wed Apr 18, 2018 6:52 pm
by dkwroot
I tried putting it in DrawMe(moho,view), but it still doesn't work.

Code: Select all

function DR_TEST:DrawMe(moho,view)	
	local points = {}
	local mesh = moho:Mesh()
	for i=0, mesh:CountPoints()-1 do
		table.insert(points, mesh:Point(i).fPos)
	end
	local g = view:Graphics()
	local matrix = LM.Matrix:new_local()
	moho.drawingLayer:GetFullTransform(moho.frame, matrix, moho.document)
	g:Push()
		g:ApplyMatrix(matrix)
		g:SetColor(MOHO.MohoGlobals.SelCol)
		for i,k in ipairs(points) do
			g:FillCircle(k, .2)
		end
	g:Pop()
	view:RefreshView()
	view:DrawMe()	
end

Re: Graphics class help

Posted: Wed Apr 18, 2018 7:23 pm
by synthsin75
Does the tool have an OnMouseDown function (even an empty one)? That's how Moho seems to know it's a tool.

Re: Graphics class help

Posted: Wed Apr 18, 2018 8:14 pm
by dkwroot
I think that was it, I'm still having trouble but it's at least drawing something now. I'll have to play with the settings and figure things out. Thanks, Wes!

Re: Graphics class help

Posted: Wed Apr 18, 2018 11:27 pm
by synthsin75
Glad I could help.