Hi @Dale,
based on your answer on my other topic, I’ve made a modified version of my script. It still puzzles me that copies seem to disappear from the doc when I match the attributes with the orginal.
If I run the script below, I expect the content from all blocks to be in the document. However, the breps from linked blocks have disappeared.
I hope you can help me out.
Best regards,
Tim
test_issue_content_dissappear.3dm (395.0 KB)
E_side (linked block).3dm (384.8 KB)
#! python3
import rhinoscriptsyntax as rs
import scriptcontext as sc
def traverse(block_name, my_func, result=None, parent_xform = rs.XformIdentity()):
if result is None:
result=[]
block_objects = rs.BlockObjects(block_name)
for obj in block_objects:
if rs.IsBlockInstance(obj):
child_block_name = rs.BlockInstanceName(obj)
xform = rs.BlockInstanceXform(obj)
traverse(child_block_name, my_func, result, xform * parent_xform)
my_func(obj, result, parent_xform)
return result
def main():
# let user select the input
obj = rs.GetObject("select block with nested linked instances", filter=rs.filter.instance, preselect=True)
if not obj:
return
block_name = rs.BlockInstanceName(obj)
def create_transformed_copy(obj, result, parent_xform):
"""
function to create a copy of objects that are not instances
@param obj: GUID object ref
@param result: list of function result
@param parent_xform: parent transformation matrix
return None
"""
if not rs.IsBlockInstance(obj):
copy = rs.TransformObject(obj, parent_xform, True)
# -----------------------
# match attributes of original to copy
if rs.IsObjectReference(obj):
source = rs.coercerhinoobject(obj)
source_attr = source.Attributes.Duplicate()
target = rs.coerceguid(copy, True)
sc.doc.Objects.ModifyAttributes(target, source_attr, True)
result.append(copy)
# recursively run this function on all objects in tree
result = traverse( block_name, create_transformed_copy)
# "result" contains all breps nested inside the block
print('result', result)
# "selection" unexpectedly does not contain breps from linked blocks
rs.UnselectAllObjects()
rs.SelectObjects(result)
print('selection', rs.SelectedObjects())
rs.ZoomSelected()
if __name__ == '__main__':
main()