Baking script creates duplicates

I use a script i got from Digital Substance to bake non-standard components like dimensions and text. It works by collecting these components by type name and component name, then baking them in a for loop. However, this for loop has no way of deleting items from its list after baking, which causes it to create duplicates. I don’t know how to improve the script, but i assume it shouldn’t be to hard. If anyone can point me in the right direction i’d very much appreciate it.

#imports
import Grasshopper as gh
import Rhino as rc
import ghpythonlib.components as comp
from System import *
from System.Collections.Generic import *

#Active Rhinodoc
rcdoc = rc.RhinoDoc.ActiveDoc
#Active GH document
my_ghdoc = ghenv.Component.OnPingDocument()
attr =rc.DocObjects.ObjectAttributes()

# Bake the dimensions in the specified layer called Dimensions
#if it doesn't exist it used the default
attr.LayerIndex = rcdoc.Layers.Find(L,True)

# Iterate the active  objects in the current GH file and find the one of the specific type
#if there are many look for the one with the name
objs = []
obj_types = []

for obj in my_ghdoc.ActiveObjects():
    
    my_type = obj.GetType()
    obj_types.append(my_type)
    
    #uncomment the next line if you want to see all the component types in the document
    if str(my_type) == Comp_type and obj.NickName == Comp_name:
            
            objs.append(obj)
            print("found it")
        
            #bake the component
            if Bake == True:
                
                for obj in objs:
                    
                #create an empty GUID list
                    ids = List[Guid]()
                    obj.BakeGeometry(rcdoc,attr,ids)
                    print("baked the dimension elements")

doc_components = obj_types

The indentation is just wrong - deindent that last if statement to the left most level:

#bake the component
if Bake == True:
    
    for obj in objs:
        
    #create an empty GUID list
        ids = List[Guid]()
        obj.BakeGeometry(rcdoc,attr,ids)
        print("baked the dimension elements")
1 Like

Trying to run this script, however I do get this error:

Runtime error (UnboundNameException): name ‘Comp_name’ is not defined
Traceback:
line 41, in script

Ha, that was easy. Not yet used to this bracketless syntax. Thanks!

You have to hook up some variables. L is for layer, Bake is for button/toggle. doc_components output will give you the types of components in your GH file. Component name is the name of the specific GH component.

2 Likes

Still not working. What it is wrong?

bake_Dim.gh (12.1 KB)

I think this is the original Digital Substance script. Mine is a bit different, and strangely it seems to work when i paste it into your file. I adapted it last week but i forgot exactly why or how i fixed it. I think i just simplified it by swapping the if for the AND statement. Try my script above and see if it works for you.

Finaly, it’s working, but it is baking the same thing five times!

Right, that was my original problem. Once more, with the fix.

#imports
import Grasshopper as gh
import Rhino as rc
import ghpythonlib.components as comp
from System import *
from System.Collections.Generic import *

#Active Rhinodoc
rcdoc = rc.RhinoDoc.ActiveDoc
#Active GH document
my_ghdoc = ghenv.Component.OnPingDocument()
attr =rc.DocObjects.ObjectAttributes()

# Bake the dimensions in the specified layer called Dimensions
#if it doesn't exist it used the default
attr.LayerIndex = rcdoc.Layers.Find(L,True)

# Iterate the active  objects in the current GH file and find the one of the specific type
#if there are many look for the one with the name
objs = []
obj_types = []

for obj in my_ghdoc.ActiveObjects():
    
    my_type = obj.GetType()
    obj_types.append(my_type)
    
    #uncomment the next line if you want to see all the component types in the document
    if str(my_type) == Comp_type and obj.NickName == Comp_name:
            
        objs.append(obj)
        print("found it")
        
#bake the component
if Bake == True:
    
    for obj in objs:
        
        #create an empty GUID list
        ids = List[Guid]()
        obj.BakeGeometry(rcdoc,attr,ids)
        print("baked the dimension elements")

doc_components = obj_types
1 Like

Nice. Just tested the latest code and works as expected.

Just created a definition as an exercise to bake multiple text elements using only one Bake button.

bake_Dim.gh (17.6 KB)

2 Likes