Exemplo n.º 1
0
    def __init__(self, context):
        super(make_test, self).__init__()

        self._radius = lwsdk.Vector(0.5, 0.5, 0.5)
        self._nsides = 24
        self._nsegments = 12
        self._center = lwsdk.Vector(0.0, 0.0, 0.0)
Exemplo n.º 2
0
    def evaluate(self, sa):
        r2 = 0.0

        # the wrapper layer copies whole arrays at once, even if you reference
        # only a single element (if you reference multiple elements, the array
        # is copied in whole each time!).  so, it is more efficient to pull it
        # all down once and then work with it locally.

        v_oPos = lwsdk.Vector(sa.oPos)
        v_color = lwsdk.Vector(sa.color)

        for i in range(3):
            d = v_oPos[i] - center[i]
            d *= d
            if d > self._r2:
                return

            r2 += d

        if r2 > self._r2:
            return

        d = math.sqrt(r2)
        d = math.cos(d * self._piOverR) * softness
        if d > 1.0:
            d = 1.0
        a = 1.0 - d

        colors = v_color * a + color * d

        # copy the whole array back at once for efficiency.
        sa.color = colors
Exemplo n.º 3
0
    def evaluate(self, da):
        item_info = lwsdk.LWItemInfo()

        pivot = item_info.param(self._itemid, lwsdk.LWIP_PIVOT, self._time)
        oPos = lwsdk.Vector(da.oPos)
        original = oPos - pivot
        magnitude = lwsdk.Vector.magnitude(oPos, pivot)
        lag_time = self._time - self._lag_rate * magnitude

        xax = item_info.param(self._itemid, lwsdk.LWIP_RIGHT, lag_time)
        yax = item_info.param(self._itemid, lwsdk.LWIP_UP, lag_time)
        zax = item_info.param(self._itemid, lwsdk.LWIP_FORWARD, lag_time)
        pos = item_info.param(self._itemid, lwsdk.LWIP_W_POSITION, lag_time)

        da.source = [
                    pos.x + original.x * \
                    xax.x + original.y * \
                    yax.x + original.z * zax.x,

                    pos.y + original.x * \
                    xax.y + original.y * \
                    yax.y + original.z * zax.y,

                    pos.z + original.x * \
                    xax.z + original.y * \
                    yax.z + original.z * zax.z
                    ]
Exemplo n.º 4
0
    def evaluate(self, ca):
        t = ca.time * self._speed

        V = lwsdk.Vector()
        V.x = 10 * t
        V.y = self._phase
        V.z = 20

        # LWTextureFuncs is a complex mess, and is not completely exported for
        # Python usage.  Only simple function calls and data members can be
        # accessed successfully. at this time.

        val = lwsdk.LWTextureFuncs().noise(V)
        val *= self._scale
        val += self._offset
        val += ca.value

        ca.setChannel(ca.chan, val)
 def polytree(polys, points):
   #here we build a tree on which polys belong to a point.
   #n will store the polyIDs assignement per point
   #nfullNormals will add the poly normals together for that point from the belonging polys
   n = []
   nfullNormals = []
   #create empty arrays
   for p in points:
    n.append([])
    nfullNormals.append([])
   #go through each poly checking with points belong to it.
   count = 0
   for poly in polys:
     pts = mesh_edit_op.polyPoints(mesh_edit_op.state,poly)
     for p in pts:
       n[self.pointidxmap[str(p)]].append(count)
       nfullNormals[self.pointidxmap[str(p)]] = lwsdk.Vector(nfullNormals[self.pointidxmap[str(p)]]) + lwsdk.Vector(mesh_edit_op.polyNormal(mesh_edit_op.state,polys[count])[1])
     count += 1
   return n, nfullNormals
  def process(self, mod_command):

    def polytree(polys, points):
      #here we build a tree on which polys belong to a point.
      #n will store the polyIDs assignement per point
      #nfullNormals will add the poly normals together for that point from the belonging polys
      n = []
      nfullNormals = []
      #create empty arrays
      for p in points:
       n.append([])
       nfullNormals.append([])
      #go through each poly checking with points belong to it.
      count = 0
      for poly in polys:
        pts = mesh_edit_op.polyPoints(mesh_edit_op.state,poly)
        for p in pts:
          n[self.pointidxmap[str(p)]].append(count)
          nfullNormals[self.pointidxmap[str(p)]] = lwsdk.Vector(nfullNormals[self.pointidxmap[str(p)]]) + lwsdk.Vector(mesh_edit_op.polyNormal(mesh_edit_op.state,polys[count])[1])
        count += 1
      return n, nfullNormals

    #deselect any Morph Targets
    command = mod_command.lookup(mod_command.data, "SELECTVMAP")
    cs_options = lwsdk.marshall_dynavalues(("MORF"))
    result = mod_command.execute(mod_command.data, command, cs_options, lwsdk.OPSEL_USER)
    #The temporary filename where it resides, typically this is the systems temp folder as it will resolve to the same on every system
    file = tempfile.gettempdir() + os.sep + "ODVertexData.txt"

    #find existing Vmaps
    loaded_weight = []; loaded_uv = []; loaded_morph = []
    for u in range(0, lwsdk.LWObjectFuncs().numVMaps( lwsdk.LWVMAP_WGHT )):
      loaded_weight.append(lwsdk.LWObjectFuncs().vmapName(lwsdk.LWVMAP_WGHT, u))
    for u in range(0, lwsdk.LWObjectFuncs().numVMaps( lwsdk.LWVMAP_TXUV )):
      loaded_uv.append(lwsdk.LWObjectFuncs().vmapName(lwsdk.LWVMAP_TXUV, u))
    for u in range(0, lwsdk.LWObjectFuncs().numVMaps( lwsdk.LWVMAP_MORF )):
      loaded_morph.append(lwsdk.LWObjectFuncs().vmapName(lwsdk.LWVMAP_MORF, u))

    #start mesh edit operations
    mesh_edit_op = mod_command.editBegin(0, 0, lwsdk.OPLYR_FG)
    if not mesh_edit_op:
      print >>sys.stderr, 'Failed to engage mesh edit operations!'
      return lwsdk.AFUNC_OK

    try:
      # Query all points
      points = []
      edit_op_result = mesh_edit_op.fastPointScan(mesh_edit_op.state, self.fast_point_scan, (points,), lwsdk.OPLYR_FG, 0)
      if edit_op_result != lwsdk.EDERR_NONE:
        mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)
        return lwsdk.AFUNC_OK
      point_count = len(points)
      edit_op_result = lwsdk.EDERR_NONE

      # Query all polygons
      polys = []
      edit_op_result = mesh_edit_op.fastPolyScan(mesh_edit_op.state, self.fast_poly_scan, (polys,), lwsdk.OPLYR_FG, 0)
      if edit_op_result != lwsdk.EDERR_NONE:
        mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)
        return lwsdk.AFUNC_OK
      poly_count = len(polys)
      edit_op_result = lwsdk.EDERR_NONE

      #if there's no points, then we dont need to do anything
      if point_count == 0:
        lwsdk.LWMessageFuncs().info("No Points.", "")
        return lwsdk.AFUNC_OK

      #initializing some variables we'll need
      positions = []
      uvMaps = []
      weightMaps = []
      morphMaps = []
      vertexNormals = []

      #open the file and start writing points header and point positions
      f = open(file, "w")
      f.write ("VERTICES:" + str(point_count) + "\n")

      # Writing point positions for each point
      for point in points:
        pos = mesh_edit_op.pointPos(mesh_edit_op.state, point)
        f.write(str(pos[0]) + " " + str(pos[1]) + " " + str(pos[2]*-1) + "\n")

      #check to see if any surfaces have smoothing on:
      smoothing = 0
      surfIDs = lwsdk.LWSurfaceFuncs().byObject(lwsdk.LWStateQueryFuncs().object())
      for surf in surfIDs:
        smooth = lwsdk.LWSurfaceFuncs().getFlt(surf, lwsdk.SURF_SMAN)
        if smooth > 0:
          smoothing = 1
          break

      #Query which polygons belong to a point and build an array for easy lookup (only needed if there's any smoothing)
      if smoothing > 0:
        ptree = polytree(polys, points)

      #write Polygon Header
      f.write("POLYGONS:" + str(len(polys)) + "\n")
      x =0
      for poly in polys:
        #check if the surface of a poly has smoothing enabled or not so that we either export smoothed or nonsmoothed normals
        surf = mesh_edit_op.polySurface(mesh_edit_op.state,poly)
        surfID = lwsdk.LWSurfaceFuncs().byName(surf, lwsdk.LWStateQueryFuncs().object())
        smoothing = lwsdk.LWSurfaceFuncs().getFlt(surfID[0], lwsdk.SURF_SMAN)
        #Write poly construction with surface name and type, as well as storing the normals
        ppoint = ""
        for point in reversed(mesh_edit_op.polyPoints(mesh_edit_op.state,poly)):
          ppoint += "," + str(self.pointidxmap[str(point)])
          if smoothing > 0:
            vertexNormals.append(lwsdk.Vector().normalize(ptree[1][self.pointidxmap[str(point)]]/float(len(ptree[0]))))
          else:
            vertexNormals.append(mesh_edit_op.polyNormal(mesh_edit_op.state,poly)[1])
        polytype = "FACE"
        subD = mesh_edit_op.polyType(mesh_edit_op.state, poly)# & lwsdk.LWPOLTYPE_SUBD
        if subD == lwsdk.LWPOLTYPE_SUBD:
          polytype = "CCSS"
        elif subD == lwsdk.LWPOLTYPE_PTCH:
          polytype = "SUBD"
        f.write(ppoint[1:] + ";;" + surf + ";;" + polytype + "\n")
      #grab all weights
      for weight in loaded_weight:
        mesh_edit_op.vMapSelect(mesh_edit_op.state, weight, lwsdk.LWVMAP_WGHT, 1)
        f.write("WEIGHT:" + weight + "\n")
        for point in points:
          if (mesh_edit_op.pointVGet(mesh_edit_op.state,point)[1]) != None:
            f.write(str(mesh_edit_op.pointVGet(mesh_edit_op.state,point)[1]) + "\n")
          else:
            f.write("0.0\n")
      #grab all UVs
      for uvs in loaded_uv:
        cont = []
        discont = []
        c = 0
        #selecting uv map
        mesh_edit_op.vMapSelect(mesh_edit_op.state, uvs, lwsdk.LWVMAP_TXUV, 2)
        #check whether we are dealing with continuous or discontinous UVs, we have to look at points per poly for this
        for poly in polys:
          for point in mesh_edit_op.polyPoints(mesh_edit_op.state,poly):
            #vpget gets uv coordinates based on point in poly, if that has a value, the uv is discontinuous.. if it doesnt, its continuous.
            pInfo = mesh_edit_op.pointVPGet(mesh_edit_op.state,point, poly)[1]
            if pInfo != None: #check if discontinous
              curPos = [pInfo[0], pInfo[1]]
              #print "oh:", self.polyidxmap[str(poly)]
              discont.append([curPos, str(self.polyidxmap[str(poly)]), str(self.pointidxmap[str(point)])])
              #discont.append([curPos, str(1), str(self.pointidxmap[str(point)])])
              c+= 1
            else: #otherwise, the uv coordinate is continuous
              if mesh_edit_op.pointVGet(mesh_edit_op.state,point)[1] != None:
                curPos = [mesh_edit_op.pointVGet(mesh_edit_op.state,point)[1][0], mesh_edit_op.pointVGet(mesh_edit_op.state, point)[1][1]]
                cont.append([curPos, str(self.pointidxmap[str(point)])])
                c+= 1
        #Write UVs
        f.write("UV:" + uvs + ":"+str(c) + "\n")
        for uvpos in discont:
          f.write(str(uvpos[0][0]) + " " + str(uvpos[0][1]) + ":PLY:" + str(uvpos[1]) + ":PNT:" + str(uvpos[2]) + "\n")
        for uvpos in cont:
          f.write(str(uvpos[0][0]) + " " + str(uvpos[0][1]) + ":PNT:" + str(uvpos[1]) + "\n")

      #grab all Morphs
      for morph in loaded_morph:
        mesh_edit_op.vMapSelect(mesh_edit_op.state, morph, lwsdk.LWVMAP_MORF, 3)
        f.write("MORPH:" + morph + "\n")
        for point in points:
          if (mesh_edit_op.pointVGet(mesh_edit_op.state,point)[1]) != None:
            ms = mesh_edit_op.pointVGet(mesh_edit_op.state,point)[1]
            f.write(str(ms[0]) + " " + str(ms[1]) + " " + str(ms[2]*-1) + "\n")
          else:
            f.write("0 0 0\n")

      #Write Vertex Normals
      f.write("VERTEXNORMALS:" + str(len(vertexNormals)) + "\n")
      for normal in vertexNormals:
        f.write(str(normal[0]) + " " + str(normal[1]) + " " + str(normal[2]*-1) + "\n")

    except:
      edit_op_result = lwsdk.EDERR_USERABORT
      raise
    finally:
      mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)

    f.close()

    return lwsdk.AFUNC_OK
Exemplo n.º 7
0
    def process(self, mod_command):
        ui = lwsdk.LWPanels()
        panel = ui.create('Random Point Distribution')

        c1 = panel.int_ctl('Number of points')
        c2 = panel.fvec_ctl('Radius')
        c3 = panel.hchoice_ctl('Shape', ['Round', 'Square'])
        c4 = panel.hchoice_ctl('Falloff towards', ['Center', 'Edges'])
        c5 = panel.int_ctl('Steepness')
        c6 = panel.hchoice_ctl('Density distribution', ['Linear', 'Exponential', 'Constant'])
        c7 = panel.str_ctl('Surface', 50)

        panel.align_controls_vertical([c1, c2, c3, c4, c5, c6, c7])
        panel.size_to_layout(5, 5)

        c1.set_int(self._num_points)
        c2.setv_fvec(self._radius)
        c3.set_int(self._shape)
        c4.set_int(self._falloff)
        c5.set_int(self._steepness)
        c6.set_int(self._density)
        c7.set_str(self._surface)

        if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0:
            ui.destroy(panel)
            return lwsdk.AFUNC_OK

        self._num_points = c1.get_int()
        self._radius     = c2.getv_fvec()
        self._shape      = c3.get_int()
        self._falloff    = c4.get_int()
        self._steepness  = c5.get_int()
        self._density    = c6.get_int()
        self._surface    = c7.get_str()

        ui.destroy(panel)

        rmax = (self._radius[0] + self._radius[1] + self._radius[2]) / 3.0

        mesh_edit_op = mod_command.editBegin(0, 0, lwsdk.OPSEL_USER)
        if not mesh_edit_op:
            print >>sys.stderr, 'Failed to engage mesh edit operations!'
            return lwsdk.AFUNC_OK

        monitor_funcs = lwsdk.DynaMonitorFuncs()
        dyna_monitor = monitor_funcs.create("Random Point Distribution", "Generating points...")
        if dyna_monitor:
            dyna_monitor.init(dyna_monitor.data, self._num_points)

        edit_op_result = lwsdk.EDERR_NONE

        random.seed()

        # catch exceptions to make sure Modeler ends up on a sane state
        try:
            i = 0
            while i < self._num_points:
                if self._shape == ptspread.ROUND:
                    point = lwsdk.Vector( (2.0 * random.random() - 1.0) * self._radius[0],
                                          (2.0 * random.random() - 1.0) * self._radius[1],
                                          (2.0 * random.random() - 1.0) * self._radius[2] )

                    r = abs(lwsdk.Vector.magnitude(point))

                    p = self.point_prob(self._density + self._falloff, r, rmax, self._steepness)

                    if random.random() < p:
                        pid = mesh_edit_op.addPoint(mesh_edit_op.state, point)
                        mesh_edit_op.addPoly(mesh_edit_op.state, lwsdk.LWPOLTYPE_FACE, None, self._surface, [pid])

                        i += 1

                        if dyna_monitor:
                            if dyna_monitor.step(dyna_monitor.data, 1):
                                edit_op_result = lwsdk.EDERR_USERABORT
                                break

                else:   # SQUARE

                    point = lwsdk.Vector()
                    point.x = (2.0 * random.random() - 1.0) * self._radius[0]
                    p = self.point_prob(self._density + self._falloff, point.x, rmax, self._steepness)

                    point.y = (2.0 * random.random() - 1.0) * self._radius[1]
                    p += self.point_prob(self._density + self._falloff, point.y, rmax, self._steepness)

                    point.z = (2.0 * random.random() - 1.0) * self._radius[2]
                    p += self.point_prob(self._density + self._falloff, point.z, rmax, self._steepness)

                    p /= 3.0

                    if random.random() < p:
                        pid = mesh_edit_op.addPoint(mesh_edit_op.state, point)
                        mesh_edit_op.addPoly(mesh_edit_op.state, lwsdk.LWPOLTYPE_FACE, None, self._surface, [pid])

                        i += 1

                        if dyna_monitor:
                            if dyna_monitor.step(dyna_monitor.data, 1):
                                edit_op_result = lwsdk.EDERR_USERABORT
                                break
        except:
            edit_op_result = lwsdk.EDERR_USERABORT
            raise
        finally:
            mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)

        if dyna_monitor:
            dyna_monitor.done(dyna_monitor.data)
            monitor_funcs.destroy(dyna_monitor)

        return lwsdk.AFUNC_OK
Exemplo n.º 8
0
import math
import lwsdk

__author__ = "Bob Hood"
__date__ = "Sep 12th 2011"
__copyright__ = "Copyright (C) 2011 NewTek, Inc."
__version__ = "1.0"
__maintainer__ = "Bob Hood"
__email__ = "*****@*****.**"
__status__ = "Example"
__lwver__ = "11"

# values shared by all instances (these should be per-instance,
# and modifiable by a GUI)

color = lwsdk.Vector(0.0, 1.0, 0.0)
center = lwsdk.Vector(0.0, 0.0, 0.0)
radius = 1.5
softness = 0.5


class blotch(lwsdk.IShader):
    def __init__(self, context):
        super(blotch, self).__init__()
        self._surface = context  # LWSurfaceID

        self._r2 = radius * radius
        self._piOverR = math.pi / radius

    # LWShader --------------------------------------------
    def flags(self):
Exemplo n.º 9
0
    def process(self, mod_command):
        ui = lwsdk.LWPanels()
        panel = ui.create('Bubbles')

        c1 = panel.hchoice_ctl('Sphere Type', ['Globe', 'Tesselated'])
        c2 = panel.int_ctl('Tesselation Level')
        c3 = panel.dist_ctl('Maximum Radius')
        c4 = panel.dist_ctl('Minimum Radius')
        c5 = panel.int_ctl('Globe Sides')
        c6 = panel.int_ctl('Globe Segments')
        c7 = panel.str_ctl('Surface', 50)

        panel.align_controls_vertical([c1, c2, c3, c4, c5, c6, c7])
        panel.size_to_layout(5, 5)

        c1.set_int(self._sphereType)
        c2.set_int(self._tessLevel)
        c3.set_float(self._maxRadius)
        c4.set_float(self._minRadius)
        c5.set_int(self._globeSides)
        c6.set_int(self._globeSegments)
        c7.set_str(self._surface)

        if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0:
            ui.destroy(panel)
            return lwsdk.AFUNC_OK

        self._sphereType = c1.get_int()
        self._tessLevel = c2.get_int()
        self._maxRadius = c3.get_float()
        self._minRadius = c4.get_float()
        self._globeSides = c5.get_int()
        self._globeSegments = c6.get_int()
        self._surface = c7.get_str()

        ui.destroy(panel)

        cs_dict = self.get_commands(mod_command)

        random.seed()

        cs_options = lwsdk.marshall_dynavalues((self._surface, ))
        result, dyna_value = mod_command.execute(mod_command.data,
                                                 cs_dict["SURFACE"],
                                                 cs_options, lwsdk.OPSEL_USER)

        mesh_edit_op = mod_command.editBegin(0, 0, lwsdk.OPSEL_USER)
        if not mesh_edit_op:
            print >> sys.stderr, 'Failed to engage mesh edit operations!'
            return lwsdk.AFUNC_OK

        # gather up the point ids

        points = []

        # NOTE: we will be passing in a sequence (list or tuple) as the argument to the callback.
        # internally, Python expects a sequence as an argument to the function, so it MUST be encased
        # in another sequence, or it will be mistaken for a sequence containing the arguments
        # (instead of an argument itself).

        edit_op_result = mesh_edit_op.fastPointScan(mesh_edit_op.state,
                                                    self.fast_point_scan,
                                                    (points, ), lwsdk.OPLYR_FG,
                                                    1)
        if edit_op_result != lwsdk.EDERR_NONE:
            mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)
            return lwsdk.AFUNC_OK

        point_count = len(points)

        # print some info to the PCore console
        print '%d points found for lwsdk.OPLYR_FG' % point_count

        edit_op_result = lwsdk.EDERR_NONE

        monitor_funcs = lwsdk.DynaMonitorFuncs()
        dyna_monitor = monitor_funcs.create("Bubbles", "Removing points...")
        if dyna_monitor:
            dyna_monitor.init(dyna_monitor.data, point_count)

        positions = []
        # catch exceptions to make sure Modeler ends up on a sane state
        try:
            for point in points:
                print type(point)
                positions.append(
                    lwsdk.Vector(
                        mesh_edit_op.pointPos(mesh_edit_op.state, point)))
                mesh_edit_op.remPoint(mesh_edit_op.state, point)

                if dyna_monitor:
                    if dyna_monitor.step(dyna_monitor.data, 1):
                        edit_op_result = lwsdk.EDERR_USERABORT
                        break
        except:
            edit_op_result = lwsdk.EDERR_USERABORT
            raise
        finally:
            mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0)

        if dyna_monitor:
            dyna_monitor.done(dyna_monitor.data)
            monitor_funcs.destroy(dyna_monitor)

        dyna_monitor = monitor_funcs.create("Bubbles", "Creating bubbles...")
        if dyna_monitor:
            dyna_monitor.init(dyna_monitor.data, point_count)

        for position in positions:
            percent = random.random()
            radius = lwsdk.Vector(self._minRadius + (
                (self._maxRadius - self._minRadius) * percent))

            if self._sphereType == bubbles.GLOBE:
                cs_options = lwsdk.marshall_dynavalues(
                    (radius, self._globeSides, self._globeSegments, position))
                result, dyna_value = mod_command.execute(
                    mod_command.data, cs_dict["MAKEBALL"], cs_options,
                    lwsdk.OPSEL_USER)
            else:
                cs_options = lwsdk.marshall_dynavalues(
                    (radius, self._tessLevel, position))
                result, dyna_value = mod_command.execute(
                    mod_command.data, cs_dict["MAKETESBALL"], cs_options,
                    lwsdk.OPSEL_USER)

            if dyna_monitor:
                if dyna_monitor.step(dyna_monitor.data, 1):
                    edit_op_result = lwsdk.EDERR_USERABORT
                    break

        if dyna_monitor:
            dyna_monitor.done(dyna_monitor.data)
            monitor_funcs.destroy(dyna_monitor)

        return lwsdk.AFUNC_OK