Last active
June 14, 2018 23:14
-
-
Save pgolay/35ae83c9ff2f40a47d4a290ebdd74a61 to your computer and use it in GitHub Desktop.
Find and highlight objects with high polygon counts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Rhino | |
import rhinoscriptsyntax as rs | |
import scriptcontext as sc | |
def MeshCountFromBlock(id, blnTris): | |
bObjs = rs.BlockObjects(rs.BlockInstanceName(id)) | |
for bId in bObjs: | |
if sc.sticky.has_key("BLOCK_MESHES"): | |
mCount = sc.sticky["BLOCK_MESHES"] | |
else: | |
mCount = 0 | |
if rs.IsSurface(bId) or rs.IsPolysurface(bId): | |
obj = sc.doc.Objects.Find(bId) | |
meshes = obj.GetMeshes(Rhino.Geometry.MeshType.Render) | |
for n in range(len(meshes)): | |
if blnTris: | |
meshes[n].Faces.ConvertQuadsToTriangles() | |
mCount += meshes[n].Faces.Count | |
sc.sticky["BLOCK_MESHES"] = mCount | |
elif rs.IsBlockInstance(bId): | |
MeshCountFromBlock(bId, blnTris) | |
elif rs.IsMesh(bId): | |
mesh = sc.doc.Objects.Find(bId).Geometry | |
if blnTris: | |
mesh.Faces.ConvertQuadsToTriangles() | |
mCount += mesh.Faces.Count | |
sc.sticky["BLOCK_MESHES"] = mCount | |
def SelHighPolygonCount(): | |
ids = rs.ObjectsByType(8+16+32+4096) | |
if not ids: | |
print "No meshes or objects with render meshes found." | |
return | |
selCount = 0 | |
while True: | |
memInfo = False | |
if sc.sticky.has_key("MEM_SIZE_INFO"): | |
memInfo = sc.sticky["MEM_SIZE_INFO"] | |
polyCount = 5000 | |
if sc.sticky.has_key("POLYCOUNT"): | |
polyCount = sc.sticky["POLYCOUNT"] | |
labels = True | |
if sc.sticky.has_key("MEMHOGLABELS"): | |
labels = sc.sticky["MEMHOGLABELS"] | |
blnTris = True | |
if sc.sticky.has_key("TRIANGULATE_QUADS"): | |
blnTris = sc.sticky["TRIANGULATE_QUADS"] | |
gi = Rhino.Input.Custom.GetInteger() | |
gi.SetDefaultNumber(polyCount) | |
gi.SetCommandPrompt("Find objects with mesh faces greater than") | |
gi.AcceptNothing(True) | |
#gi.SetCommandPrompt("Set the number of objects to label") | |
opMeshInfo = Rhino.Input.Custom.OptionToggle(memInfo, "No", "Yes") | |
opLabels = Rhino.Input.Custom.OptionToggle(labels, "No", "Yes") | |
opTris = Rhino.Input.Custom.OptionToggle(blnTris, "No", "Yes") | |
gi.AddOptionToggle("AddLabels", opLabels) | |
gi.AddOptionToggle("MemoryInfo", opMeshInfo) | |
gi.AddOptionToggle("Triangles", opTris) | |
rc = gi.Get() | |
if gi.CommandResult()!=Rhino.Commands.Result.Success: | |
return None | |
if rc==Rhino.Input.GetResult.Number: | |
polyCount = gi.Number() | |
sc.sticky["POLYCOUNT"] = polyCount | |
break | |
elif rc==Rhino.Input.GetResult.Nothing: | |
break | |
elif rc==Rhino.Input.GetResult.Option: | |
memInfo = opMeshInfo.CurrentValue | |
sc.sticky["MEM_SIZE_INFO"]= memInfo | |
labels = opLabels.CurrentValue | |
sc.sticky["MEMHOGLABELS"] = labels | |
blnTris = opTris.CurrentValue | |
sc.sticky["TRIANGULATE_QUADS"]= blnTris | |
continue | |
#polyCount = rs.GetInteger("There are "+ str(len(temp)) +" objects. How many would you like to label?",oldMark,1) | |
if not polyCount: return | |
selIds = [] | |
strPolys = " polygons. " | |
if blnTris: strPolys = " triangles. " | |
rs.EnableRedraw(False) | |
for id in ids: | |
if rs.IsObjectSelectable(id): | |
if rs.IsBlockInstance(id): | |
MeshCountFromBlock(id, blnTris) | |
if sc.sticky.has_key("BLOCK_MESHES"): | |
mCount = sc.sticky["BLOCK_MESHES"] | |
sc.sticky.Remove("BLOCK_MESHES") | |
else: | |
strInfo = None | |
obj = sc.doc.Objects.Find(id) | |
x = Rhino.DocObjects.RhinoObject.GetRenderMeshes([obj], True, False) | |
meshes = obj.GetMeshes(Rhino.Geometry.MeshType.Render) | |
pass | |
mCount = 0 | |
for n in range(len(meshes)): | |
if blnTris: | |
meshes[n].Faces.ConvertQuadsToTriangles() | |
mCount += meshes[n].Faces.Count | |
if mCount >= polyCount: | |
selCount += 1 | |
strInfo = "{:,}".format(mCount) + strPolys + "\n" | |
bb = rs.BoundingBox(id) | |
pt = bb[0] + ((bb[6]-bb[0])/2) | |
selIds.append(id) | |
if memInfo: | |
mem = obj.Geometry.MemoryEstimate()/1000 | |
strInfo = strInfo + "{:,}".format(mem) + " k memory usage estimate." | |
print strInfo | |
if labels: | |
dot = rs.AddTextDot(strInfo,pt) | |
rs.ObjectLayer(dot, rs.ObjectLayer(id)) | |
rs.AddObjectsToGroup([id, dot], rs.AddGroup()) | |
selIds.append(dot) | |
rs.SelectObjects (selIds) | |
strOb = " objects " | |
if selCount == 1: strOb = " object " | |
print str(selCount)+ strOb +"found with at least " + "{:,}".format(polyCount) + " polygons." | |
rs.EnableRedraw(True) | |
if __name__ == "__main__": | |
SelHighPolygonCount() |
Fixed a typo.
Number formatting.
Typo converting to k
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added support for block instances. I think.