Quantcast
Channel: Scripting - McNeel Forum
Viewing all articles
Browse latest Browse all 5938

Issue: rs.MatchObjectAttributes() makes objects dissapear

$
0
0

Hi there,

I use rs.MatchObjectAttributes() to clone the attributes (e.g. objectlayer, usertexts, etc) from objects, nested inside a block to their copies. (I use this mechanism as a workaround for this related issue)

When I run the script below with runPythonScript "C:/path/scriptname.py", I expect the copies to be in the document, but when I call rs.MatchObjectAttributes(), the objects seem to have become invalid (or at least unselectable). If rs.MatchObjectAttributes() is not called, the copies are valid and selected

I hope somebody can help me out.

Thanks,
Tim

test_issue_content_dissappear.3dm (395.0 KB)

#! python 2
import traceback as tb
import rhinoscriptsyntax as rs

class BlockDefinition(object):

    def __init__(self, name):
        self.name = name

    def traverse(self, func, result=None, block_name=None, parent_xform=rs.XformIdentity()):

        # initialize variables if not set
        if result == None:
            result = []

        if block_name == None:
            block_name = self.name

        # traverse block tree
        content = rs.BlockObjects(block_name)
        for obj in content:
            if rs.IsBlockInstance(obj):
                instance_xform = rs.BlockInstanceXform(obj)
                if parent_xform:
                    instance_xform = rs.XformMultiply(parent_xform, instance_xform )

                self.traverse( func, result, rs.BlockInstanceName(obj), instance_xform )

            func(obj, result, parent_xform)

        return result


def main():
    try:
        objs = rs.GetObjects(preselect=True, filter=rs.filter.instance)
        for obj in objs:
            if rs.IsBlockInstance(obj):
                block_name = rs.BlockInstanceName(obj)

                # traverse all child blocks, and add breps to the result
                definition = BlockDefinition(block_name)
                def _get_breps(obj, result, xform):
                    # when obj is a brep, add it to the result
                    if rs.IsBrep(obj):
                        copy = rs.TransformObject(obj, xform, copy=True)

                        """ ############# ISSUE OCCURS HERE ########### """
                        """ when I comment out the line below, the described issue does not occur """
                        rs.MatchObjectAttributes(copy, obj)
                        result.append(copy)

                content_copies = definition.traverse(_get_breps)
                print('content_copies', content_copies)

                # empty current selection and select copies
                rs.UnselectAllObjects()
                rs.SelectObjects(content_copies)
                print('selection', rs.SelectedObjects())

                rs.ZoomSelected()

    except Exception as exc:
        print(tb.format_exc())

if __name__ == '__main__':
    main()

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 5938

Trending Articles