Asking for Improved Script to Rotate-Translate 3D objects

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:

Asking for Improved Script to Rotate-Translate 3D objects

Post by Rai López »

I know that there is three scripts for this, but somebody could write an unique Script that let you do this with a numeric text field to choose the angle for rotate 3D objects in any "X,Y,Z" axis, and (I don't know if this is possible) a "X & Y" numeric text field too to translate (maybe scale too) this 3D forms for can put it where you want without the necesity of translate/rotate/scale the 3D layer, cause in some ocasions that is not convenient (especially when you are working with bones i.e.) ...well, THANKS for readme and BYE!!
User avatar
Rai López
Posts: 2259
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

I DID IT!! :D And it Works, I can't believe it!! 8)
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Hey,Ramón, so where is it? Share the wealth, dude!

Cool that another person has gotten the scripting thing going on. It would be great to take a look at. Thanks.

--Brian
User avatar
Agent
Posts: 140
Joined: Wed Aug 18, 2004 5:12 pm
Location: Budapest, Hungary

Post by Agent »

I'd be happy if theres any script like this
A 3d rotating camera would be nice
User avatar
Rai López
Posts: 2259
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

SORRY!! I thought that nobody was interested... Finaly, it resulted very simple and it served to me to learn some things about "simple scripting", I'd like (very much) that the window could REMAIN OPENED while make the changes, but I don't know how do it (of course) and I don't know if this could be possible...

...Ah! I did another script to scale 3D objects directly in Moho, I used this comand for it: "mesh:ScaleToRadius" and it works yes, the objects are scaled, but there is a curious problem and always there is a vertice that is not scaled :-( ...I don't know if this is a problem of "my" script or if it's a comand/Moho bug, If I get it works I'll try to put all in only one script, it'll be nice too can translate the 3D objects, all in only one script, but I don't know if there is a comand for this, well... if somebody is interested I'll put here the scale script too, although I imagine that LM don't like that I put this so (instead a link) in his forum... sorry, but I haven't another way... ÁÁÁDIOS!

NOTE for all other interested people: Only copy the follow text into a *.txt file and rename it to: lm_rotate3D_XYZ.lua
-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "LM_Rotate3D_XYZ"

-- **************************************************
-- General information about this script
-- **************************************************

LM_Rotate3D_XYZ = {}

function LM_Rotate3D_XYZ:Name()
return "Rotate 3D XYZ"
end

function LM_Rotate3D_XYZ:Version()
return "5.0"
end

function LM_Rotate3D_XYZ:Description()
return "Rotates a 3D model around the X, Y or Z axis."
end

function LM_Rotate3D_XYZ:Creator()
return "Lost Marble & R.e.M"
end

function LM_Rotate3D_XYZ:UILabel()
return("Rotate XYZ...")
end

-- **************************************************
-- Recurring values
-- **************************************************

LM_Rotate3D_XYZ.xangle = 0
LM_Rotate3D_XYZ.yangle = 0
LM_Rotate3D_XYZ.zangle = 0

-- **************************************************
-- Rotate 3D XYZ dialog
-- **************************************************

local LM_Rotate3D_XYZDialog = {}

function LM_Rotate3D_XYZDialog:new(moho)
local d = LM.GUI.SimpleDialog("XYZ Rotation", LM_Rotate3D_XYZDialog)
local l = d:GetLayout()

d.moho = moho

l:PushH()
l:PushV()
l:AddChild(LM.GUI.StaticText("X Angle"), LM.GUI.ALIGN_LEFT)
l:AddChild(LM.GUI.StaticText("Y Angle"), LM.GUI.ALIGN_LEFT)
l:AddChild(LM.GUI.StaticText("Z Angle"), LM.GUI.ALIGN_LEFT)
l:Pop()
l:PushV()
d.xangle = LM.GUI.TextControl(0, "0.0000", 0, LM.GUI.FIELD_FLOAT)
l:AddChild(d.xangle)
d.yangle = LM.GUI.TextControl(0, "0.0000", 0, LM.GUI.FIELD_FLOAT)
l:AddChild(d.yangle)
d.zangle = LM.GUI.TextControl(0, "0.0000", 0, LM.GUI.FIELD_FLOAT)
l:AddChild(d.zangle)
l:Pop()
l:Pop()

return d
end

function LM_Rotate3D_XYZDialog:UpdateWidgets()
self.xangle:SetValue(LM_Rotate3D_XYZ.xangle)
self.yangle:SetValue(LM_Rotate3D_XYZ.yangle)
self.zangle:SetValue(LM_Rotate3D_XYZ.zangle)
end

function LM_Rotate3D_XYZDialog:OnValidate()
local b = true
if (not self:Validate(self.xangle, -360, 360)) then
b = false
end
if (not self:Validate(self.yangle, -360, 360)) then
b = false
end
if (not self:Validate(self.zangle, -360, 360)) then
b = false
end
return b
end

function LM_Rotate3D_XYZDialog:OnOK()
LM_Rotate3D_XYZ.xangle = self.xangle:FloatValue()
LM_Rotate3D_XYZ.yangle = self.yangle:FloatValue()
LM_Rotate3D_XYZ.zangle = self.zangle:FloatValue()
end

-- **************************************************
-- The guts of this script
-- **************************************************

function LM_Rotate3D_XYZ:IsEnabled(moho)
if (moho.layer:LayerType() ~= MOHO.LT_3D) then
return false
end
if (moho.layer:CurrentAction() ~= "") then
return false -- creating new objects in the middle of an action can lead to unexpected results
end
return true
end

function LM_Rotate3D_XYZ:Run(moho)
local mesh = moho:Mesh3D()
if (mesh == nil) then
return
end

local dlog = LM_Rotate3D_XYZDialog:new(moho)
if (dlog:DoModal() == LM.GUI.MSG_CANCEL) then
return
end

moho.document:PrepUndo(moho.layer)
moho.document:SetDirty()

local vec = LM.Vector3:new_local()

for i = 0, mesh:CountPoints() - 1 do
local v = mesh:Point(i)
v:Rotate(LM.X_AXIS, math.rad(self.xangle))
v:Rotate(LM.Y_AXIS, math.rad(self.yangle))
v:Rotate(LM.Z_AXIS, math.rad(self.zangle))
mesh:SetPoint(i, v)
end

-- Because we modified the points directly, this mesh shouldn't be re-loaded from an external file.
-- This can be prevented by telling the layer that there is no external file.
local layer = moho:LayerAs3D(moho.layer)
layer:SetSourceMesh("")
end
User avatar
Lost Marble
Site Admin
Posts: 2354
Joined: Tue Aug 03, 2004 6:02 pm
Location: Scotts Valley, California, USA
Contact:

Post by Lost Marble »

Ramón López wrote:I'd like (very much) that the window could REMAIN OPENED while make the changes, but I don't know how do it (of course) and I don't know if this could be possible...
Do you mean that you don't want to have to close and re-open Moho while you edit your script? This is possible right now - if you're creating a script, it can involve making lots of little changes. In Moho, if you hold down the Ctrl key and press F5, it will re-load the scripts so that Moho will recognize any changes you made without having to quit and re-start the program.
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Cool. I, for one, would like to see the other script. And as LM said, the <CTRL><F5> combo to refresh the scripts is a lifesaver when you are debugging. On some scripts I would certainly have given up if it weren't for that. Also, if you have to post a script inside your message, you should use the "Code" button instead of the "Quote" button. Then it will keep the formatting, indentations, etc. in the script.

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

Post by Rai López »

...NO!!! (HELLO :lol: ) I mean that I'd like that the script window (where you make the changes (angles in this case)) remain open, so you can make a change, see it in main window, and re-change if you want, I think that this is not possible, but I only want to be sure... :idea: Maybe if the script will be a script Tool instead a Menu Script... BYE!
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Look at the Bone Constraints window in the Select Bone tool. It stays open until you close it. I don't remember the exact command you have to change to make it work like that. I've used it in other things, so if you don't figure it out I'll look it up when I get back from work. Not sure if it would work as a Menu script, but it is worth a try.

--Brian
User avatar
Lost Marble
Site Admin
Posts: 2354
Joined: Tue Aug 03, 2004 6:02 pm
Location: Scotts Valley, California, USA
Contact:

Post by Lost Marble »

Ramón,

I just took a look at your script, and I noticed that it's called "LM_Rotate3D_XYZ". I would ask you to please name it something else.

The "LM_" part at the beginning of all the included scripts stands for Lost Marble. If we were to someday include a script called LM_Rotate3D_XYZ, then it would conflict with yours, and probably neither one would work.

Instead, could you name it "RL_Rotate3D_XYZ"? The RL standing for Ramón López. Or, you could choose some other prefix, but please don't use LM - that way we don't have to worry about future scripts that might be included with Moho.
User avatar
Rai López
Posts: 2259
Joined: Sun Aug 08, 2004 1:41 pm
Location: Spain
Contact:

Post by Rai López »

THANKS 7feet!!! :D (for the clue) & SORRY LM!!! (for that...) I get the scripting documentation/tutorials since 3 or 4 days ago only, I could read how must I do for the next time, but I don't knew when I modified your script... Never more :arrow: CIAO!
Post Reply