Help: trying to add a mapping and a mapping channel to an object

Hi guys, I am really struggeling to get the right parameters when trying to add a planar mapping to mapping channel 9 of an object.

When I run the script I get this error:
Message: expected TextureMapping, got type

import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino


def addPlanarMapping(obj_id,channel):
    bbox=rs.BoundingBox(obj_id)
    tm = Rhino.Render.TextureMapping
    plane = rs.WorldZXPlane()
    plane.Origin = bbox[0]
    
    dx= Rhino.Geometry.Interval(0,rs.Distance(bbox[0],bbox[1]))
    dy=Rhino.Geometry.Interval(0,rs.Distance(bbox[0],bbox[4]))
    dz=Rhino.Geometry.Interval(0,1)
    tm.CreatePlaneMapping(plane,dx,dy,dz)
    
    object = sc.doc.Objects.Find(obj_id)
    object.SetTextureMapping(channel,tm)

obj_id = rs.GetObject("mesh",rs.filter.mesh,preselect=True)
if obj_id: addPlanarMapping(obj_id,9)

Just in case I don’t get any answer I post my workaround here in case anybody else stumbles across this down the road some time :wink:

def addPlanarMapping(obj_id,channel):
    bbox=rs.BoundingBox(obj_id)
    rs.SelectObject(obj_id)
    rs.Command("_-ApplyPlanarMapping 3Point "+str(bbox[0])+" "+str(bbox[1])+" "+str(bbox[4])+" UV Single 1 1 9", False)
1 Like