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 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 select_layers(self, mod_command, history): init(mod_command) objFuncs = lwsdk.LWObjectFuncs() currentObject = objFuncs.focusObject() numberOfLayers = objFuncs.maxLayers(currentObject) foregroundLayers = [] backgroundLayers = [] for i in range(numberOfLayers): name = objFuncs.layerName(currentObject, i) if name != None: if name.find(history.string) != -1: foregroundLayers.append(str(i + 1)) else: backgroundLayers.append(str(i + 1)) else: backgroundLayers.append(str(i + 1)) lyrsetfg(' '.join(foregroundLayers)) lyrsetbg(' '.join(backgroundLayers))
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): #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) 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)) 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: # Getting Point ID of selected Point 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 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 f = open(file, "w") f.write ("VERTICES:" + str(point_count) + "\n") positions = [] uvMaps = [] weightMaps = [] morphMaps = [] # Filling Position Array for Selected 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") #write polygons-point connection for poly reconstruction f.write("POLYGONS:" + str(len(polys)) + "\n") x =0 for poly in polys: surf = mesh_edit_op.polySurface(mesh_edit_op.state,poly) ppoint = "" for point in reversed(mesh_edit_op.polyPoints(mesh_edit_op.state,poly)): ppoint += "," + str(self.pointidxmap[str(point)]) 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 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") 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): #get the command arguments (so that we can also run this from layout) cmd = mod_command.argument.replace('"', '') file = tempfile.gettempdir() + os.sep + "ODVertexData.txt" #open the temp file if os.path.exists(file): f = open(file) lines = f.readlines() f.close() else: lwsdk.LWMessageFuncs().info( "Storage File does not exist. Needs to be created via the Layout CopyTransform counterpart", "") return 0 #get the pointcount from the file pntCount = int(lines[0].split(":")[1].strip()) #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)) #check if we are in modeler, if so, clear polys # if cmd == "": # #Remove Current mesh from layer # command = mod_command.lookup(mod_command.data, "CUT") # result = mod_command.execute(mod_command.data, command, None, lwsdk.OPLYR_FG) edit_op_result = lwsdk.EDERR_NONE 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 try: vertline = [] polyline = [] uvMaps = [] morphMaps = [] weightMaps = [] count = 0 #Parse File to see what Data we have for line in lines: if line.startswith("VERTICES:"): vertline.append( [int(line.strip().split(":")[1].strip()), count]) if line.startswith("POLYGONS:"): polyline.append( [int(line.strip().split(":")[1].strip()), count]) if line.startswith("UV:"): uvMaps.append( [line.strip().split(":")[1:], count] ) # changed this to add the # of uv coordinates into the mix if line.startswith("MORPH"): morphMaps.append([line.split(":")[1].strip(), count]) if line.startswith("WEIGHT"): weightMaps.append([line.split(":")[1].strip(), count]) count += 1 #create Points for verts in vertline: points = [] for i in xrange(verts[1] + 1, verts[1] + verts[0] + 1): x = lines[i].split(" ") pt = [float(x[0]), float(x[1]), float(x[2].strip()) * -1] points.append(mesh_edit_op.addPoint( mesh_edit_op.state, pt)) #create Polygons for polygons in polyline: polys = [] for i in xrange(polygons[1] + 1, polygons[1] + polygons[0] + 1): pts = [] surf = (lines[i].split(";;")[1]).strip() polytype = (lines[i].split(";;")[2]).strip() for x in (lines[i].split(";;")[0]).strip().split(","): pts.insert(0, (points[int(x.strip())])) ptype = lwsdk.LWPOLTYPE_FACE if polytype == "CCSS": ptype = lwsdk.LWPOLTYPE_SUBD elif polytype == "SUBD": ptype = lwsdk.LWPOLTYPE_PTCH polys.append( mesh_edit_op.addPoly(mesh_edit_op.state, ptype, None, surf, pts)) #setup weightmaps for weightMap in weightMaps: mesh_edit_op.vMapSelect(mesh_edit_op.state, weightMap[0], lwsdk.LWVMAP_WGHT, 1) count = 0 for point in points: if lines[weightMap[1] + 1 + count].strip() != "None": mesh_edit_op.pntVMap( mesh_edit_op.state, point, lwsdk.LWVMAP_WGHT, weightMap[0], [float(lines[weightMap[1] + 1 + count].strip())]) count += 1 #Set Morph Map Values for morphMap in morphMaps: mesh_edit_op.vMapSelect(mesh_edit_op.state, morphMap[0], lwsdk.LWVMAP_MORF, 3) count = 0 for point in points: if lines[morphMap[1] + 1 + count].strip() != "None": mesh_edit_op.pntVMap( mesh_edit_op.state, point, lwsdk.LWVMAP_MORF, morphMap[0], [ float(lines[morphMap[1] + 1 + count].split(" ")[0]), float(lines[morphMap[1] + 1 + count].split(" ")[1]), float(lines[morphMap[1] + 1 + count].split(" ")[2]) * -1 ]) count += 1 #Set UV Map Values for uvMap in uvMaps: mesh_edit_op.vMapSelect(mesh_edit_op.state, uvMap[0][0], lwsdk.LWVMAP_TXUV, 2) count = 0 for i in range(int(uvMap[0][1])): line = lines[uvMap[1] + 1 + count] split = line.split(":") #if len(split) > 3: #check the format to see if it has a point and poly classifier, determining with that, whether the uv is discontinuous or continuous #mesh_edit_op.pntVPMap(mesh_edit_op.state, points[int(split[4])], polys[int(split[2])], lwsdk.LWVMAP_TXUV, uvMap[0][0], [float(split[0].split(" ")[0]), float(split[0].split(" ")[1])]) #else: mesh_edit_op.pntVMap(mesh_edit_op.state, points[int(split[2])], lwsdk.LWVMAP_TXUV, uvMap[0][0], [ float(split[0].split(" ")[0]), float(split[0].split(" ")[1]) ]) count += 1 # #remove unused UVMaps # for m in loaded_uv: # if m not in str(uvMaps): # mesh_edit_op.vMapSelect(mesh_edit_op.state, m, lwsdk.LWVMAP_TXUV, 2) # mesh_edit_op.vMapRemove(mesh_edit_op.state) # # #remove unused UVMaps # for m in loaded_weight: # if m not in str(weightMaps): # mesh_edit_op.vMapSelect(mesh_edit_op.state, m, lwsdk.LWVMAP_WGHT, 1) # mesh_edit_op.vMapRemove(mesh_edit_op.state) # # #remove unused UVMaps # for m in loaded_morph: # if m not in str(morphMaps): # mesh_edit_op.vMapSelect(mesh_edit_op.state, m, lwsdk.LWVMAP_MORF, 3) # mesh_edit_op.vMapRemove(mesh_edit_op.state) except: edit_op_result = lwsdk.EDERR_USERABORT raise finally: mesh_edit_op.done(mesh_edit_op.state, edit_op_result, 0) return lwsdk.AFUNC_OK
def process(self, ga): ui = lwsdk.LWPanels() panel = ui.create('Final Fantasy 7:Remake Import') controlWidth = 64 c1 = panel.load_ctl('Select File', controlWidth) c1.set_str(self._filepath) if panel.open(lwsdk.PANF_BLOCKING | lwsdk.PANF_CANCEL) == 0: ui.destroy(panel) return lwsdk.AFUNC_OK self.filepath = c1.get_str() progress_count = 8 t1 = time.time() interface_info = lwsdk.LWInterfaceInfo() if not (interface_info.generalFlags & lwsdk.LWGENF_PARENTINPLACE): ga.evaluate(ga.data, "ParentInPlace") #set bind pose at frame -10 ga.evaluate(ga.data, "GoToFrame -10") interface_info = lwsdk.LWInterfaceInfo() selected_items = interface_info.selected_items() #if a mesh not selected create a parent null for the bones if not selected_items: result = ga.evaluate(ga.data, "AddNull Root") interface_info = lwsdk.LWInterfaceInfo() selected_items = interface_info.selected_items() buildItemID = selected_items[0] else: item_info = lwsdk.LWItemInfo() mytype = item_info.type(selected_items[0]) if mytype == lwsdk.LWI_OBJECT: buildItemID = selected_items[0] else: result = ga.evaluate(ga.data, "AddNull Root") interface_info = lwsdk.LWInterfaceInfo() selected_items = interface_info.selected_items() buildItemID = selected_items[0] #get weightmap count object_functions = lwsdk.LWObjectFuncs() numWeights = object_functions.numVMaps(lwsdk.LWVMAP_WGHT) files = path_wrangler(self.filepath) files.get_files() md = open(files.data['uexp'], 'rb') ua = open(files.data['uasset'], 'rb') meshName = files.data['meshName'] submesh_name = files.data['submesh_name'] arm = False weightData = {} Weight_array = [] vertexArray = [] NA = [] normal_array = [] UVs0 = [] UVs1 = [] UVs2 = [] UVs3 = [] UVs4 = [] faces = [] names = readUasset(ua) ua.close() pattern0 = re.compile( b'\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00........') for x in xrange(20000): s1 = struct.unpack("18s", md.read(18))[0] if pattern0.match(s1): c0 = struct.unpack("<L", md.read(4))[0] c1 = struct.unpack("<L", md.read(4))[0] c2 = struct.unpack("<L", md.read(4))[0] c3 = struct.unpack("<L", md.read(4))[0] c4 = struct.unpack("<L", md.read(4))[0] if (c0 and c1 and c2 and c3 and c4 > 1000000000): break else: md.seek(-20, 1) md.seek(-17, 1) materialCount = struct.unpack("<L", md.read(4))[0] materials = {} for m in xrange(materialCount): materials[m] = {} materials[m]['val0'] = struct.unpack("<l", md.read(4))[0] stringIndex = struct.unpack("<L", md.read(4))[0] unk0 = struct.unpack("<L", md.read(4))[0] unk1 = struct.unpack("<L", md.read(4))[0] unk2 = struct.unpack("<L", md.read(4))[0] unk3 = struct.unpack("<f", md.read(4))[0] unk4 = struct.unpack("<f", md.read(4))[0] unk5 = struct.unpack("<L", md.read(4))[0] unk6 = struct.unpack("<L", md.read(4))[0] materials[m]['name'] = names[stringIndex] boneCount = struct.unpack("<L", md.read(4))[0] joint_data = {} joint_names = [] for i in xrange(boneCount): string_index = struct.unpack("<L", md.read(4))[0] jName = names[string_index] unk = struct.unpack("<L", md.read(4))[0] parent = struct.unpack("<l", md.read(4))[0] joint_data[i] = {"name": jName, "parent": parent} boneCount2 = struct.unpack("<L", md.read(4))[0] progress_count += boneCount2 if not moninit(progress_count, "Importing", title='Progress'): raise Exception('Hell!') boneArray = [] psn = {} IDs = [] for bn in range(boneCount2): boneName = joint_data[bn]["name"] BNparent = joint_data[bn]["parent"] m1 = struct.unpack("<10f", md.read(40)) ps0 = (m1[4], m1[5], m1[6]) #BNps = np.asarray(ps0) rt0_ = (m1[3], m1[0], m1[1], m1[2]) rt0 = quat2mat(rt0_) if BNparent == -1: BNparent = 0 #BNps = [mi[3,1], mi[3,2], mi[3,0]] BNps = ps0 psn[bn] = BNps #BNrt = mat2euler(mi, axes='ryxz') ga.evaluate(ga.data, "SelectItem %s" % lwsdk.itemid_to_str(buildItemID)) layoutCommand = "AddBone " + boneName result = ga.evaluate(ga.data, layoutCommand) # no influence except for weight maps result = ga.evaluate(ga.data, 'BoneStrength 0') interface_info = lwsdk.LWInterfaceInfo() selected_items = interface_info.selected_items() itemID = selected_items[0] IDs.append(itemID) #UpdateMotion needed for parent in place if bn != 0: ga.evaluate(ga.data, 'UpdateMotion') ga.evaluate( ga.data, "ParentItem %s" % lwsdk.itemid_to_str(IDs[BNparent])) layoutCommand = "Position " + str(BNps[0]) + " " + str( BNps[1]) + " " + str(BNps[2]) result = ga.evaluate(ga.data, layoutCommand) if bn == 0: result = ga.evaluate(ga.data, "BoneRestLength 0.5") else: lenX = psn[BNparent][0] - BNps[0] lenY = psn[BNparent][1] - BNps[1] lenZ = psn[BNparent][2] - BNps[2] #dont use full bone length....dummy boneLength = math.sqrt(lenX * lenX + lenY * lenY + lenZ * lenZ) * 0.6 result = ga.evaluate(ga.data, "BoneRestLength " + str(boneLength)) #BNrt = quat2euler(rt0_, axes='szyx') BNrt = quat2euler(rt0_, axes='ryxz') rotx = (180.0 / math.pi) * BNrt[0] roty = (180.0 / math.pi) * BNrt[1] rotz = (180.0 / math.pi) * BNrt[2] layoutCommand = "Rotation " + str(rotx) + " " + str( roty) + " " + str(rotz) result = ga.evaluate(ga.data, layoutCommand) if bn == 0: layoutCommand = "Rotation " + str(180.0) + " " + str( 180.0) + " " + str(0.0) result = ga.evaluate(ga.data, layoutCommand) # if 'autokey' is not turned on, we need to explicitly # create keys for the object at the current time offset if not (interface_info.generalFlags & lwsdk.LWGENF_AUTOKEY): ga.evaluate(ga.data, "CreateKey %f" % interface_info.curTime) #keys created at frame -10 so delete any at frame 0 ga.evaluate(ga.data, 'DeleteKey 0') if (numWeights > 0): if bn != 0: for b in range(numWeights): weightName = object_functions.vmapName( lwsdk.LWVMAP_WGHT, b) if weightName == boneName: layoutCommand = 'BoneWeightMapName ' + boneName result = ga.evaluate(ga.data, layoutCommand) result = ga.evaluate(ga.data, 'BoneWeightMapOnly') ga.evaluate(ga.data, 'RecordRestPosition') monstep() #-----------------------------------------------------------# boneCount3 = struct.unpack("<L", md.read(4))[0] md.seek(boneCount3 * 12, 1) vertexGroups = {} unk0 = struct.unpack("<L", md.read(4))[0] unk1 = struct.unpack("B", md.read(1))[0] unk2 = struct.unpack("B", md.read(1))[0] groupCount = struct.unpack("<L", md.read(4))[0] for m in xrange(groupCount): z1 = struct.unpack("<H", md.read(2))[0] ID = struct.unpack("<H", md.read(2))[0] md.seek(24, 1) vertexGroups[ID] = {'range': 0, 'bones': []} # pragma region bone palette start = struct.unpack("<L", md.read(4))[0] count = struct.unpack("<L", md.read(4))[0] vertexGroups[ID]['bones_np'] = np.zeros((boneCount2, ), dtype=int) bone_names = [] for bn in xrange(count): bid = struct.unpack("<H", md.read(2))[0] vertexGroups[ID]['bones_np'][bn] = bid vertexGroups[ID]['bones'].append(bid) bone_names.append(joint_data[bid]["name"]) # pragma endregion bone palette size = struct.unpack("<L", md.read(4))[0] stop = start + size vertexGroups[ID]['range'] = np.arange(start, stop) vertexGroups[ID]["start"] = start vertexGroups[ID]["stop"] = stop vertexGroups[ID]["size"] = size vertexGroups[ID]["names"] = bone_names md.seek(34, 1) FFx4 = readHexString(md, 4) flag = struct.unpack("<L", md.read(4))[0] if flag: # extra data for this group count = struct.unpack("<L", md.read(4))[0] md.seek(count * 16, 1) else: null = struct.unpack("<L", md.read(4))[0] process_surfaces(files, vertexGroups, materials) #-----------------------------------------------------------# ga.evaluate(ga.data, 'SelectAllBones') #ga.evaluate(ga.data, 'RecordRestPosition') bonemayastyledraw() itemiconscale(0.2) monend() elapsed = time.time() - t1 print "Time: " + str(elapsed) md.close() return lwsdk.AFUNC_OK
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 createMaterial(files, vertexGroups, materials, surf): textures = getMaterialTypes(files) """ textures: { 'NP0002_00_Body': {'NP0002_00_Body_C', 'NP0002_00_Body_A', 'NP0002_00_Body_N'}, 'NP0002_00_Eye': {'NP0002_00_Eye_C'}, 'NP0002_00_Eyelash': {'NP0002_00_Head_N', 'NP0002_00_Head_C'}, 'NP0002_00_Hair': {'NP0002_00_Hair_C', 'NP0002_00_Hair_A', 'NP0002_00_Hair_N'}, 'NP0002_00_Head': {'NP0002_00_Head_N', 'NP0002_00_Head_C'}, 'NP0002_00_Mouth': set(), 'NP0002_00_Skin': {'NP0002_00_Body_C', 'NP0002_00_Body_N'} } """ texture_list = textures[surf] NodeBuild = [] NodeBuild.append("{ Surface\n") NodeBuild.append(" \"Default\"\n") NodeBuild.append(" SmoothingAngle 1.5625\n") NodeBuild.append(" CompatibilityVersion 77b\n") NodeBuild.append(" { Nodes\n") NodeBuild.append(" { Root\n") NodeBuild.append(" Location 0 0\n") NodeBuild.append(" Zoom 1\n") NodeBuild.append(" Disabled 1\n") NodeBuild.append(" }\n") NodeBuild.append(" Version 1\n") NodeBuild.append(" { Nodes\n") NodeBuild.append(" Server \"Surface\"\n") NodeBuild.append(" { Tag\n") NodeBuild.append(" RealName \"Surface\"\n") NodeBuild.append(" Name \"Surface\"\n") NodeBuild.append(" Coordinates -1890 -561\n") NodeBuild.append(" Mode 1\n") NodeBuild.append(" Selected 0\n") NodeBuild.append(" { Data\n") NodeBuild.append(" }\n") NodeBuild.append(" Preview \"\"\n") NodeBuild.append(" }\n") NodeBuild.append(" Server \"Input\"\n") NodeBuild.append(" { Tag\n") NodeBuild.append(" RealName \"Input\"\n") NodeBuild.append(" Name \"Input\"\n") NodeBuild.append(" Coordinates 0 -289\n") NodeBuild.append(" Mode 0\n") NodeBuild.append(" Selected 0\n") NodeBuild.append(" { Data\n") NodeBuild.append(" }\n") NodeBuild.append(" Preview \"Item ID\"\n") NodeBuild.append(" }\n") # Add material node if 'head' in surf.lower() or 'skin' in surf.lower(): NodeBuild.append(" Server \"Skin\"\n") NodeBuild.append(" { Tag\n") NodeBuild.append(" RealName \"Skin\"\n") NodeBuild.append(" Name \"Skin (1)\"\n") NodeBuild.append(" Coordinates -1260 -480\n") ''' NodeBuild.append(" { Data\n") NodeBuild.append(" { Attributes\n") NodeBuild.append(" { Metadata\n") NodeBuild.append(" Version 1\n") NodeBuild.append(" Enumerations 0\n") NodeBuild.append(" { AttributeData\n") #color NodeBuild.append(" { Attr\n") NodeBuild.append(" Name \"Epidermis Color\"\n") NodeBuild.append(" Flags 0\n") NodeBuild.append(" Tag \"ENVELOPE\" \"On\"\n") NodeBuild.append(" Tag \"FORMAT\" \"Color\"\n") NodeBuild.append(" Tag \"NodeInputID\" \"\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" \"vparam3\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" 3\n") NodeBuild.append(" 0.9881 0.9098 0.7685\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") ''' else: NodeBuild.append(" Server \"Principled BSDF\"\n") NodeBuild.append(" { Tag\n") NodeBuild.append(" RealName \"Principled BSDF\"\n") NodeBuild.append(" Name \"Principled BSDF (1)\"\n") NodeBuild.append(" Coordinates -894 -393\n") NodeBuild.append(" { Data\n") NodeBuild.append(" { Attributes\n") NodeBuild.append(" { Metadata\n") NodeBuild.append(" Version 1\n") NodeBuild.append(" Enumerations 0\n") NodeBuild.append(" { AttributeData\n") #color NodeBuild.append(" { Attr\n") NodeBuild.append(" Name \"Color\"\n") NodeBuild.append(" Flags 0\n") NodeBuild.append(" Tag \"ENVELOPE\" \"On\"\n") NodeBuild.append(" Tag \"FORMAT\" \"Percent\"\n") NodeBuild.append(" Tag \"NodeInputID\" \"\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" \"vparam3\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" 3\n") NodeBuild.append(" 0.5 0.5 0.5\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") #specular NodeBuild.append(" { Attr\n") NodeBuild.append(" Name \"Specular\"\n") NodeBuild.append(" Flags 0\n") NodeBuild.append(" Tag \"ENVELOPE\" \"On\"\n") NodeBuild.append(" Tag \"FORMAT\" \"Percent\"\n") NodeBuild.append(" Tag \"NodeInputID\" \"\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" \"vparam\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" 1\n") NodeBuild.append(" 0\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") #roughness NodeBuild.append(" { Attr\n") NodeBuild.append(" Name \"Roughness\"\n") NodeBuild.append(" Flags 0\n") NodeBuild.append(" Tag \"ENVELOPE\" \"On\"\n") NodeBuild.append(" Tag \"FORMAT\" \"Percent\"\n") NodeBuild.append(" Tag \"NodeInputID\" \"\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" \"vparam\"\n") NodeBuild.append(" { Value\n") NodeBuild.append(" 1\n") NodeBuild.append(" 1\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") #close NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" Preview \"Material\"\n") NodeBuild.append(" }\n") for t in texture_list: loc = (0, 0) the_file = "" UVMap = lwsdk.LWObjectFuncs().vmapName(lwsdk.LWVMAP_TXUV, 0) if t[-2:] == '_C': the_file = files.data['tex_dir'] + "\\" + t + ".png" loc = CoordinateList["Color"] Server = " Server \"Image\"\n" RealName = " RealName \"Image\"\n" Name = " Name \"Image (1)\"\n" elif t[-2:] == '_N': the_file = files.data['tex_dir'] + "\\" + t + ".png" loc = CoordinateList["Normal"] Server = " Server \"NormalMap\"\n" RealName = " RealName \"NormalMap\"\n" Name = " Name \"NormalMap (1)\"\n" elif t[-2:] == '_A': the_file = files.data['tex_dir'] + "\\" + t + ".png" loc = CoordinateList["Alpha"] Server = " Server \"Image\"\n" RealName = " RealName \"Image\"\n" Name = " Name \"Image (2)\"\n" elif t[-2:] == '_O': the_file = files.data['tex_dir'] + "\\" + t + ".png" loc = CoordinateList["Opacity"] if UV_count > 1: UVMap = lwsdk.LWObjectFuncs().vmapName(lwsdk.LWVMAP_TXUV, 1) Server = " Server \"Image\"\n" RealName = " RealName \"Image\"\n" Name = " Name \"Image (3)\"\n" NodeBuild.append(Server) NodeBuild.append(" { Tag\n") NodeBuild.append(RealName) NodeBuild.append(Name) NodeBuild.append(" Coordinates " + loc + "\n") NodeBuild.append(" { Data\n") if t[-2:] == '_A': NodeBuild.append(" InvertColor 1\n") NodeBuild.append(" { Image\n") if os.path.exists(the_file): NodeBuild.append(" { Clip\n") NodeBuild.append(" { Still\n") NodeBuild.append(" \"" + the_file + "\"\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" Mapping 5\n") NodeBuild.append(" UV \"" + UVMap + "\"\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append(" }\n") #make them there connections NodeBuild.append(" { Connections\n") NodeBuild.append(" NodeName \"Surface\"\n") NodeBuild.append(" InputName \"Material\"\n") if 'head' in surf.lower() or 'skin' in surf.lower(): NodeBuild.append(" InputNodeName \"Skin (1)\"\n") else: NodeBuild.append(" InputNodeName \"Principled BSDF (1)\"\n") NodeBuild.append(" InputOutputName \"Material\"\n") for t in texture_list: if t[-2:] == '_A': NodeBuild.append(" NodeName \"Surface\"\n") NodeBuild.append(" InputName \"Clip\"\n") NodeBuild.append(" InputNodeName \"Image (2)\"\n") NodeBuild.append(" InputOutputName \"Color\"\n") elif t[-2:] == '_C': if 'head' in surf.lower() or 'skin' in surf.lower(): NodeBuild.append(" NodeName \"Skin (1)\"\n") NodeBuild.append(" InputName \"Epidermis Color\"\n") NodeBuild.append(" InputNodeName \"Image (1)\"\n") NodeBuild.append(" InputOutputName \"Color\"\n") NodeBuild.append(" NodeName \"Skin (1)\"\n") NodeBuild.append(" InputName \"Dermis Color\"\n") NodeBuild.append(" InputNodeName \"Image (1)\"\n") NodeBuild.append(" InputOutputName \"Color\"\n") else: NodeBuild.append(" NodeName \"Principled BSDF (1)\"\n") NodeBuild.append(" InputName \"Color\"\n") NodeBuild.append(" InputNodeName \"Image (1)\"\n") NodeBuild.append(" InputOutputName \"Color\"\n") elif t[-2:] == '_N': if 'head' in surf.lower() or 'skin' in surf.lower(): NodeBuild.append(" NodeName \"Skin (1)\"\n") else: NodeBuild.append(" NodeName \"Principled BSDF (1)\"\n") # NodeBuild.append(" InputName \"Normal\"\n") NodeBuild.append(" InputNodeName \"NormalMap (1)\"\n") NodeBuild.append(" InputOutputName \"Normal\"\n") elif t[-2:] == '_O': NodeBuild.append(" NodeName \"Image (1)\"\n") NodeBuild.append(" InputName \"Opacity\"\n") NodeBuild.append(" InputNodeName \"Image (3)\"\n") NodeBuild.append(" InputOutputName \"Color\"\n") # End connections segment NodeBuild.append(" }\n") NodeBuild.append(" }\n") NodeBuild.append("}\n") # Complete the node build by putting it together NodeBuild = "".join(NodeBuild) f = NamedTemporaryFile(mode='w+', delete=False) f.write(NodeBuild) f.close() lwsdk.command("Surf_LoadText \"" + f.name + "\"") os.unlink(f.name) # delete the file
#PBR Surface Setup by Ryan Roye import lwsdk import os from tempfile import NamedTemporaryFile from utils import * CoordinateList = { "Color": "-600 -220", "Alpha": "-1280 -740", "Normal": "-540 -800", "Opacity": "-280 -100" } UV_count = lwsdk.LWObjectFuncs().numVMaps(lwsdk.LWVMAP_TXUV) def createMaterial(files, vertexGroups, materials, surf): textures = getMaterialTypes(files) """ textures: { 'NP0002_00_Body': {'NP0002_00_Body_C', 'NP0002_00_Body_A', 'NP0002_00_Body_N'}, 'NP0002_00_Eye': {'NP0002_00_Eye_C'}, 'NP0002_00_Eyelash': {'NP0002_00_Head_N', 'NP0002_00_Head_C'}, 'NP0002_00_Hair': {'NP0002_00_Hair_C', 'NP0002_00_Hair_A', 'NP0002_00_Hair_N'}, 'NP0002_00_Head': {'NP0002_00_Head_N', 'NP0002_00_Head_C'}, 'NP0002_00_Mouth': set(), 'NP0002_00_Skin': {'NP0002_00_Body_C', 'NP0002_00_Body_N'} } """
def count_items(self, ctrl, param): return lwsdk.LWObjectFuncs().numVMaps(lwsdk.LWVMAP_WGHT)
def name_items(self, ctrl, param, row): return lwsdk.LWObjectFuncs().vmapName(lwsdk.LWVMAP_WGHT, row)