Any script to change 3d color?
Moderators: Víctor Paredes, Belgarath, slowtiger
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Any script to change 3d color?
Hello, I have for example a 3d object in obj .can I change the color from the MOHO program?
Someone please help me with a code ....
https://www.mediafire.com/file/77ju3spl ... .moho/file
Someone please help me with a code ....
https://www.mediafire.com/file/77ju3spl ... .moho/file
- hayasidist
- Posts: 3857
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: Any script to change 3d color?
a 3d object is made up of faces.
each face has an associated material.
the default material in the style window is material number 0 (note that the style window needs to be headed by Default 3D Material)
In the example you uploaded, all 6 faces have the same material "Mat" (material number 1).
Two approaches to change the whole cube:
1: change the material associated with each face to be default - then the cube instantly picks up whatever is in the style window
2: change the attributes of each face's material to be the same as the default (i.e. copy the fields of material 0 into those of the material for each face)
doing it on a face-by-face basis will be tricky, because there's no native UI to pick 3d object faces...
here's the core of the code needed for those two options.
hope that helps
===
However, if all you want to do is modify the colours of the cube within moho - get "Mat" as the active material in the style window and change the fill and stroke.
each face has an associated material.
the default material in the style window is material number 0 (note that the style window needs to be headed by Default 3D Material)
In the example you uploaded, all 6 faces have the same material "Mat" (material number 1).
Two approaches to change the whole cube:
1: change the material associated with each face to be default - then the cube instantly picks up whatever is in the style window
2: change the attributes of each face's material to be the same as the default (i.e. copy the fields of material 0 into those of the material for each face)
doing it on a face-by-face basis will be tricky, because there's no native UI to pick 3d object faces...
here's the core of the code needed for those two options.
Code: Select all
-- option 1
for i = 0, mesh:CountFaces()-1 do
local face = mesh:Face(i)
face.matID = 0
end
-- option 2
local function resetMat(mat, def)
mat.color.r, mat.color.g, mat.color.b = def.color.r, def.color.g, def.color.b
mat.edgeColor.r, mat.edgeColor.g, mat.edgeColor.b = def.edgeColor.r, def.edgeColor.g, def.edgeColor.b
mat.drawEdges, mat.edgeWidth = def.drawEdges, def.edgeWidth
end
for i = 0, mesh:CountFaces()-1 do
local face = mesh:Face(i)
resetMat(mesh:Material(face.matID), mesh:Material(0))
end
===
However, if all you want to do is modify the colours of the cube within moho - get "Mat" as the active material in the style window and change the fill and stroke.
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Re: Any script to change 3d color?
Thanks for your answer. I will see if I can apply it , What I want is a tool to select the color with the eyedropper , and paint the cube or other 3d objects inside the same MOHO.hayasidist wrote: ↑Sun Dec 19, 2021 10:28 pm a 3d object is made up of faces.
each face has an associated material.
Thank you very much it helped me !!!!
- hayasidist
- Posts: 3857
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: Any script to change 3d color?
ok ... this will get the 3d mesh
and this snippet will create a new material (obviously you can set any name / colours as necessary)
if you get stuck, just shout.
Code: Select all
if layer:LayerType() == MOHO.LT_3D then
local mesh = moho:Mesh3D()
else
return false
end
if not mesh then
return false
end
Code: Select all
local mName = "mat" .. tostring(mesh:CountMaterials())
local mat = mesh:CreateNewMaterial(mName)
mat.color = LM.ColorOps:RgbColor(255, 255, 128, 255)
mat.edgeColor = LM.ColorOps:RgbColor(255, 255, 0, 255)
mat.drawEdges = true
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Re: Any script to change 3d color?
One question, in this code:hayasidist wrote: ↑Tue Dec 21, 2021 6:05 pm ok ... this will get the 3d mesh
if you get stuck, just shout.
Code: Select all
if layer:LayerType() == MOHO.LT_3D then
local mesh = moho:Mesh3D()
else
return false
end
I try to do this but I keep getting an error
Code: Select all
local mesh =moho:LayerAs3D(childLayer):Mesh3D()
local faces=mesh:CountFaces()
Here is the LUA file :https://www.mediafire.com/file/fdpfingh ... 2.lua/file
.
- hayasidist
- Posts: 3857
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: Any script to change 3d color?
the first is where you're working on the active layer (from layer = moho.layer)
the second is a logical assumption, but Mesh3DLayer does not have the equivalent of a 2d vector layer :Mesh() method (https://mohoscripting.com/classes/Mesh3DLayer and https://mohoscripting.com/classes/MeshLayer}
so you need to make the 3d layer the active one
this will mean your original layer selection will be lost unless you save it before servicing the 3d layers and restore it afterwards.
the second is a logical assumption, but Mesh3DLayer does not have the equivalent of a 2d vector layer :Mesh() method (https://mohoscripting.com/classes/Mesh3DLayer and https://mohoscripting.com/classes/MeshLayer}
so you need to make the 3d layer the active one
Code: Select all
childLayer = moho:LayerAs3D(childLayer)
moho:SetSelLayer(childLayer) -- https://mohoscripting.com/methods/60
mesh = moho:Mesh3D()
faces=mesh:CountFaces()
-
- Posts: 69
- Joined: Thu Jan 24, 2019 3:52 am
Re: Any script to change 3d color?
A I see, then it would not be possible, I would have to select the 3d object.hayasidist wrote: ↑Wed Mar 23, 2022 8:05 pm the first is where you're working on the active layer (from layer = moho.layer)
the second is a logical assumption, but Mesh3DLayer does not have the equivalent of a 2d vector layer :Mesh() method (https://mohoscripting.com/classes/Mesh3DLayer and https://mohoscripting.com/classes/MeshLayer}
so you need to make the 3d layer the active one
this will mean your original layer selection will be lost unless you save it before servicing the 3d layers and restore it afterwards.Code: Select all
childLayer = moho:LayerAs3D(childLayer) moho:SetSelLayer(childLayer) -- https://mohoscripting.com/methods/60 mesh = moho:Mesh3D() faces=mesh:CountFaces()
Thank you very much, but I found this code useful.
I will look for another way.
- hayasidist
- Posts: 3857
- Joined: Wed Feb 16, 2011 8:12 pm
- Location: Kent, England
Re: Any script to change 3d color?
at the risk of repetition: there is no UI in Moho for 3d objects, either to select them or to pick faces or mesh points. If you want to work on the object, you need the _layer_ selected (which you can do from a script or from the layers UI)bbrraayyaann wrote: ↑Wed Mar 23, 2022 9:16 pm A I see, then it would not be possible, I would have to select the 3d object.