How to turn on material option: self-illumination in Python script

@Alain, @pascal, @John_Brock,

I trimmed a textured mesh with an arbitrary curve using a Python script and the texturing in the trimmed mesh perfectly matches the appearance of the original mesh right up to the boundary if I go to the Materials tab and turn on self-illumination. How do I do turn on self-illumination in the Python script?

Trimmed, textured mesh with self-illumination manually turned-on:

Original, textured mesh:

I obtained the texture material from the original mesh using:

mobj = doc.Objects.Find(mesh)
# Get texture material for original mesh.
source_mat = mobj.Attributes.MaterialIndex

and I applied this to the trimmed mesh: tmesh using:

obj = doc.Objects.Find(tmesh)
# Apply source texture material to new mesh.
obj.Attributes.MaterialIndex = source_mat
obj.Attributes.MaterialSource = ObjectMaterialSource.MaterialFromObject
obj.CommitChanges()

but this does nothing to the setting for self-illumination.

On your Rhino.DocObjects.Material set DisableLighting property. This controls self-illumination.

This means self-illumination is a property of the material, not the object.

I tried:

Rhino.DocObjects.Material.DisableLighting = False

but this fails with the message:

Message: static property 'DisableLighting' of 'Material' can only be assigned to through a type, not an instance

Exactly how should I code this in my Python script?

Regards,
Terry.

Hi @Terry_Chappell, below seems to change something in V6:

import Rhino
import scriptcontext
import rhinoscriptsyntax as rs

def DoSomething():
    
    obj_id = rs.GetObject("Select object", 8+16+32, True, True)
    if not obj_id: return
    
    obj = rs.coercerhinoobject(obj_id, True, True)
    
    mat_index = obj.Attributes.MaterialIndex
    if not mat_index >= 0: return
    
    # toggle diffuse lightning
    material = scriptcontext.doc.Materials[mat_index]
    material.DisableLighting = not material.DisableLighting
    material.CommitChanges()
    
DoSomething()

But if does not toggle the self-illumination checkbox in the UI. @nathanletwory, how to change that ?

_
c.

1 Like

I leveraged your last three material lines in my Python script and now it is working fine.

from scriptcontext import doc

# Make sure self-illumination is turned on or else the trimmed mesh will be dark.
material = doc.Materials[source_mat]
material.DisableLighting = True
material.CommitChanges()

Thanks for your perfect help.

Regards,
Terry.

I assume a redraw is needed.

Tried that but the checkbox does not switch. I’ll asume some tricky RDK code is required.

_
c.

Not sure, Rhino.DocObjects.Material is ON_Material, not a proper RDK material ^.^

But maybe @andy or @johnc know better.

for material in sc.doc.RenderMaterials:
material.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
material.SetParameter(“disable-lighting”, True)
material.EndChange()

Combining your code with @andy s code you’d have something like this:

# Make sure self-illumination is turned on or else the trimmed mesh will be dark.
mat = doc.Materials[source_mat]
material = mat.RenderMaterial
material.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program)
material.SetParameter(“disable-lighting”, True)
material.EndChange()

Have you tested this? It does not work for my case. The check box in front of Self-illumination under Advanced Options of the Materials tab does not turn on. So I am just using:

	obj.Attributes.MaterialIndex = source_mat
	obj.Attributes.MaterialSource = ObjectMaterialSource.MaterialFromObject
	obj.CommitChanges()
	# Make sure self-illumination is turned on or else trimmed mesh will be dark.
	mat = doc.Materials[obj.Attributes.MaterialIndex] # Does not work if source_mat is used here.
	mat.DisableLighting = True
	mat.CommitChanges()

and left out the lines you suggested:

	material = mat.RenderMaterial
	material.BeginChange(Render.RenderContent.ChangeContexts.Program)
	material.SetParameter('disable-lighting', True)
	material.EndChange()

Yes I have tested this. The following script I used:

import scriptcontext as sc
import Rhino.Render as Render

for m in sc.doc.Materials:
	rm = m.RenderMaterial
	rm.BeginChange(Render.RenderContent.ChangeContexts.Program)
	rm.SetParameter('disable-lighting', True)
	rm.EndChange()

And it yields a result like this:

I ran a copy of your exact code on the attached mesh and I do not see Self-illumination toggling like in your video. Strange.

I MeshForTest.3dm (6.9 MB)

Your material is a very old-style material. You can see that in the material editor: you can’t change its type. I don’t know (yet?) how to get it converted programmatically, but you can do the following:

  1. create an empty custom material
  2. select old material
  3. right-click on texture and choose copy
  4. select new material
  5. right-click on empty color texture slot anf select Paste as Instance
  6. assign new material to you object

note that the old material disappears, that is normal.

Now the python script will work.

Nice detective work. For now I will just use:

	obj.Attributes.MaterialIndex = source_mat
	obj.Attributes.MaterialSource = ObjectMaterialSource.MaterialFromObject
	obj.CommitChanges()
	# Make sure self-illumination is turned on or else trimmed mesh will be dark.
	mat = doc.Materials[obj.Attributes.MaterialIndex] # Does not work if source_mat is used here.
	mat.DisableLighting = True
	mat.CommitChanges()

I will try not to get distracted by the fact that the self-illumination checkbox is not checked. Manually checking and unchecking the Self-illumination box does toggle self-illumination for this mesh/material combination.

The Agisoft Photoscan program is generating the material. Maybe you could drop them a note about a better way to do the material.

Regards,
Terry.

I hadn’t heard of Agisoft Photoscan before, but as a user of it, wouldn’t it be better you told them about it yourself?

That said, I do think that on our side we should always just read materials such that they are fully functional v6 materials, not proxies or anything less: https://mcneel.myjetbrains.com/youtrack/issue/RH-50100

The challenge with my discussing this with Agisoft is that I do not know what they should change about their material. The material is actually a jpg file on which they put u,v coordinates that are referenced by the texture coordinates in the OBJ file. Here is an example:

This work I am doing deals with 3D models generated by photogrammetry of 100’s of photos taken by a drone. From the model one can extract many interesting quantitative results: elevation maps, contour maps, tree count, tree heights, tree canopy diameter, tree volume, estimated tree yield, cut and fill of piles (like rocks, sand, construction materials).

Regards,
Terry.

1 Like

Ah, so you do an OBJ import? In that case we should do the changes necessary. For some reason many of our importers create these old-style materials.

1 Like

I found that there are 4 choices when exporting to an OBJ file in Photoscan:
JPEG, PNG, TIFF and EXR.
Currently I am using JPEG.
Would PGN or TIFF or EXR work better with Rhino 6 for controlling the material self-illumination?

Regards,
Terry.

No, in the case of importers we need to fix them so that they don’t add old-style materials.