def process(self, mod_command): ui = lwsdk.LWPanels() panel = ui.create('Subtract Weight') c = panel.listbox_ctl('Base Map', 300, 16, self.name_items, self.count_items) panel.align_controls_vertical([c]) panel.size_to_layout(1, 1) if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0: ui.destroy(panel) return lwsdk.AFUNC_OK self._base = lwsdk.LWObjectFuncs().vmapName(lwsdk.LWVMAP_WGHT, c.get_int()) print('listed base weight: %s' % self._base) ui.destroy(panel) selname, selid = lwsdk.LWStateQueryFuncs().vmap(lwsdk.LWM_VMAP_WEIGHT) self._target = selname print('selected weight map id: %d' % selid) print('selected weight map name: %s' % selname) op = mod_command.editBegin(0, 0, lwsdk.OPSEL_USER) if not op: print >> sys.stderr, 'failed to editBegin' return lwsdk.AFUNC_OK result = lwsdk.EDERR_NONE try: selpoints = [] result = op.fastPointScan(op.state, self.fast_point_scan, (selpoints, ), lwsdk.OPLYR_FG, 1) if result != lwsdk.EDERR_NONE: op.done(op.state, result, 0) return lwsdk.AFUNC_OK print('selected points: %d' % len(selpoints)) #selpoints = op.genPoints(op.state, lwsdk.OPLYR_FG, 1) for p in selpoints: # get base value op.vMapSelect(op.state, self._base, lwsdk.LWVMAP_WGHT, 1) b = op.pointVGet(op.state, p)[1] print('pt:%d' % p) if (b != None): print('w(B):%f' % b) # get target value op.vMapSelect(op.state, self._target, lwsdk.LWVMAP_WGHT, 1) v = op.pointVGet(op.state, p)[1] if (v != None): print('w(A):%f' % v) if (b != None): v = 1.0 - b else: v = 1.0 op.pntVMap(op.state, p, lwsdk.LWVMAP_WGHT, self._target, [v]) except: result = lwsdk.EDERR_USERABORT raise finally: op.done(op.state, result, 0) return lwsdk.AFUNC_OK
def process(self, mod_command): state_query = lwsdk.LWStateQueryFuncs() selected_bg_layers_str_list = state_query.layerList( lwsdk.OPLYR_BG, None).split() if len(selected_bg_layers_str_list) > 1: self.show_error_dialog('Invalid layer selection', 'Select only one bg layer.') return lwsdk.AFUNC_OK data_layers_str = state_query.layerList(lwsdk.OPLYR_NONEMPTY, None).split() data_layers = [int(n) for n in data_layers_str] data_layers.sort() data_layers.reverse() selected_bg_layer = int(selected_bg_layers_str_list[0]) selected_fg_layers_str = state_query.layerList(lwsdk.OPLYR_FG, None) selected_fg_layers_str_list = selected_fg_layers_str.split() selected_fg_layers_int_list = [ int(n) for n in selected_fg_layers_str_list ] for data_layer in data_layers: if selected_bg_layer > data_layer and not data_layer in selected_fg_layers_int_list: self.set_layers(mod_command, "SETALAYER", selected_fg_layers_str) self.set_layers(mod_command, "SETBLAYER", str(data_layer)) return lwsdk.AFUNC_OK return lwsdk.AFUNC_OK
def process(self, mod): state_query = lwsdk.LWStateQueryFuncs() selected_fg_layers_str = state_query.layerList(lwsdk.OPLYR_FG, None) selected_bg_layers_str = state_query.layerList(lwsdk.OPLYR_BG, None) selected_bg_layers_str_list = selected_bg_layers_str.split() if len(selected_bg_layers_str_list) != 1: self.show_error_dialog('Invalid layer selection', 'Select only one bg layer') mod.execute(mod.data, mod.lookup(mod.data, 'CUT'), None, lwsdk.OPSEL_USER) self.swap_layers(mod, selected_bg_layers_str, selected_fg_layers_str) mod.execute(mod.data, mod.lookup(mod.data, 'PASTE'), None, lwsdk.OPSEL_USER) self.swap_layers(mod, selected_fg_layers_str, selected_bg_layers_str) return lwsdk.AFUNC_OK
def select_layers(self, mod, data): obj_funcs = lwsdk.LWObjectFuncs() state_query = lwsdk.LWStateQueryFuncs() obj_name = state_query.object() layer_list = state_query.layerList(lwsdk.OPLYR_NONEMPTY, obj_name) # there is no mesh ! if layer_list == '': message_funcs = lwsdk.LWMessageFuncs() message_funcs.error('No mesh data', '') return lwsdk.AFUNC_OK current_obj = obj_funcs.focusObject() layers = layer_list.split(' ') foreground_layers = [] background_layers = [] for layer in layers: layer_int = int(layer) - 1 # layer name is (unnamed), display None layer_name = obj_funcs.layerName(current_obj, layer_int) if layer_name == None: layer_name = '' if data.select_contains == (0 if layer_name.find(data.string) < 0 else 1): foreground_layers.append(layer) else: background_layers.append(layer) if len(foreground_layers) == 0: raise NoForegroundLayer() if len(foreground_layers) > 0: cs_options = lwsdk.marshall_dynavalues(' '.join(foreground_layers)) cs_setlayer = mod.lookup(mod.data, "SETALAYER") mod.execute(mod.data, cs_setlayer, cs_options, lwsdk.OPSEL_USER) if len(background_layers) > 0 and data.select_others: cs_options = lwsdk.marshall_dynavalues(' '.join(background_layers)) cs_setlayer = mod.lookup(mod.data, "setblayer") mod.execute(mod.data, cs_setlayer, cs_options, lwsdk.OPSEL_USER)
def selectLayers(self, data): obj_funcs = lwsdk.LWObjectFuncs() state_query = lwsdk.LWStateQueryFuncs() obj_name = state_query.object() layer_list = state_query.layerList(lwsdk.OPLYR_NONEMPTY, obj_name) # there is no mesh ! if layer_list == '': message_funcs = lwsdk.LWMessageFuncs() message_funcs.error('No mesh data', '') return lwsdk.AFUNC_OK current_obj = obj_funcs.focusObject() layers = layer_list.split(' ') foreground_layers = [] background_layers = [] for layer in layers: layer_int = int(layer) - 1 # layer name is (unnamed), display None layer_name = obj_funcs.layerName(current_obj, layer_int) if layer_name == None: layer_name = '' if data.select_contains == ( False if layer_name.find(data.string) < 0 else True): foreground_layers.append(layer) else: background_layers.append(layer) print('foreground_layers') print(foreground_layers) print('background_layers') print(background_layers)
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
def process(self, mod_command): ui = lwsdk.LWPanels() panel = ui.create('Gears') c1 = panel.hchoice_ctl('Axis', ['X', 'Y', 'Z']) c2 = panel.int_ctl('Number of Teeth') c3 = panel.dist_ctl('Inner Radius') c4 = panel.dist_ctl('Outer Radius') c5 = panel.dist_ctl('Thickness') c6 = panel.hchoice_ctl('Gear Type', ['Angular', 'Smooth']) c7 = panel.fvec_ctl('Center') panel.align_controls_vertical([c1, c2, c3, c4, c5, c6, c7]) panel.size_to_layout(5, 5) c1.set_int(self._axis) c2.set_int(self._teeth) c3.set_float(self._rad_inner) c4.set_float(self._rad_outer) c5.set_float(self._thickness) c6.set_int(self._geartype) c7.setv_fvec(self._center) if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0: ui.destroy(panel) return lwsdk.AFUNC_OK self._axis = c1.get_int() self._teeth = c2.get_int() self._rad_inner = c3.get_float() self._rad_outer = c4.get_float() self._thickness = c5.get_float() self._geartype = c6.get_int() self._center = c7.getv_fvec() ui.destroy(panel) cs_dict = self.get_commands(mod_command) cx = self._center[0] cy = self._center[1] cz = self._center[2] - (self._thickness / 2) t_ang = 360 / self._teeth / 57.2957794 # figure out the layers we need to use and those that are # available for our temporary work fg_layers = lwsdk.LWStateQueryFuncs().layerList(lwsdk.OPLYR_FG, None) fg_layers_list = fg_layers.split() empty_layers = lwsdk.LWStateQueryFuncs().layerList( lwsdk.OPLYR_EMPTY, None) empty_layers_list = empty_layers.split() cs_options = lwsdk.marshall_dynavalues(fg_layers_list[0]) result = mod_command.execute(mod_command.data, cs_dict["SETLAYER"], cs_options, lwsdk.OPSEL_USER) x = 0 empty_layer = empty_layers_list[x] while x < len(empty_layers_list) and (empty_layer in fg_layers_list): x += 1 empty_layer = empty_layers_list[x] # only punch a hole if we can get an empty layer to work in if x == len(empty_layers_list): print >> sys.stderr, "Cannot locate an empty background layer!" return lwsdk.AFUNC_OK 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("Gears", "Generating gear...") if dyna_monitor: dyna_monitor.init(dyna_monitor.data, self._teeth) edit_op_result = lwsdk.EDERR_NONE ptID = [] cancelled = False # catch exceptions to make sure Modeler ends up on a sane state try: tooth = 0 for tooth in range(self._teeth): a1 = t_ang * tooth a2 = a1 + (t_ang * 3 / 6) a3 = a1 + (t_ang * 4 / 6) a4 = a1 + (t_ang * 5 / 6) pt = [(self._rad_inner * math.sin(a1) + cx), (self._rad_inner * math.cos(a1) + cy), cz] ptID.append(mesh_edit_op.addPoint(mesh_edit_op.state, pt)) pt = [(self._rad_inner * math.sin(a2) + cx), (self._rad_inner * math.cos(a2) + cy), cz] ptID.append(mesh_edit_op.addPoint(mesh_edit_op.state, pt)) pt = [(self._rad_outer * math.sin(a3) + cx), (self._rad_outer * math.cos(a3) + cy), cz] ptID.append(mesh_edit_op.addPoint(mesh_edit_op.state, pt)) pt = [(self._rad_outer * math.sin(a4) + cx), (self._rad_outer * math.cos(a4) + cy), cz] ptID.append(mesh_edit_op.addPoint(mesh_edit_op.state, pt)) if dyna_monitor: result = dyna_monitor.step(dyna_monitor.data, 1) if result: cancelled = True break if cancelled: edit_op_result = lwsdk.EDERR_USERABORT else: if self._geartype == gears.ANGULAR: mesh_edit_op.addPoly(mesh_edit_op.state, lwsdk.LWPOLTYPE_FACE, None, "Gear", ptID) else: # Smooth ptID.append(ptID[0]) # close the loop mesh_edit_op.addCurve(mesh_edit_op.state, None, ptID, 0) 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) if cancelled: return lwsdk.AFUNC_OK if self._geartype == gears.SMOOTH: result = mod_command.execute(mod_command.data, cs_dict["FREEZECURVES"], None, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues(('Z', self._thickness)) result = mod_command.execute(mod_command.data, cs_dict["EXTRUDE"], cs_options, lwsdk.OPSEL_USER) # make hole with a diameter 25% of the gear's radius cs_options = lwsdk.marshall_dynavalues(empty_layer) result = mod_command.execute(mod_command.data, cs_dict["SETLAYER"], cs_options, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues(( [ self._rad_outer * 0.25, # radius self._rad_outer * 0.25, self._rad_outer * 0.25 ], -0.5, # top self._thickness + 1, # bottom 'Z', # axis 32, # number of sides 1, # number of segments [cx, cy, cz] # center )) result = mod_command.execute(mod_command.data, cs_dict["MAKEDISC"], cs_options, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues(fg_layers_list[0]) result = mod_command.execute(mod_command.data, cs_dict["SETLAYER"], cs_options, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues(empty_layer) result = mod_command.execute(mod_command.data, cs_dict["SETBLAYER"], cs_options, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues('SUBTRACT') result = mod_command.execute(mod_command.data, cs_dict["BOOLEAN"], cs_options, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues(empty_layer) result = mod_command.execute(mod_command.data, cs_dict["SETLAYER"], cs_options, lwsdk.OPSEL_USER) result = mod_command.execute(mod_command.data, cs_dict["DELETE"], None, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues(fg_layers_list[0]) result = mod_command.execute(mod_command.data, cs_dict["SETLAYER"], cs_options, lwsdk.OPSEL_USER) result = mod_command.execute(mod_command.data, cs_dict["MERGEPOINTS"], None, lwsdk.OPSEL_USER) result = mod_command.execute(mod_command.data, cs_dict["ALIGNPOLS"], None, lwsdk.OPSEL_USER) if self._geartype == gears.SMOOTH: result = mod_command.execute(mod_command.data, cs_dict["FLIP"], None, lwsdk.OPSEL_USER) if self._axis != gears.Z: angle = 90.0 if self._axis == gears.X: cs_options = lwsdk.marshall_dynavalues((angle, 'Y')) elif self._axis == gears.Y: cs_options = lwsdk.marshall_dynavalues((angle, 'X')) result = mod_command.execute(mod_command.data, cs_dict["ROTATE"], cs_options, lwsdk.OPSEL_USER) cs_options = lwsdk.marshall_dynavalues(fg_layers) result = mod_command.execute(mod_command.data, cs_dict["SETLAYER"], cs_options, lwsdk.OPSEL_USER) return lwsdk.AFUNC_OK