BUG MatchObjectAttributes

Hi All,

I wanted to point out a potential bug when using rs.MatchObjectAttributes() .

import rhinoscriptsyntax as rs

srcId = rs.GetObject('pick source')
targetId = rs.GetObject('pick target')
rs.MatchObjectAttributes(targetId, srcId)

Whilst all attributes are copied from srcId to targetId, User Strings are completely removed from srcId object in the process.

The implementation of rs.MatchObjectAttributes should probably look more like this:

def MatchObjectAttributes(target_ids, source_id=None):
    """Matches, or copies the attributes of a source object to a target object
    Parameters:
      target_ids = identifiers of objects to copy attributes to
      source_id[opt] = identifier of object to copy attributes from. If None,
        then the default attributes are copied to the target_ids
    Returns:
      number of objects modified
    """
    id = rhutil.coerceguid(target_ids, False)
    if id: target_ids = [id]
    source_attr = scriptcontext.doc.CreateDefaultAttributes()
    if source_id:
        source = rhutil.coercerhinoobject(source_id, True, True)
        source_attr = source.Attributes.Duplicate()
    rc = 0
    for id in target_ids:
        id = rhutil.coerceguid(id, True)
        if scriptcontext.doc.Objects.ModifyAttributes(id, source_attr, True):
            rc += 1
    if rc: scriptcontext.doc.Views.Redraw()
    return rc

http://mcneel.myjetbrains.com/youtrack/issue/RH-29590

That’s the one!

Hi Dale,

It says "No module named utility"
my script is as follows

import rhinoscriptsyntax as rs
import Rhino.Geometry
import Rhino
import scriptcontext
import utility as rhutil
import System.Enum
import math

thanks

If you remove:

import utility as rhutil

Does your code run?

Hi Dale,

It says

Message: global name ‘rhutil’ is not defined

Thanks

Without the code that does not work, we’re not going to get very far…

If you are trying to use the above in your own code, then use this:

import rhinoscriptsyntax as rs
import scriptcontext

def MyMatchObjectAttributes(target_ids, source_id=None):
    """Matches, or copies the attributes of a source object to a target object
    Parameters:
      target_ids = identifiers of objects to copy attributes to
      source_id[opt] = identifier of object to copy attributes from. If None,
        then the default attributes are copied to the target_ids
    Returns:
      number of objects modified
    """
    id = rs.coerceguid(target_ids, False)
    if id: target_ids = [id]
    source_attr = scriptcontext.doc.CreateDefaultAttributes()
    if source_id:
        source = rs.coercerhinoobject(source_id, True, True)
        source_attr = source.Attributes.Duplicate()
    rc = 0
    for id in target_ids:
        id = rs.coerceguid(id, True)
        if scriptcontext.doc.Objects.ModifyAttributes(id, source_attr, True):
            rc += 1
    if rc: scriptcontext.doc.Views.Redraw()
    return rc

Thanks,thats great