def SampleSynchronizeRenderColors():

    objects = rs.AllObjects()
    if objects is None: return

    for obj in objects:

        color = rs.ObjectColor(obj)
        source = rs.ObjectColorSource(obj)
        material = -1

        if source == 0:
            layer = rs.ObjectLayer(obj)
            material = rs.LayerMaterialIndex(layer)
            if material < 0:
                material = rs.AddMaterialToLayer(layer)

        elif source == 1:
            material = rs.ObjectMaterialIndex(obj)
            if material < 0:
                material = rs.AddMaterialToObject(obj)

        if material >= 0:
            if color != rs.MaterialColor(material):
                rs.MaterialColor(material, color)

    rs.Redraw()
def applyVrayMatToLayer(layer, matName):
    """
    applies vray mat to a layer
    input: layer, single layer name - matName: Vray mat string e.g. /glass_architectural
    returns: None
    """
    srf = rs.AddSphere([0,0,0], 1)
    rs.SelectObject(srf)
    rs.Command("-_visApplyMaterial "+matName)
    matIndex = rs.ObjectMaterialIndex(srf)
    rs.LayerMaterialIndex(layer, matIndex)
    rs.DeleteObject(srf)
    return None
def matIndexByMatName(matName):
    rs.EnableRedraw(False)
    circle = rs.AddCircle([0, 0, 0], 5)
    srf = rs.AddPlanarSrf(circle)
    #rs.ObjectMaterialIndex(srf, material_index =8)
    rs.SelectObject(srf)
    rs.Command("-_visApplyMaterial " + matName)
    matIndex = rs.ObjectMaterialIndex(srf)
    rs.DeleteObject(circle)
    rs.DeleteObject(srf)
    #obj = rs.coercerhinoobject(srf)
    #obj.Attributes.MaterialIndex = 8
    #print obj.Attributes.MaterialIndex
    rs.EnableRedraw(True)
    return matInd
Exemple #4
0
def ObjectColorToMaterial():
    try:
        objs = rs.GetObjects("Select objects to transfer color to material",
                             1073750077,
                             preselect=True)
        if objs is None: return
        for obj in objs:
            color = rs.ObjectColor(obj)
            index = sc.doc.Materials.Add()
            mat = sc.doc.Materials[index]
            mat.DiffuseColor = color
            mat.Name = str(color)
            mat.CommitChanges()
            rs.ObjectMaterialIndex(obj, index)
            rs.ObjectMaterialSource(obj, 1)
        return True
    except:
        return False
def add_cube_w_material(count):

    for i in xrange(count):
        for j in xrange(count):


            ### define pt, plane
            tmp_pt = rs.AddPoint(i * 2, j * 2, 0)
            tmp_pln = rs.PlaneFromPoints(tmp_pt,
            rs.PointAdd(tmp_pt, (1,0,0)), rs.PointAdd(tmp_pt, (0,1,0)))


            ### define extrude line
            line_ex = rs.AddLine((0,0,0), (0,0,1))


            ### draw rect
            tmp_crv = rs.AddRectangle(tmp_pln, 1,1)
            tmp_surf = rs.AddPlanarSrf(tmp_crv)
            tmp_box = rs.ExtrudeSurface(tmp_surf, line_ex, True)


            ### set color
            rs.AddMaterialToObject(tmp_box)

            index = rs.ObjectMaterialIndex(tmp_box)

            rs.MaterialName(index, str(i) + "_" +str(j))
            rs.MaterialColor(index,  ((255 / count) * i, 255 - ((255 / count) * j), 255 - ((255 / count) * j)))

            # name = rs.MaterialName(index)
            # print name


            ### delete
            rs.DeleteObject(tmp_pt)
            rs.DeleteObject(tmp_crv)
            rs.DeleteObject(tmp_surf)
            rs.DeleteObject(line_ex)
Exemple #6
0
def processObject(object, parentInstances):
    global g_instances
    global g_instancesByName
    global g_parts
    global g_materials

    name = rs.ObjectName(object)
    if not name:
        name = "Unnamed"
    type = rs.ObjectType(object)
    layer = rs.ObjectLayer(object)

    if type == rs.filter.instance:
        type = rs.BlockInstanceName(object)

        xform = rs.BlockInstanceXform(object)

        # Seems like transforms are in global frame already
        # --> Probably due to exploding the block hierarchies...
        #for parent in reversed(parentInstances[1:]) :
        #    xform = parent["xform"] * xform

        subObjects = rs.ExplodeBlockInstance(object)

        fullName = name
        if len(parentInstances) > 1:
            for parent in parentInstances[1:]:
                fullName = parent["name"] + "." + fullName
        originalFullName = fullName

        appendixCtr = 1
        while fullName in g_instancesByName:
            fullName = format("%s+%d" % (originalFullName, appendixCtr))
            appendixCtr += 1
        if fullName != originalFullName:
            print("WARNING: Renamed %s => %s" % (originalFullName, fullName))

        instance = \
        {
               "name" : name,
           "fullName" : fullName,
               "type" : type,
              "xform" : xform,
            "parents" : list(parentInstances),
              "parts" : [],
            "touched" : False,
        }
        g_instances.append(instance)
        g_instancesByName[fullName] = instance

        for subObject in subObjects:
            processObject(subObject, parentInstances + [instance])
        return

    skipReason = None
    if rs.LayerLocked(layer):
        skipReason = "layer locked"
    elif not rs.LayerVisible(layer):
        skipReason = "layer hidden"
    elif type != rs.filter.polysurface and type != rs.filter.surface:
        skipReason = "bad type - " + typeStr[type]

    if skipReason:
        # make sure we can delete object by moving to current layer
        rs.ObjectLayer(object, rs.CurrentLayer())
        print("Skipping %s (%s)" % (str(object), skipReason))
    else:
        brep = rs.coercebrep(object)
        meshes = rc.Geometry.Mesh.CreateFromBrep(brep, g_meshParams)
        joinedMesh = rc.Geometry.Mesh()
        for mesh in meshes:
            joinedMesh.Append(mesh)
        joinedMesh.Reduce(0, False, 10, False)
        if not joinedMesh.Faces.ConvertQuadsToTriangles():
            print("WARNING: Failed to convert quads to tris for %s" %
                  (str(object)))
        if not joinedMesh.Compact():
            print("WARNING: Failed to compact %s" % (str(object)))

        materialSrc = rs.ObjectMaterialSource(object)
        if materialSrc == 0:
            materialIdx = rs.LayerMaterialIndex(rs.ObjectLayer(object))
        else:
            materialIdx = rs.ObjectMaterialIndex(object)

        material = rs.MaterialName(materialIdx)
        if not material:
            material = "None"
        g_materials[material] = materialIdx

        joinedMeshGuid = sc.doc.Objects.AddMesh(joinedMesh)
        rs.ObjectName(joinedMeshGuid, name)
        rs.ObjectMaterialSource(joinedMeshGuid, 1)
        rs.ObjectMaterialIndex(joinedMeshGuid, materialIdx)

        part = \
        {
                "name" : name,
                "mesh" : joinedMesh,
            "instance" : parentInstances[-1],
            "material" : material,
        }
        parentInstances[-1]["parts"].append(part)
        if not parentInstances[-1]["touched"]:
            for parentInstance in parentInstances:
                parentInstance["touched"] = True
        g_parts.append(part)

    rs.DeleteObject(object)
import rhinoscriptsyntax as rs

sourceObjects = rs.GetObjects(message="Select Objects to use color",
                              preselect=True,
                              select=False)

for obj in sourceObjects:
    obj_color = rs.ObjectColor(obj)
    obj_mat = rs.ObjectMaterialIndex(obj)
    if obj_mat == -1:
        obj_mat = rs.AddMaterialToObject(obj)
    rs.MaterialColor(obj_mat, obj_color)
    rs.ObjectPrintColor(obj, color=rs.ObjectColor(obj))
Exemple #8
0
        tmp_pt = rs.AddPoint(i * 2, j * 2, 0)
        tmp_pln = rs.PlaneFromPoints(tmp_pt, rs.PointAdd(tmp_pt, (1, 0, 0)),
                                     rs.PointAdd(tmp_pt, (0, 1, 0)))

        ### define extrude line
        line_ex = rs.AddLine((0, 0, 0), (0, 0, 1))

        ### draw rect
        tmp_crv = rs.AddRectangle(tmp_pln, 1, 1)
        tmp_surf = rs.AddPlanarSrf(tmp_crv)
        tmp_box = rs.ExtrudeSurface(tmp_surf, line_ex, True)

        ### set color
        rs.AddMaterialToObject(tmp_box)

        index = rs.ObjectMaterialIndex(tmp_box)

        rs.MaterialName(index, str(i) + "_" + str(j))
        rs.MaterialColor(index,
                         ((255 / count) * i, 255 - ((255 / count) * j), 255 -
                          ((255 / count) * j)))

        name = rs.MaterialName(index)
        #        print name

        ### delete
        rs.DeleteObject(tmp_pt)
        rs.DeleteObject(tmp_crv)
        rs.DeleteObject(tmp_surf)
        rs.DeleteObject(line_ex)