Problem with GroupLayer and CountLayers()

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
lehtiniemi
Posts: 107
Joined: Mon Jan 14, 2013 3:18 pm

Problem with GroupLayer and CountLayers()

Post by lehtiniemi »

I'm trying to go through all layers in the document, but in the code below, after detecting a IsGroupType() and trying to read the layer count, I get "attempt to call method 'CountLayers' (a nil value). What am I doing wrong here?

I have three layers in my document: A group layer, a bone layer and a normal layer. At index 1 my script prints: i=1, "Bone layer at 1" but then the error when I try to read the layer count. All IsGroupType()-layers should have CountLayers() because their parent class is GroupLayer, right?

Code: Select all

	for i = 0, moho.document:CountLayers()-1 do
		local currentLayer = moho.document:Layer(i)

		if (currentLayer:LayerType() == MOHO.LT_GROUP) then print ("Group layer at ", i) end
		if (currentLayer:LayerType() == MOHO.LT_BONE) then print ("Bone layer at ", i) end
		if (currentLayer:LayerType() == MOHO.LT_PARTICLE) then print ("Particle layer at ", i) end
		if (currentLayer:LayerType() == MOHO.LT_SWITCH) then print ("Switch layer at ", i) end

		if (currentLayer:IsGroupType()) then
			print ("Group layer found at index ", i, ", layer count is ", currentLayer:CountLayers())
		end
	end
User avatar
synthsin75
Posts: 9964
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Problem with GroupLayer and CountLayers()

Post by synthsin75 »

You need to cast the layer as a group type first.

local group = moho:LayerAsGroup(layer)

group:Countlayers()
lehtiniemi
Posts: 107
Joined: Mon Jan 14, 2013 3:18 pm

Re: Problem with GroupLayer and CountLayers()

Post by lehtiniemi »

synthsin75 wrote:You need to cast the layer as a group type first.

local group = moho:LayerAsGroup(layer)

group:Countlayers()

Thank you!!!!
Post Reply