After lots of messing around, got it working. Mixed results though.
The layers are getting applied in the correct locations, but not all the geometry is getting done. Only one piece of geometry per layer is getting assigned. Everything else is default.
The strangest part is that once the layer gets used in any block, no other blocks with the same layers ever get it applied anywhere.
Also, layer hierarchy is lost. Parent layers aren’t imported.

Also, for anybody who comes after and wants to know what I did to get it working:

filePaths needs list access
the top two inputs are strings, the bottom are booleans
private void RunScript(List filePaths, string blockName, bool attach, bool all, ref object A)
** {**
A = ImportBlocks(filePaths, blockName, all);
/// <summary>
/// Finds and imports blocks from library files.
/// If block with the same name exists in the current file then nothing is imported.
/// </summary>
/// <param name="filePaths">File paths to library 3dm files with blocks. If there are several blocks with the same name the first one will be imported.</param>
/// <param name="blockName">Block name. Masks(* or ?) can be used.</param>
/// <param name="all">If true, all blocks matching this mask <paramref name="blockName"/> will be imported.
/// If false, only first one will be imported.
/// Set true only if you use masks (such as blockName_*).</param>
/// <returns>Existing or newly created BlockDefenition index.</returns>
}
internal static List <int> ImportBlocks(List <string> filePaths, string blockName, bool all)
{
var iDefIndexes = new List<int>();
if (string.IsNullOrEmpty(blockName)) return iDefIndexes;
string blockNamePattern = WildcardToRegex(blockName);
var rhinodoc = Rhino.RhinoDoc.ActiveDoc;
var allDefsNames = rhinodoc.InstanceDefinitions.Select(d => d.Name).ToList();
List<string> defNames = allDefsNames
.Where(n => n != null)
.Where(n => Regex.IsMatch(n, blockNamePattern))
.ToList();
foreach (var defName in defNames)
{
var instanceDefinition = rhinodoc.InstanceDefinitions.Find(defName);
iDefIndexes.Add(instanceDefinition.Index);
if (!all) return iDefIndexes; // выходим, если нужен только один блок.
}
if (filePaths != null && filePaths.Any()) // если есть пути то нужно посмотреть в файлах-библиотеках.
{
foreach (var filePath in filePaths)
{
using (var libraryDoc = Rhino.RhinoDoc.CreateHeadless(null))
{
if (!System.IO.File.Exists(filePath))
{
// AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, $"Path {filePath} doesn't exist");
continue;
}
if (!libraryDoc.Import(filePath)) continue;
var allLibDefsNames = libraryDoc.InstanceDefinitions.Select(d => d.Name).ToList();
var libDefNames = allLibDefsNames
.Where(n => Regex.IsMatch(n, blockNamePattern))
.Where(n => !defNames.Contains(n)) // лишний раз не перезаписывать существующие с таким же именем!
.ToList();
foreach (var libDefName in libDefNames)
{
var libraryInstanceDefinition = libraryDoc.InstanceDefinitions.Find(libDefName);
if (libraryInstanceDefinition.IsDeleted)
{
libraryInstanceDefinition = null;
continue;
}
var geom = libraryInstanceDefinition.GetObjects().Select(o => o.Geometry);
var attr = libraryInstanceDefinition.GetObjects().Select(o => o.Attributes);
foreach (var att in attr)
{
var layerIndex = att.LayerIndex;
var outerLayer = libraryDoc.Layers[layerIndex];
var layerFullName = outerLayer.FullPath;
var localLayerIndex = rhinodoc.Layers.FindByFullPath(layerFullName, -1);
if (localLayerIndex == -1)
{
var materialIndex = AddLayerMaterial(outerLayer, libraryDoc, rhinodoc);
var newIndex = rhinodoc.Layers.Add(outerLayer);
att.LayerIndex = newIndex;
var addedLayer = rhinodoc.Layers[newIndex];
addedLayer.RenderMaterialIndex = materialIndex; //???
}
else
{
att.LayerIndex = localLayerIndex;
}
}
var iDefIndex = rhinodoc.InstanceDefinitions.Add(libraryInstanceDefinition.Name, libraryInstanceDefinition.Description, libraryInstanceDefinition.Url, libraryInstanceDefinition.UrlDescription, Rhino.Geometry.Point3d.Origin, geom, attr);
if (iDefIndex >= 0)
{
iDefIndexes.Add(iDefIndex);
if (!all)
{
return iDefIndexes;
}
}
}
}
}
}
return iDefIndexes;
}
private static int AddLayerMaterial(Rhino.DocObjects.Layer outerLayer, Rhino.RhinoDoc libraryDoc, Rhino.RhinoDoc rhinodoc)
{
var material = outerLayer.RenderMaterial;
var materialIndex = outerLayer.RenderMaterialIndex;
if (material == null)
{
material = libraryDoc.Materials[materialIndex].RenderMaterial;
}
var localMaterialIndex = rhinodoc.Materials.Find(material.Name, true);
if (localMaterialIndex == -1)
{
if (materialIndex != -1)
{
var outerMaterial = libraryDoc.Materials[materialIndex];
localMaterialIndex = rhinodoc.Materials.Add(outerMaterial);
}
}
return localMaterialIndex;
//rhinodoc.RenderMaterials.
}
public static string WildcardToRegex(string pattern)
{
return "^" + System.Text.RegularExpressions.Regex.Escape(pattern)
.Replace(@"\*", ".*")
.Replace(@"\?", ".")
+ "$";
}
You will also need to make sure these declarations are at the top:
using System.Linq;
using System.Text.RegularExpressions;