Hey all!
i want to write my first lua/moho script :
the idea is to assign automatically named styles to every equal colors in a project. (in my case it will help to fix .ai import color problem but would be cool anyway!)
I can't find methods to
-create named style and deal with it
-assign it to a shape
Are "named" styles in the API ?
thank you!!
apply named styles by script
Moderators: Víctor Paredes, Belgarath, slowtiger
- Lost Marble
- Site Admin
- Posts: 2355
- Joined: Tue Aug 03, 2004 6:02 pm
- Location: Scotts Valley, California, USA
- Contact:
Even though it wouldn't seem to actually apply a named style, I think I've got a usable workaround. Check out the "color picker" section of the lm_select_shape script. You can apply a style from one shape to another. And you can test what a shapes color is. The rub here is that it only seems to give you back the line and fill colors that were the defaults when the shape was created (or modified), and only for the defaults disregarding any applied styles.
But perhaps not really such a problem if you are trying to color correct imported files that don't have applied styles yet. So, this is what I worked out so far I'm leaving out all the boilerplate at the start of the script.:
Just the usual there.
Oh, well, the comments are kinda covering it. Onward
Now, I know that the test for color matching is awful clunky, but I guess you just can't directly compare one rgb_color object with another. It would be more elegant using ColorVector objects that you could directly compare without fiddling with the individual RGB values, but they use a number from zero to 1 instead of 0 to 255 and I just didn't wan't to think enough to code the back and forth conversions. It also makes it pretty clear whats going on.
You have to select a shape with the Select Shape tool first.
It even seems to work so far. At least if the objects you are scanning don't already have an applied style. If they do, but their underlying default values match, Whoops, they get changed anyway. Which is why I wouldn't use it where you have any other applied styles. Just tweak the imported file, save it, and bring the finished Moho object back into your project. You do have to create styles and then apply them to a garbage object that you then select.
So far this only works on the current layer, but it shouldn't be too hard to make it parse through the whole project if you want. Hope that's a little help. The whole script ***here*** to play with. All the print commands are just there to let you (well really me while I was debugging) see what is going on, so feel free to kill 'em or comment them out if you don't wan't the Lua console window popping up all the time.
--Brian
But perhaps not really such a problem if you are trying to color correct imported files that don't have applied styles yet. So, this is what I worked out so far I'm leaving out all the boilerplate at the start of the script.:
Code: Select all
-- **************************************************
-- Recurring values
-- **************************************************
-- style from the selected shape we're going to copy
SF_ShapeTest.stylie = nil
-- the variables for the color we are searching for
-- to determine if a shapes style will be changed
SF_ShapeTest.searchColorLine = LM.rgb_color:new_local()
SF_ShapeTest.searchColorFill = LM.rgb_color:new_local()
-- I hardcoded the values for now. You'd need to set up
-- some UI doodads to enter values
SF_ShapeTest.searchColorLine.r = 0
SF_ShapeTest.searchColorLine.g = 0
SF_ShapeTest.searchColorLine.b = 0
SF_ShapeTest.searchColorLine.a = 255
SF_ShapeTest.searchColorFill.r = 255
SF_ShapeTest.searchColorFill.g = 255
SF_ShapeTest.searchColorFill.b = 255
SF_ShapeTest.searchColorFill.a = 255
-- the shape we're going to copy the style from, also used
-- as a flag that one was actually selected
SF_ShapeTest.shapeToCopy = nil
Code: Select all
-- **************************************************
-- The guts of this script
-- **************************************************
function SF_ShapeTest:Run(moho)
local mesh = moho:Mesh()
if (mesh == nil) then
return
end
moho.document:PrepUndo(moho.layer)
moho.document:SetDirty()
-- Find the selected shape (if any) and get it's style
for i = 0, mesh:CountShapes() - 1 do
if (mesh:Shape(i).fSelected == true) then
self.shapeToCopy = mesh:Shape(i)
self.stylie = self.shapeToCopy.fMyStyle
print("Found selected shape #",i)
end
end
Code: Select all
for i = 0, mesh:CountShapes() - 1 do
-- go through all the other shapes, and make sure you had one selected
-- in the first place
if (mesh:Shape(i).fSelected == false) and (self.shapeToCopy) then
print("Testing shape #", i)
-- get the current shapes line and fill color values
local testShape = mesh:Shape(i)
local testStylie = testShape.fMyStyle
local testColorLine = testStylie.fLineCol.value
--or, if you want to cut down lines, you can do it in one shot:
local testColorFill = mesh:Shape(i).fMyStyle.fFillCol.value
-- test them out to see if they are ones you want to change
if (testColorFill.r == self.searchColorFill.r) and
(testColorFill.g == self.searchColorFill.g) and
(testColorFill.b == self.searchColorFill.b) and
(testColorLine.r == self.searchColorLine.r) and
(testColorLine.g == self.searchColorLine.g) and
(testColorLine.b == self.searchColorLine.b) then
-- if it matches, copy over the style from the selected shape
print ("Modifying Shape #", i,"to style from shape #",self.gotShape)
testShape:CopyStyleProperties(self.shapeToCopy)
else
-- otherwise go on your merry way
print ("Excluding shape#",i,"from change")
end
end
end
end
You have to select a shape with the Select Shape tool first.
It even seems to work so far. At least if the objects you are scanning don't already have an applied style. If they do, but their underlying default values match, Whoops, they get changed anyway. Which is why I wouldn't use it where you have any other applied styles. Just tweak the imported file, save it, and bring the finished Moho object back into your project. You do have to create styles and then apply them to a garbage object that you then select.
So far this only works on the current layer, but it shouldn't be too hard to make it parse through the whole project if you want. Hope that's a little help. The whole script ***here*** to play with. All the print commands are just there to let you (well really me while I was debugging) see what is going on, so feel free to kill 'em or comment them out if you don't wan't the Lua console window popping up all the time.
--Brian