Page 1 of 1

Any script to change 3d color?

Posted: Sun Dec 19, 2021 4:04 pm
by bbrraayyaann
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

Re: Any script to change 3d color?

Posted: Sun Dec 19, 2021 10:28 pm
by hayasidist
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.

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
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.

Re: Any script to change 3d color?

Posted: Tue Dec 21, 2021 2:31 pm
by bbrraayyaann
hayasidist wrote: Sun Dec 19, 2021 10:28 pm a 3d object is made up of faces.
each face has an associated material.
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.




Thank you very much it helped me !!!!

Re: Any script to change 3d color?

Posted: Tue Dec 21, 2021 6:05 pm
by hayasidist
ok ... this will get the 3d mesh

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
and this snippet will create a new material (obviously you can set any name / colours as necessary)

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
if you get stuck, just shout.

Re: Any script to change 3d color?

Posted: Wed Mar 23, 2022 4:34 pm
by bbrraayyaann
hayasidist wrote: Tue Dec 21, 2021 6:05 pm ok ... this will get the 3d mesh

if you get stuck, just shout.
One question, in this code:

Code: Select all

	if layer:LayerType() == MOHO.LT_3D then 
		local mesh = moho:Mesh3D()
	else
		return false
	end
How to refer to the layer?
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


.

Re: Any script to change 3d color?

Posted: Wed Mar 23, 2022 8:05 pm
by hayasidist
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

Code: Select all

childLayer = moho:LayerAs3D(childLayer)
moho:SetSelLayer(childLayer) -- https://mohoscripting.com/methods/60
mesh = moho:Mesh3D()
faces=mesh:CountFaces()
this will mean your original layer selection will be lost unless you save it before servicing the 3d layers and restore it afterwards.

Re: Any script to change 3d color?

Posted: Wed Mar 23, 2022 9:16 pm
by bbrraayyaann
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

Code: Select all

childLayer = moho:LayerAs3D(childLayer)
moho:SetSelLayer(childLayer) -- https://mohoscripting.com/methods/60
mesh = moho:Mesh3D()
faces=mesh:CountFaces()
this will mean your original layer selection will be lost unless you save it before servicing the 3d layers and restore it afterwards.
A I see, then it would not be possible, I would have to select the 3d object.
Thank you very much, but I found this code useful.

I will look for another way.

Re: Any script to change 3d color?

Posted: Wed Mar 23, 2022 9:27 pm
by hayasidist
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.
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)