Graphics class help

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:

Graphics class help

Post 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.
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Graphics class help

Post 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()
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Graphics class help

Post 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.
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Graphics class help

Post by synthsin75 »

The function it's in needs to be supplied "view" or "moho.view" as an argument.
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Graphics class help

Post 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.
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Graphics class help

Post 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.
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Graphics class help

Post 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.
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Graphics class help

Post by synthsin75 »

A tool's DrawMe function will run it without input.
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Graphics class help

Post 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
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Graphics class help

Post by synthsin75 »

Does the tool have an OnMouseDown function (even an empty one)? That's how Moho seems to know it's a tool.
dkwroot
Posts: 677
Joined: Thu May 02, 2013 6:56 am
Location: USA
Contact:

Re: Graphics class help

Post 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!
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Graphics class help

Post by synthsin75 »

Glad I could help.
Post Reply