Add simple Texture Map in Python

I am searching around for a programmatic way of adding a simple planar UV texture map to an object in Python.

My goal is to add mappings to a bunch of flat surfaces representing planks in a wood floor, and vary the mapping parameters randomly so when I associate a material to that object (usually via a layer), the material will appear random.

Any ideas/pointers on how this could be done? Is this capability available in Python?

It is in the UI, so figure there must be hooks somewhere…

Thanks

Tom

Good news - I’m well on my way to making this work. Here is the code so far:

# Create a plane
new_plane_origin = [0.0, 0.0, 0.0]
new_plane_normal = [0.0, 0.0, 1.0]
new_plane = rs.PlaneFromNormal(new_plane_origin, new_plane_normal)

# Define mapping intervals
dx = r.Geometry.Interval(0.0, 3000.0)
dy = r.Geometry.Interval(0.0, 360.0)
dz = r.Geometry.Interval(0.0, 1.0)

# Create new texture mapping
new_mapping = r.Render.TextureMapping.CreatePlaneMapping(new_plane, dx, dy, dz, False)

# Create transforms to modify the mapping as needed

# Rotation
rotation_vector = r.Geometry.Vector3d(0.0, 0.0, 1.0)
rotation_center = r.Geometry.Point3d(0.0,0.0,0.0)
new_transform_rotation = r.Geometry.Transform.Rotation(math.pi/2, rotation_vector, rotation_center)

# Translation
new_transform_translation = r.Geometry.Transform.Translation(1.0, 50.0, 1.0)

# Combine Transforms into one
new_transform = r.Geometry.Transform.Multiply(new_transform_rotation, new_transform_translation)

# set the new texture mapping
rhino_object.SetTextureMapping(1, new_mapping, new_transform)

Hey Tom, did you figure this out? I’ve been trying to figure out how to adjust texture UV maps through python as well.

Hey - Yup, the code I posted above worked for my purposes. Its a little crude, but basically it allowed me to map the same wood texture to bunch of flat surfaces so I could approximate random textures. Here is the complete script:

import rhinoscriptsyntax as rs
import Rhino as r
import math
import random

# Select a bunch of FLAT surfaces representing planks of a wood floor
# This script will assign a planar texture mapping, and will randomize 
# the mapping so the planks appear different
# texture must be larger than plank for this work properly

#Texture Params
texture_dx = 118.110
texture_dy = 14.173

texture_rotation = math.pi/2
#texture_rotation = 0

#Plank Params
plank_dx = 5
plank_dy = 48

# Max Shift Values
max_shift_dx = texture_dx - plank_dy
max_shift_dy = texture_dy = plank_dx



filter = r.DocObjects.ObjectType.Surface

rc, objrefs = r.Input.RhinoGet.GetMultipleObjects("Select objects to assign random texture map", False, filter)

# for each object
for obj_ref in objrefs:

    r_obj = obj_ref.Object()

    # find the lower corner    
    bbox = rs.BoundingBox(r_obj, None, True)
    min_point = bbox[0]
    
    shift_dx = random.random()* max_shift_dx
    shift_dy = random.random()* max_shift_dy
    
    # Create a plane / TODO: Shift the point of the plane?
    new_plane_origin = [min_point[0]-shift_dx, min_point[1]-shift_dy, 0.0]
    new_plane_normal = [0.0, 0.0, 1.0]
    new_plane = rs.PlaneFromNormal(new_plane_origin, new_plane_normal)
    
    # Define mapping intervals
    dx = r.Geometry.Interval(0.0, texture_dx)
    dy = r.Geometry.Interval(0.0, texture_dy)
    dz = r.Geometry.Interval(0.0, 1.0)
    
    # Create new texture mapping
    new_mapping = r.Render.TextureMapping.CreatePlaneMapping(new_plane, dx, dy, dz, False)
    
    # Rotation
    rotation_vector = r.Geometry.Vector3d(0.0, 0.0, 1.0)
    rotation_center = r.Geometry.Point3d(0.0,0.0,0.0)
    new_transform_rotation = r.Geometry.Transform.Rotation(texture_rotation, rotation_vector, rotation_center)
    
    # set the new texture mapping
    r_obj.SetTextureMapping(1, new_mapping, new_transform_rotation)
3 Likes