In RhinoCommon, I’m trying to modify the attributes of a light, but it’s not working as expected. Changes to the LightObject attributes don’t seem to stick to the document.
With regular objects, I usually delete and re-add them and it works fine. But with lights, re-adding gives the light a new ID every time, probably because the original light is still “in use” even after being deleted. This is a problem because I need to keep track of the same light using its ID.
Another issue: when looping through the Lights table, I often get null objects, so it’s hard to know what’s actually in there, especially if something was deleted. And unlike other tables, there doesn’t seem to be a way to purge lights properly.
Also, when working with File3dm, I can’t access the light table at all, which blocks any attempt to import lights into the current document.
Would love to know if there’s a workaround, or if this could maybe be improved in RhinoCommon in the future. Thanks!
Here is a simple script to change the color of lights randomly:
// #! csharp
using System.Drawing;
using System;
using System.Collections.Generic;
using Rhino;
var rng = new System.Random();
int min_c = 75;
int max_c = 245;
// Dictionary to record changes we want to make
Dictionary<Guid, Rhino.Geometry.Light> lights = new Dictionary<Guid, Rhino.Geometry.Light>();
// Record new light geometry in a dictionary, shouldn't modify
// a table in a loop you're reading in.
foreach(var l in __rhino_doc__.Lights) {
Rhino.Geometry.Light lg = l.DuplicateLightGeometry();
lg.Diffuse = System.Drawing.Color.FromArgb(
255,
rng.Next(min_c, max_c),
rng.Next(min_c, max_c),
rng.Next(min_c, max_c));
lights[l.Id] = lg;
}
// Now modify the light changes we recorded
foreach(KeyValuePair<Guid, Rhino.Geometry.Light> kvp in lights) {
__rhino_doc__.Lights.Modify(kvp.Key, kvp.Value);
}
Another thing to log maybe as a request is if the lights table can iterate LightObjects and also be able to access the lights table from file3dm, is not possible at the moment
Some of the issues iterating the oight table are as-designed. Lights aren’t really the same as other tables because they are RhinoObjects in the document. They are generally kept in the table and marked deleted, in part because they may be marked not deleted in the event of an undo.
I am not sure what might cause an entry to become null, though. Do you have an example?