Notifications
Clear all

[Closed] Trouble with vector

Hey Guys,

  I created a script that allows me to pick a color from a texture on a plane and find the bounding area of that color. From there I the script creates a mesh plane based on the bounding area of the picked color.  The script works fine when my textured object is at 0,0,0. However, when I move the object from the origin, it fails. Also there is strange behavior when the object is not at 0,0,0. If there is a edit_mesh modifier on a plane, the script will fail to create a plane based off the color value, and it will remove the modifier from the stack, or it will move the object back to the origin. Almost like it performed and undo. I use the painter interface to sample the color from the texture. Here is the function that actually looks for my selected color value: 

          fn findColorBox obj cpColor yOz =
          --Function will find the B.Box of the specified color then return the min and max position
          --obj = my selected object, cpColor = the color from my colorpicker, yOz = state of my checkbox if y or z is up
          (
              try
              (
                  if classof obj != Editable_mesh then
                  (
                      messagebox "please pick an editable mesh"
                  )
                  bb =  nodeLocalBoundingBox obj --get the greyscale bitmap from a floor object
                  bm = obj.material.diffusemap.bitmap -- get the bitmap texture from the plane
                  incVal = (distance [bb[1][1],bb[1][2],bb[1][3]] [bb[2][1],bb[1][2],bb[1][3]] )/bm.width -- get distance between 2 points for length of plane and calc the interation size
      
                  if yOz == true then --check if y or z is up
                  (
                      topVal = bb[1][2]
                      botVal = bb[2][2]
                      state =0
                  )
                  else
                  (
                      topVal = bb[1][3]
                      botVal = bb[2][3]
                      state = 1
                  )
      
                  for i = topVal to botVal by incVal do -- for top of the b. box to bottom
                  (
                      if state == 0 then -- check if y or z is up
                      (
                          offSetVect = [0,0,-300]
                          tempCent = obj.transform.position
                          rayCent = tempCent+[(bb[1][1]),i,300] --increment ray down y or z 
                          print "Why am I working"
                      )
                      else
                      (
                          offSetVect = [0,300,0]
                          tempCent = obj.transform.position
                          rayCent = tempCent+[(bb[1][1]),-300,i]
                          print "State = 1"
                      )
                      -- create rays and find pos, barys and tex face
                      print rayCent
                      print offSetVect
                      myRay = ray (rayCent)(obj.position+offSetVect)  -- make a ray that shoots at the plane that travles and hits each pixel in y or z
                      rayStuff = intersectRayEx obj myRay -- add the ray, bary coords, and position to the array
      
                      -- get the texture faces
                      texFace = getTVFace obj rayStuff[2]
                              
                      -- Get the UVW verts of the face
                      tv1 = getTVert obj texFace.x
                      tv2 = getTVert obj texFace.y
                      tv3 = getTVert obj texFace.z
              
                      -- make the t verts from bary
                      tv = tv1*rayStuff[3].x + tv2*rayStuff[3].y + tv3*rayStuff[3].z
                      tempV = tv[2]*bm.width --find the v value of my uv
                      tempV = bm.Width - tempV --reverse the V
      
                      -- get the pixels and start checking them for a value
                      pix = getPixels bm [0,tempV] bm.width
                      if pix != undefined then
                      (
                          for t = 1 to pix.count do
                          (
                              classof pix[t]
                              if pix[t] == cpColor do
                              (
                                  offsetX = t*incVal --find the pos of the pixel in x
                                  append pm.myPos (rayStuff[1].pos + [offsetX,0,0] )
                              )
                          )
                      ) -- end if
                      else
                      (
                      )
                      pb1.value = (100.*i/(distance [bb[1][1],bb[1][2],bb[1][3]] [bb[2][1],bb[1][2],bb[1][3]]))
                  )-- end i for loop    
                  pm.myPos = #(pm.myPos[1], pm.myPos[pm.myPos.count])        
                  pb1.value = 0
              )
              catch
              (
                  print state
                  print offSetVect
              )
          )-- end check color value function 
      

Any insight or help would be appreciated

2 Replies

I think you have to add the translation of the object to the bounding box. the bounds are in local space…

Thanks for the help! I was moving the ray in the wrong space. Tweaked the code a bit and it worked. Thanks again.