...Problems to get the Shape ID's.

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

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
User avatar
Rai López
Posts: 2259
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

...Problems to get the Shape ID's.

Post by Rai López »

Well, during a loooooong night I've been having troubles trying to get the ID and Name of the shapes in a Vector Layer by embedded script... It seems that it's not so easy or direct like in the bones case and I need it to can get and manage the stablisher order of that shapes inside the layer; and well, I write here just in case someone have any suggestion or clue about that; really it'd be SO welcomed... I've been trying the typical lines of code:

Code: Select all

      for count = 0, (mesh:CountShapes() - 1) do 
         local currentShape = mesh:Shape(count) 
         local name = currentShape:Name()
         if (string.sub(name, -3) == ".id") then 
            currentShapeID = Mesh:ShapeID(currentShape) 
            theShapeID = currentShape
         end 
      end
Or somenthing so... I've been trying so much variations that I'm not sure about nothing yet, well... thanks in advance for any help and, GOOD NIGHT/DAY!
User avatar
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Post by Rasheed »

I'm not sure what you meant by "shape names". AFAIK a shape doesn't have a name, only a reference (which is sometime called the shape ID). Although there are no shape names, one can define point groups, which have names. Is this perhaps what you meant?

One refers to shapes (and all other moho objects) via a structure called userdata. Userdata is in essence an interface with the C++ programming code of Anime Studio.

In vector layers there is a mesh object available. The vector layer automatically inherits the member function Mesh(). You refer to that member function with moho:Mesh(). This means "run the Mesh function of this layer". moho here simply means "this layer". The Mesh() member function returns the reference to ("address" of) the mesh object in the vector layer. You capture this mesh object in Lua as follows:

Code: Select all

local mesh = moho:Mesh()
The local variable mesh now contains the mesh object of the scripted layer (the layer containing the layer script). If there is no mesh available (e.g. in a group layer), moho:Mesh() returns a nil value (meaning: "does not exist").

The mesh object itself has also function members, such as:
  • CountShapes() -- returns the number of shapes in the mesh (an integer value)
  • Shape(n) -- returns the reference ("address") to the n-th shape object in the mesh (beginning counting from zero)
  • CountGroups() -- returns the number of points groups in the mesh (an integer value)
  • Group(n) -- returns the reference ("address") to the n-th point group object in the mesh
Each point group object has member functions as well, such as:
  • Name() -- returns the name of the point group (a string value)
Here is some sample code to explain.

Code: Select all

function LayerScript(moho)

    local mesh = moho:Mesh()    -- the current scripted layer's mesh
    
    -- if there is no mesh available, exit the script layer
    if ( mesh == nil ) then
        return false
    end
    
    local shapes = {}           -- array to hold the shapes
    local groups = {}           -- array to hold the point groups
                                -- and their names
    
    local shapeCount = mesh:CountShapes()   -- count the shapes
    if (shapeCount > 0 ) then
        for i=0, shapeCount - 1 do
            shapes[i] = mesh:Shape(i)       -- read shape into array
        end
    end
    
    local groupCount = mesh:CountGroups()   -- count the point groups
    if ( groupCount > 0 ) then
        for i=0, groupCount - 1 do
            groups[i] = mesh:Group(i)           -- read point groups into array
            groups[i].name = groups[i]:Name()   -- read PG name into array
            print (i .. ': ' .. groups[i].name ) -- print it
        end
    end
    
end
I hope the earlier explanation is clear enough to demystify the Lua code. I have any questions, be sure sure to ask them, and I will see if I can answer them.

And if you meant something completely different than I described here, feel free to point that out as well.
User avatar
Rai López
Posts: 2259
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

...I'M HOME! :D (at last...) I find your reply and... Uf! it couldn't be more welcomed! THANK YOU!!! Now... to study :), and then I'll said you if I can get that I want joining all that with that I still have, for now surely it'll help! I was so standstill on that... I think the ID issues always bring me some headache at last...

AH! And about the Shape Names (fortunately) we always have could put'em a name, you only must select it with the Select Shape Tool and name it in the Style Window, a little different that in the other cases but possible, and it can be a great help in many cases, cause of this it seems weird to me that I can't get that name by scripting easily using that Name() or .fName fuctions of the manual, well, at least until now! Let's see ...tHAaNkS AGAIN!!! :D
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I always have my prefs set to automatically name bones and shapes. Such a joy to be able to find the bottom shape under 50 other shapes with one click.

-vern
User avatar
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Post by Rasheed »

:oops: Of course, shapes can have names (what was I thinking!).

The Moho Scripting Reference manual states this:
fName (LM_String)

Name of the shape.
However, Lua cannot access the LM_String data structure, see:
http://www.lostmarble.com/forum/viewtop ... =6425#6425

So, you cannot read or write shape names, unfortunately, Ramón.

At least I got that right :)
User avatar
heyvern
Posts: 7035
Joined: Fri Sep 02, 2005 4:49 am

Post by heyvern »

I get it now Rasheed...

That kind of sucks doesn't it? I am so use to editing my "raw" lua files with grep I don't know what is available in LUA and what isn't.

-vern
User avatar
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Post by Rasheed »

It seems that beside a book "Creating Anime with Anime Studio" there is a need for "Scripting for Anime Studio" ;)

The Moho Scripting manual is obviously too concise and also unclear in places (whoever knew that LM_String wasn't available in the Lua-Moho interface)? If I only had the programming skills to write a scripting book myself...
User avatar
Rai López
Posts: 2259
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

...HI!!! :D
Rasheed wrote:It seems that beside a book "Creating Anime with Anime Studio" there is a need for "Scripting for Anime Studio" ;)
OH! But I'm waiting almost two years only for that three little "Examples***" in the Scripting documentadion get out, so... well, I think that wait for a entire book seems, sadly, like an impossible dream...

...BTW! I'm very excited again with all this scripting issues and you don't know how I love you are here again with all this experiments, I think your other "Layer Structure" script can be of great help in many casess and with your code here for ID Shapes I'm now getting some results! Well, now I only have a little doubt too... I wonder if when I obtain the ID shapes whith it I'm obtaining really the "real" ID of each shape or only a numbered list of them... that could be not exactly the same, uh? Or am I (surely) wrong? Hmmm... Surely :roll:

Well, now at work again... :cry:
User avatar
Rasheed
Posts: 2008
Joined: Tue May 17, 2005 8:30 am
Location: The Netherlands

Post by Rasheed »

Well Ramón, the solution would be to create a tool that allows you to name shapes and styles, independently of what AS/Moho does. Of course, it would only work in RAM, unless you store the variables in an AS document. You could use a Note layer, make it invisible in editing view, and translate the Note as far away from vision as possible. You could even try to modify an AS document directly (although I'm not in favor of that, because file access is often not very cross-platform).

Until LM includes Name() and SetName() member functions for shapes and fills (or include other ways to read and write LM_String structures) into the Lua scripting interface, this would be the best workaround I can think of right now.
Post Reply