0583_Delete Operation for Two Strings

该博客介绍了一种使用动态规划方法解决找到使两个单词相同所需的最小字符删除步数问题。通过建立二维数组dp,记录两个字符串的最长公共子序列,并最终返回两个字符串长度与最长子序列长度差的两倍作为答案。示例给出了sea和eat的例子,输出为2。代码展示了具体的实现过程。

0583两个字符串的删除操作

题目描述

给定两个单词 word1word2,找到使得 word1word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。

示例

输入: "sea", "eat"
输出: 2
解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"

题解

方法一:动态规划

  1. 要求删除的做小步数可以转化为求最长的相同子序列,再用两个word的长度-相同子序列的两倍就求得了最小步数

  2. 状态:

    dp[i][j]=以word1[0]-word2[i-1]为子串以及word1[0]-word2[j-1]为子串的最长子序列
    
  3. 转移关系

    如果s[i]==s[j]说明两个字符相等,此时最长子串的长度比两者的都取前一个字符的最长子串多1(注意dp的下标是比word下标大的,因为涉及到i-1避免越界)

    if(s[i-1]==s[j-1])
    	dp[i][j]=dp[i-1][j-1]+1
    

    如果两个字符不相等,那就最长相同子串终止,这个时候的最长子串取两个各往前推一个字符的较大者

    else
       dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    
  4. 最后得到了最终答案

      return len1+len2-2*dp[len1][len2];
    
    

代码

class Solution {
public:
    int minDistance(string word1, string word2) {
        int len1=word1.size();
        int len2=word2.size();
        if(len1==0||len2==0)
            return 0;
        //dp[i][j]表示word1中下标为从0到i-1 以及 word2中下标为0到j-1的最大公共子序列
        vector<vector<int>> dp(len1+1,vector<int>(len2+1,0));

        for(int i=1;i<=len1;i++)
        {
            for(int j=1;j<=len2;j++)
            {
                //两者相等,最长序列+1
                if(word1[i-1]==word2[j-1])
                    dp[i][j]=dp[i-1][j-1]+1;

                //两者不等,最长序列取较长者
                else
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
        //两者的长度减去公共的部分就是要删除的
        return len1+len2-2*dp[len1][len2];

    }
};
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="error__6">Operation failed. Please try later.</string> <string name="error_263">No default tenant found, please contact service provider.</string> <string name="error__107">Failed to connect to the camera. Try again later.</string> <string name="error_004">Invalid email address.</string> <string name="error_006">Incorrect mobile number.</string> <string name="error_009">The phone number has already been registered once, please use another number or use this number to login.</string> <string name="error_010">Incorrect verification code.</string> <string name="error_011">The email address has been bound.</string> <string name="error_012">Failed to log in to the app. Please log in again.</string> <string name="error_018">Invalid application ID.</string> <string name="error_019">Invalid operation.</string> <string name="error_024">Invalid plug-in.</string> <string name="error_025">Operation failed. Please try later.</string> <string name="error_026">Plug-in does not exist.</string> <string name="error_027">Failed to log in to the app. Please log in again.</string> <string name="error_030">The terminal has been bound.</string> <string name="error_031">Operation failed. Please try later.</string> <string name="error_032">The terminal does not bind to this account.</string> <string name="error_033">Terminals are being operated by other users.</string> <string name="error_034">Operation failed. Please try later.</string> <string name="error_035">Failed to obtain the application list.</string> <string name="error_036">Failed to obtain the application image.</string> <string name="error_037">Failed to obtain the application version.</string> <string name="error_038">Failed to obtain the Wi-Fi coverage information.</string> <string name="error_040">The email address has not been bound to an account.</string> <string name="error_042">Operation failed. Please try later.</string> <string name="error_044">Operation failed. Please try later.</string> <string name="error_048">Operation failed. Please try later.</string> <string name="error_050">Operation failed. Please try later.</string> <string name="error_051">Operation failed. Please try later.</string> <string name="error_056">Configuration page query failed.</string> <string name="error_057">Operation failed. Please try later.</string> <string name="error_058">Plug-in list query failed.</string> <string name="error_059">Plug-in image query failed.</string> <string name="error_060">Operation failed. Please try later.</string> <string name="error_061">Operation failed. Please try later.</string> <string name="error_063">Invalid broadband account.</string> <string name="error_064">Operation failed. Please try later.</string> <string name="error_066">Invalid account type.</string> <string name="error_067">The password cannot be empty.</string> <string name="error_068">The confirmation password cannot be empty.</string> <string name="error_069">The account cannot be empty.</string> <string name="error_070">Incorrect PPPoE account.</string> <string name="error_072">The cloud account is empty.</string> <string name="error_073">Incorrect cloud account type.</string> <string name="error_075">The cloud account is registered.</string> <string name="error_076">The cloud account does not exist.</string> <string name="error_077">You have not bound any broadband account or gateway.</string> <string name="error_078">The MAC address of the device is not registered to the cloud platform. </string> <string name="error_081">The current binding relationship does not exist.</string> <string name="error_082">The user account does not exist.</string> <string name="error_087">Binding failed, No broadband account.</string> <string name="error_093">You have not bound any broadband account.</string> <string name="error_094">The number of gateways to be bound has reached the maximum. New gateways cannot be bound.</string> <string name="error_097">Broadband account verification is not currently available.</string> <string name="error_098">The device already exists.</string> <string name="error_099">The Wi-Fi SSID already exists.</string> <string name="error_100">The Wi-Fi SSID is empty.</string> <string name="error_101">The account is added.</string> <string name="error_103">The password will expire soon.</string> <string name="error_105">The user account is locked.</string> <string name="error_106">User login failed.</string> <string name="error_107">Exceeded the number limit of accounts that can be logged in at the same time.</string> <string name="error_108">Password complexity is invalid.</string> <string name="error_110">Incorrect mobile phone type.</string> <string name="error_111">The mobile phone version is empty.</string> <string name="error_112">Exceeded the password resetting limit in 24 hours.</string> <string name="error_113">Exceeded the number limit of security question attempts. The password resetting is locked.</string> <string name="error_114">Incorrect answer to the security question.</string> <string name="error_115">Invalid parameter for the security question.</string> <string name="error_116">Incorrect old password.</string> <string name="error_117">No plug-in version.</string> <string name="error_118">Incorrect password.</string> <string name="error_121">Invalid order number.</string> <string name="error_122">WO cloud account has been registered.</string> <string name="error_123">WO cloud account does not exist.</string> <string name="error_124">WO cloud client authentication failed.</string> <string name="error_133">Failed to join.</string> <string name="error_139">Only administrators have this permission.</string> <string name="error_144">The log file size exceeds 5 MB and cannot be uploaded.</string> <string name="error_145">Plug-in already exists.</string> <string name="error_146">Plug-in version already exists.</string> <string name="error_150">The log switch has been enabled.</string> <string name="error_151">The log switch has been disabled.</string> <string name="error_152">Failed to query third-party authentication information.</string> <string name="error_153">Failed to forward plug-in third-party authentication information.</string> <string name="error_154">Duplicate room name.</string> <string name="error_155">The message template cannot be left empty.</string> <string name="error_156">The message template does not exist.</string> <string name="error_157">The mapping SMS template does not exist.</string> <string name="error_158">The SMS gateway is unavailable.</string> <string name="error_159">The plug-in service name is left empty or does not exist.</string> <string name="error_160">The plug-in service code is left empty or does not exist.</string> <string name="error_161">The plug-in random code is left blank or does not exist.</string> <string name="error_162">The mobile number cannot be left empty.</string> <string name="error_163">Failed to connect to the SMS gateway.</string> <string name="error_164">Failed to bind the SMS gateway.</string> <string name="error_165">Failed to send the SMS message.</string> <string name="error_166">SMS sending timed out.</string> <string name="error_171">The email address for receiving an authentication code is not the email address of the administrator.</string> <string name="error_172">The format of the message notification switch is incorrect.</string> <string name="error_173">The user password of the storage service fails the verification.</string> <string name="error_174">Status of the gateway to which the user is bound is invalid.</string> <string name="error_175">The email server is not configured.</string> <string name="error_176">Failed to send the email.</string> <string name="error_184">Sender authentication on the email server failed.</string> <string name="error_185">Incorrect email address.</string> <string name="error_186">Operation failed. Please try later.</string> <string name="error_187">Operation failed. Please try later.</string> <string name="error_189">You do not have the permission to access the gateway.</string> <string name="error_192">This type of message is not currently supported by the SMS gateway.</string> <string name="error_193">Sessiontimeout must be specified if the speed-up interface oper_type is set to 0.</string> <string name="error_196">Image size too large.</string> <string name="error_197">Incorrect image format.</string> <string name="error_198">The plug-in software version is not available.</string> <string name="error_199">Mandatory parameter symbolicName not found.</string> <string name="error_200">The plug-in record is empty.</string> <string name="error_204">BSS is not configured.</string> <string name="error_205">Failed to register the cloud account.</string> <string name="error_209">Mandatory parameter #{field} is not carried or is left empty.</string> <string name="error_210">Invalid request format.</string> <string name="error_211">The password has expired.</string> <string name="error_212">The user is blocked.</string> <string name="error_213">The MAC address is not bound.</string> <string name="error_215">You have not purchased such type of device.</string> <string name="error_216">Failed to identify the device type.</string> <string name="error_217">The number of devices of this type exceeds the maximum allowed by the package.</string> <string name="error_218">The package has not been activated.</string> <string name="error_222">Cloud storage has not been configured on the web page.</string> <string name="error_226">The account is not bound to a mobile number.</string> <string name="error_227">The account is not bound to an email address.</string> <string name="error_228">The cloud storage parameters cannot be left empty.</string> <string name="error_230">Failed to create the temporary Amazon Cloud certificate.</string> <string name="error_231">The verification code has expired.</string> <string name="error_232">No profile image has been uploaded for the user.</string> <string name="error_233">The application has not been purchased.</string> <string name="error_234">Incorrect cloud storage configuration.</string> <string name="error_236">Failed to obtain the cloud token.</string> <string name="error_237">Non-current administrator to create sub-accounts</string> <string name="error_244">The mobile number cannot be used for login.</string> <string name="error_245">The email address cannot be used for login.</string> <string name="error_246">The account cannot be modified repeatedly.</string> <string name="error_248">The plug-in is the latest version.</string> <string name="error_249">The login has expired.</string> <string name="error_250">The plug-in is the latest version.</string> <string name="error_251">Users with no service provisioned cannot bind a gateway.</string> <string name="error_252">The mobile number bound to the new administrator does not match the number segment specified by the carrier.</string> <string name="error_253">The mobile phone number bound to the new administrator is not used.</string> <string name="error_254">The mobile phone number bound to the new administrator has been used.</string> <string name="error_256">Your account has been disabled.</string> <string name="error_1018">Your password has been reset, please set a new password on the cloud platform</string> <string name="error_30000001">Operation failed. Please try later.</string> <string name="error_30000101">The account already exists.</string> <string name="error_30000102">The account does not exist.</string> <string name="error_30000103">The new account already exists.</string> <string name="error_30000104">The mobile number already exists.</string> <string name="error_30000105">The email address already exists.</string> <string name="error_30000106">The package does not exist.</string> <string name="error_30000107">Product ID{#{field}} does not exist.</string> <string name="error_30000108">Incorrect email address format.</string> <string name="error_30000109">The password does not meet the complexity requirements. Enter a correct one.</string> <string name="error_30000110">The account is not an administrator account.</string> <string name="error_30000111">No gateway has been bound for the account.</string> <string name="error_30000112">Multiple gateways have been bound for the account.</string> <string name="error_30000113">The gateway bound to this account has been enabled for remote management.</string> <string name="error_30000114">The gateway bound to this account has suspended remote management</string> <string name="error_30000115">The package is not specified.</string> <string name="error_30000116">Failed to delete the storage service file.</string> <string name="error_30000121">Unknown northbound bundle operation.</string> <string name="error_30000122">The plug-in software version is not found.</string> <string name="error_30000123">The plug-in software version already exists.</string> <string name="error_30000124">Mandatory parameter symbolicName is not specified.</string> <string name="error_30000125">The plug-in record is empty.</string> <string name="error_30000126">The plug-in has been bound to an app.</string> <string name="error_30000127">The plug-in does not exist.</string> <string name="error_30000128">The format of the mobile number is incorrect.</string> <string name="error_30000129">A new account cannot be the same as the old account.</string> <string name="error_30000130">The password must be different from previous passwords.</string> <string name="error__1004">The service package is not available for use.</string> <string name="error__1008">The entered broadband account is inconsistent with the one used for dial-up access.</string> <string name="error__1011">The user is offline.</string> <string name="error__1012">Subscribed services have expired.</string> <string name="error__1014">The IP address is outside the IP address range that has access to smart speed-up.</string> <string name="error__1015">The remaining quota is zero.</string> <string name="error_2002">The room does not exist.</string> <string name="error_2003">The device SN does not exist.</string> <string name="error_2004">The device is already added.</string> <string name="error_2005">No such device service is purchased.</string> <string name="error_2006">The package is not activated.</string> <string name="error_2007">The number of devices exceeds the limit of package.</string> <string name="error_2008">The device name already exists.</string> <string name="error_10100502">The service is invalid.</string> <string name="error_10100503">The service has ceased.</string> <string name="error_10100505">The service package does not exist.</string> <string name="error_10100508">BOD services do not support accumulation.</string> <string name="error_10100509">No basic-bandwidth information is available to calculate the amount of additional BOD bandwidth required.</string> <string name="error_10100601">The service is not subscribed.</string> <string name="error_10100602">The service is in used.</string> <string name="error_10100604">The service is not in use and the acceleration does not need to be stopped.</string> <string name="error_999">Operation failed. Please try later.</string> <string name="error_999108">Operation failed. Please try later.</string> <string name="error_999100">Service cannot parse XML-request and detect necessary fields</string> <string name="error_999101">Account for requested login or contract number is not found (for methods like balance checking)</string> <string name="error_999102">The contract attachment of the requested number is not found.</string> <string name="error_999103">Service with the requested number is not found</string> <string name="error_999104">Wrong phone number format</string> <string name="error_999105">Wrong email format</string> <string name="error_999106">Password doesn\'t meet the requirements (too short, etc)</string> <string name="error_999107">Error of internal account identification</string> <string name="error_999200">Method is not supported or not currently available</string> <string name="error_999202">BSS returns an error indicating that changing the mobile phone number failed.</string> <string name="error_999203">BSS returns an error indicating that changing the email address failed.</string> <string name="error_999300">Error during internal request processing in billing system (it will be returned XML-structure with details of the error)</string> <string name="error_network_err">Connection timed out. Check your network connection.</string> <string name="error_40000002">The length exceeds the maximum limit.</string> <string name="error_40000005">The parameter value exceeds the maximum value.</string> <string name="error_40000006">The parameter value is less than the minimum value.</string> <string name="error_80010001">The scene name already exists.</string> <string name="error_80010002">The scene name is empty.</string> <string name="error_80010003">The scene contains non-existing or deleted devices.</string> <string name="error_80010004">The scene name does not exist.</string> <string name="error_80000001">The plug-in name is empty.</string> <string name="error_80000002">The data flag is empty.</string> <string name="error_80010005">The action of the device is empty.</string> <string name="error_80010006">The device type does not exist.</string> <string name="error_80011001">The app name does not exist.</string> <string name="error_80011002">The operation name is empty.</string> <string name="error_80011003">The service name does not exist.</string> <string name="error_80012001">The camera password is incorrect or locked.</string> <string name="error_80013001">More rooms are created than allowed.</string> <string name="error_80010007">Incorrect time format in the scheduled scene.</string> <string name="error_80010008">The action name is empty.</string> <string name="error_80010009">Name of trigger condition is empty.</string> <string name="error_1013">The MAC address of your gateway does not exist on the server.</string> <string name="logo_homeGatewayService.token.invalid">Your login has expired.</string> <string name="error_1014">The number of verification codes sent in your account has reached the maximum. Please try again tomorrow.</string> <string name="error_0x02163dc3">The number of same characters in a row in the password has exceeded the maximum.</string> <string name="error_0x02163dcc">Failed to input the correct old password.</string> <string name="error_0x02163dc1">The password does not meet requirements.</string> <string name="error_0x02163dc2">The length of the password does not meet requirements.</string> <string name="error_0x021639b8">The new password must contain at least two characters that are not used in the old password.</string> <string name="error_0x02163dc4">The password does not comply with the repetition policy.</string> <string name="error_0x02163dc6">The operation does not comply with the minimum modification time interval.</string> <string name="error_0035014004">Password entered incorrectly too many times, please wait %1$s minutes and try again.</string> <string name="error_276">The mobile number has been registered with another broadband service provider and cannot be invited.</string> <string name="error_across_tenant">The mobile number has been registered with another broadband service provider, so you cannot invite it.</string> <string name="error__11">Operation failed. Please try later.</string> <string name="error__12">Operation failed. Please try later.</string> <string name="error_HomeGatewayService_parameter_invalid">Incorrect request parameter.</string> <string name="error_HomeGatewayService_cmdtype_invalid">The current platform version does not support this function.</string> <string name="error_HomeGatewayService_service_invalid">Server error.</string> <string name="error_HomeGatewayService_homegateway_no_permission">You do not have permission to manage the gateway.</string> <string name="error_281">Failed to send the SMS message. Please contact your broadband service provider.</string> <string name="error_282">The server fails to send an SMS message because the mobile number is invalid.</string> <string name="error_283">The server fails to send an SMS message because the mobile number is invalid.</string> <string name="error_284">The server is busy sending SMS messages. Please try again later.</string> <string name="error_285">The server is busy sending SMS messages. Please try again later.</string> <string name="error_286">The server fails to send the SMS message because the content is invalid.</string> <string name="error_287">The server fails to send an SMS message because the number is in the blacklist.</string> <string name="error_288">The SMS message sent by the server contains sensitive words. Change your gateway remarks and try again.</string> <string name="error_0">Operation successful.</string> <string name="error_7">The gateway is busy and cannot process the current request. Try again later.</string> <string name="error__1">Operation failed. Please try later.</string> <string name="error__2">Network request timed out.</string> <string name="error__36">The Wi-Fi service has been disabled.</string> <string name="error__100">Failed to assign transit server port. Try again</string> <string name="error__101">Failed to connect to the transit server. Check if the server is online.</string> <string name="error__105">Failed to obtain the video service address of the camera</string> <string name="error__108">User authentication failed</string> <string name="error__202">When MLO is enabled, the authentication mode must be WPA2, WPA3, or WPA2/WPA3.</string> <string name="error_001">Duplicate username.</string> <string name="error_002">Passwords don’t match.</string> <string name="error_003">Incorrect username or password.</string> <string name="error_005">The length of the password does not meet requirements.</string> <string name="error_007">The username cannot be empty.</string> <string name="error_008">Duplicate nickname.</string> <string name="error_013">Incorrect username or password.</string> <string name="error_014">Exceeded the maximum number of terminals bound to the current account.</string> <string name="error_015">Exceeded the maximum number of accounts bound to this terminal.</string> <string name="error_016">No smart ONT has been bound</string> <string name="error_020">The gateway is offline.</string> <string name="error_021">You do not have the permission to access the gateway.</string> <string name="error_022">The gateway is not registered.</string> <string name="error_023">The username does not exist.</string> <string name="error_029">No terminal is bound to this account.</string> <string name="error_039">The mobile number has not been bound to an account.</string> <string name="error_041">Failed to modify the password: The old password is entered incorrectly.</string> <string name="error_043">The gateway connection timed out. Try again later or check whether the gateway network is connected.</string> <string name="error_045">Operation failed. Please try later.</string> <string name="error_046">Operation failed. Please try later.</string> <string name="error_047">Failed to obtain the device control list.</string> <string name="error_049">Exceeded the maximum number of controllable devices.</string> <string name="error_052">The gateway is not registered.</string> <string name="error_053">Device control is not supported.</string> <string name="error_054">Operation failed. Please try later.</string> <string name="error_055">Unknown error occurred on the device control template.</string> <string name="error_065">Problem description cannot be empty.</string> <string name="error_071">Not a smart gateway.</string> <string name="error_079">Operation failed: the gateway has been unbound.</string> <string name="error_080">Operation failed: the current account has not joined.</string> <string name="error_083">Operation failed. Please try later.</string> <string name="error_084">A non-invite user cannot modify the remarks.</string> <string name="error_085">Unbinding failed. Please try again later.</string> <string name="error_086">Devices exist in the room.</string> <string name="error_088">This app does not support non-smart gateways.</string> <string name="error_089">Binding failed. Please try again later.</string> <string name="error_090">Binding failed. Please try again later.</string> <string name="error_091">Binding failed. Please try again later.</string> <string name="error_092">The number of invited users has exceeded the upper limit.</string> <string name="error_095">Deletion failed. The last user cannot be deleted.</string> <string name="error_096">The list of network users to be deleted is empty.</string> <string name="error_102">The new password cannot be the same as any previous password.</string> <string name="error_104">The password has expired.</string> <string name="error_109">The greeting message is empty.</string> <string name="error_119">The room name cannot be empty.</string> <string name="error_120">The room ID cannot be empty.</string> <string name="error_125">The username does not meet the complexity requirements. Enter a correct one.</string> <string name="error_126">The username cannot be empty.</string> <string name="error_127">The username does not meet the complexity requirements. Enter a correct one.</string> <string name="error_128">No mobile number is bound.</string> <string name="error_129">This mobile number is not a administrator account.</string> <string name="error_130">The mobile number must be a Bahraini number.</string> <string name="error_131">The mobile number has been bound.</string> <string name="error_132">No phone number is bound to the current account.</string> <string name="error_134">Administrators cannot leave.</string> <string name="error_135">The mobile number is not bound to any account.</string> <string name="error_136">The login has expired.</string> <string name="error_140">The device has been updated to the latest version.</string> <string name="error_141">No new version is available.</string> <string name="error_143">Incorrect log file format.</string> <string name="error_147">The gateway is being upgraded.</string> <string name="error_148">The phone number has already been registered once, please use another number or use this number to login.</string> <string name="error_149">Duplicate room name.</string> <string name="error_167">Operation failed. Please try later.</string> <string name="error_168">Starting the plug-in…</string> <string name="error_169">Stopping the plug-in…</string> <string name="error_170">Uninstalling the plug-in…</string> <string name="error_188">The gateway does not exist.</string> <string name="error_190">You do not have access to this gateway.</string> <string name="error_191">The message push type is empty or of an incorrect format.</string> <string name="error_194">Operation failed. Please try later.</string> <string name="error_195">You cannot invite other network members to join.</string> <string name="error_LHConsumerService_login_accountRoleMismatch">Members from other enterprises are not allowed.</string> <string name="error_LHConsumerService_bindGroup_exceedMaxBindGroupNum">Members from other enterprises are not allowed.</string> <string name="error_201">Failed to check the remaining balance.</string> <string name="error_202">Failed to change the mobile number.</string> <string name="error_203">Failed to change the email address.</string> <string name="error_214">Incorrect addedDeviceList format.</string> <string name="error_219">The password cannot be the same as the username or the username in reverse.</string> <string name="error_220">You do not have the administrative permission.</string> <string name="error_221">Failed to uninstall all the plug-ins of the home gateway.</string> <string name="error_224">The new gateway is not registered.</string> <string name="error_225">The new home gateway has been bound.</string> <string name="error_238">Your account has not been bound with a mobile number or email address. The password cannot be retrieved through self service. You are advised to contact your administrator to retrieve the password.</string> <string name="error_239">The mobile number is bound to multiple accounts.</string> <string name="error_240">The email address is bound to multiple accounts.</string> <string name="error_247">Anonymous binding information cannot be queried by using a mobile number or an email address.</string> <string name="error_255">The account has been bound to a gateway, and cannot be bound to another one.</string> <string name="error_272">Incorrect username or password.</string> <string name="error_1002">The interval between requests for sending verification codes is too short.</string> <string name="error_30000117">The home gateway MAC address does not exist.</string> <string name="error_30000118">The home gateway is offline.</string> <string name="error_30000119">The home gateway plug-in does not exist.</string> <string name="error_30000120">The plug-in is not installed or not upgraded on the home gateway.</string> <string name="error__1013">The MAC address (XXX) of your gateway does not exist on the server.</string> <string name="error_2001">The input parameter is empty.</string> <string name="error_40000001">The parameter contains unsupported special characters.</string> <string name="error_40000003">The length is less than the minimum limit.</string> <string name="error_40000004">The parameter is not a numeric type.</string> <string name="error_40000007">Invalid IP address.</string> <string name="net_conn_failed">Connection failed.</string> <string name="error__3">Connection failed.</string> <string name="error__4">The gateway is busy and cannot process the current request. Try again later.</string> <string name="error__5">Operation failed. Please try later.</string> <string name="error__7">Operation failed. Please try later.</string> <string name="error__8">Not supported by the gateway.</string> <string name="error__9">Operation failed. Please try later.</string> <string name="error__10">Your login has expired. Please log in again.</string> <string name="error_280">Failed to send the SMS message. Please try again later.</string> <string name="error_300">The current server version does not support user name registration.</string> <string name="error__102">Camera offline or camera connection failed. Check if the camera is powered on.</string> <string name="error__104">Camera video service address illegal, please report impaired</string> <string name="error__106">Gateway cannot connect to the transit server. Check if the server is online.</string> <string name="error__200">Reached the upper limit.</string> <string name="error__201">When MLO is enabled, the working mode must be 802.11be.</string> <string name="error__203">Saving Wi-Fi settings. Please wait.</string> <string name="error__204">Incorrect request parameter.</string> <string name="error__205">The Wi-Fi frequency band is not enabled. Enable it first.</string> <string name="error__301">Fails to play the real-time video of the camera. Failed to connect to the gateway locally.</string> <string name="error__330">Application control is not enabled. Please enable it first.</string> <string name="error__331">The number of application control policies has reached the upper limit.</string> <string name="error__332">The app control rule does not exist.</string> <string name="error__622">The rate limit periods cannot overlap with each other.</string> <string name="error_028">Your account has been used for login on another device. If this was done without your knowledge, your account and password may have been compromised. Choose Settings > Account And Security to change your account password.</string> <string name="error_2009">Failed to query the package status.</string> <string name="error__99999">Operation failed. Please try later.</string> <string name="error_1017">The MAC address cannot be empty.</string> <string name="error_licence_expire">Licence expired, please contact service provider.</string> <string name="LHConsumerService.bindLocalAccount.hasFamily">The account is already in a family.</string> <string name="LHConsumerService.bindLocalAccount.bound">Already bound to another account.</string> <string name="LHConsumerService.weakvalue.error">The password is too simple.</string> <string name="error_LHConsumerService_feedback_daylimit">The number of problems you reported has reached the upper limit. Try again 24 hours later.</string> <string name="error_306">Failed to send the verification code.The possible cause is that the mobile number format is incorrect or the mobile number has been bound.</string> <string name="error_307">Failed to send the verification code.The possible cause is that the email address format is incorrect or the email address has been bound.</string> <string name="error_308">Failed to send the verification code.The possible cause is that the mobile number format is incorrect or the mobile number is not bound.</string> <string name="error_309">Failed to send the verification code.The possible cause is that the email address format is incorrect or the email address is not bound.</string> <string name="error_310">The client IP address is locked,please try again later.</string> <string name="error_311">The current version does not support multi-factor authentication. Contact the administrator to disable multi-factor authentication.</string> <string name="error__340">The portal function has been enabled. The speed limit cannot be configured. To set the rate limit, disable Portal authentication on the web management page of the gateway.</string> <string name="error_AppSdk_notInitialized">Failed to initialize the app. Please log in again.</string> <string name="error_log_collect_offline">Device is offline</string> <string name="error_log_collect_conflict">Another collection task of the device is in progress.</string> <string name="error_log_collect_failed">Log collection failed</string> <string name="LHConsumerService.domainMigration.notSupportBinding">The server has been switched. Please switch to the corresponding region and register again.</string> <string name="error__41">Failed to set the 5G Wi-Fi because it has been locked. Contact your carrier to enable it first.</string> <string name="error__14">The same Wi-Fi SSID already exists in the current frequency band.</string> <string name="BindGateway_101">You have joined a family. Please refresh the home page and view it.</string> <string name="error_third_platform_user">Third-party users exist, unbinding failed.</string> </resources>
06-11
ldtcommon代码: """ .. module:: ldtcommon :synopsis: Constants and templates non dcc specific used in other packages. .. moduleauthor:: Ezequiel Mastrasso """ import lucidity import logging import os logger = logging.getLogger(__name__) LOOKDEVTOOLS_FOLDER = os.environ['LOOKDEVTOOLS'] #: Attributes for tagging meshes for surfacing and texture-to-mesh matching ATTR_SURFACING_PROJECT = "surfacing_project" ATTR_SURFACING_OBJECT = "surfacing_object" #: Attribute for tagging Materials. Values: name of assigned project or object ATTR_MATERIAL = "surfacing_material" #: Attribute for tagging material assignmnents. Values: 'project' or 'object'] ATTR_MATERIAL_ASSIGN = "surfacing_assign" #: Attribute for tagging viewport material. Values: 'color' or 'pattern'] ATTR_MATERIAL_VP = "surfacing_vp" #: Global string matching ratios to compare strings against lucidity parsed files. #: Notice that the ratio constant should be high enough, TEXTURE_MATCHING_RATIO = 90 TEXTURE_CHANNEL_MATCHING_RATIO = 90 #: Texture file template, ANCHOR RIGHT TEXTURE_FILE_PATTERN = '{surfacing_project}_{surfacing_object}_{channel}_{colorspace}.{udim}.{extension}' #: Default shader node to use DEFAULT_SHADER = 'PxrSurface' #: Materials json Config, contains texture name mappings to shader plugs CONFIG_MATERIALS_JSON = os.path.join( os.environ['LOOKDEVTOOLS'], 'python', 'ldtconfig', 'materials.json') def texture_file_template(custom_pattern=None): """ Get a lucidity Template object using a custom template. Kwargs: custom_pattern (str): Custom lucidity file pattern. Returns: lucity.Template object with the custom partern """ logger.info('Loading lucidity with:\n %s' % custom_pattern) texture_file_template = lucidity.Template( 'textureset_element', custom_pattern, anchor=lucidity.Template.ANCHOR_END # TODO (Eze) Add STRICT? ) return texture_file_template ldtmaya代码: """ .. module:: ldtmaya :synopsis: general maya functions. .. moduleauthor:: Ezequiel Mastrasso """ import ldtcommon import ldtutils from ldtcommon import ATTR_SURFACING_PROJECT from ldtcommon import ATTR_SURFACING_OBJECT from ldtcommon import ATTR_MATERIAL from ldtcommon import ATTR_MATERIAL_ASSIGN from ldtcommon import ATTR_MATERIAL_VP from ldtui import qtutils from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import os import sys import traceback import random import logging import pymel.core as pm import maya.mel as mel import maya.cmds as mc logger = logging.getLogger(__name__) def surfacingInit(): """ Initialize the scene for surfacing projects. Creates the surfacing root, an empty surfacing project and object, and runs the validation to create and connect the partition Returns: bool. Valid scene. """ root = create_surfacing_root() if not root.members: surfacing_project = create_surfacing_project("defaultProject") create_surfacing_object(surfacing_project, "defaultObject") validate_surfacing() def create_surfacing_root_node(): """Create projects root node""" surfacing_root = pm.createNode( "objectSet", name="surfacing_root" ) surfacing_root.setAttr( "surfacing_root", "", force=True ) return surfacing_root def create_surfacing_root(): """Create projects root if it doesnt exist.""" if not get_surfacing_root(): surfacing_root = get_surfacing_root() return surfacing_root else: return get_surfacing_root() def create_surfacing_project(name=None): """ Creates a surfacing project. Kwargs: name (str): surfacing project name """ if not name: name = "project" surfacing_project = pm.createNode( "objectSet", name=name ) surfacing_project.setAttr( ATTR_SURFACING_PROJECT, "", force=True ) create_surfacing_object(surfacing_project) get_surfacing_root().add(surfacing_project) update_surfacing_partition() return surfacing_project def create_surfacing_object(project, name=None): """ Creates a surfacing Object under a given project. Args: project (PyNode): surfacing project Kwargs: name (str): surfacing object name """ if not name: name = "object" surfacing_set = pm.createNode( "objectSet", name=name ) surfacing_set.setAttr( ATTR_SURFACING_OBJECT, "", force=True ) project.add(surfacing_set) return surfacing_set def get_surfacing_root(): """ Get the project root node. Returns: PyNode. Surfacing root node Raises: Exception. """ objSetLs = [ item for item in pm.ls(type="objectSet") if item.hasAttr("surfacing_root") ] if len(objSetLs) == 0: logger.info( "surfacing_root node found, creating one" ) return create_surfacing_root_node() elif len(objSetLs) > 1: raise Exception( "More than 1 surfacing_root node found, clean up your scene" ) return objSetLs[0] def get_surfacing_projects(): """ Get all surfacing Projects under the root. Returns: list. surfacing projects PyNodes list. """ objSetLs = [ item for item in pm.ls(type="objectSet") if item.hasAttr(ATTR_SURFACING_PROJECT) ] return objSetLs def get_surfacing_project_by_name(name=None): """ Get surfacing Project by name. Kwargs: name (str): surfacing project name. Returns: PyNode. Returns first found hit, we are assuming the objectSet name is equal to surfacing_project attr value. """ projects_list = get_surfacing_projects() for each in projects_list: if name == each.name(): return each return None def get_surfacing_object_by_name(name=None): """ Get surfacing Object by name. Kwargs: name (str): surfacing object name. Returns: PyNode. Returns first found hit, we are assuming the objectSet name is equal to surfacing_object attr value. """ projects_list = get_surfacing_projects() for prj in projects_list: objs = get_surfacing_objects(prj) for obj in objs: if name == obj.name(): return obj return None def delete_surfacing_project(project): """ Delete a surfacing_project, and its members. Args: project (PyNode): surfacing project. """ if is_surfacing_project(project): pm.delete(project.members()) def get_surfacing_objects(project): """ Get all surfacing Objects under the given surfacing project Args: project (PyNode): surfacing project """ if is_surfacing_project(project): return project.members() else: return [] def is_surfacing_project(project): """ Check if the node is a surfacing project Args: project (PyNode): surfacing project Returns: bool. True if it is. """ if project.hasAttr(ATTR_SURFACING_PROJECT): return True else: return False def is_surfacing_object(surfacing_object): """ Check if node is surfacing Object Args: surfacing_object (PyNode): surfacing_object """ if surfacing_object.hasAttr(ATTR_SURFACING_OBJECT): return True else: return False def remove_surfacing_invalid_members(): """ Pops all not-allowd member types from surfacing projects and objects. Only Allowed types: objectSets (surfacing_projects) inside the surfacing projects root objectSets (surfacing_object) inside surfacing projects transforms (that have a mesh) inside surfacing_object """ project_root = get_surfacing_root() for project in project_root.members(): if ( not project.type() == "objectSet" ): # TODO (eze) add check for attr project_root.removeMembers([project]) for project in get_surfacing_projects(): for object in get_surfacing_objects( project ): # TODO (eze) add check for attr if not object.type() == "objectSet": project.removeMembers([object]) else: for member in object.members(): if not member.type() == "transform": logger.info( "removing invalid member: %s" % member ) object.removeMembers([member]) elif not member.listRelatives( type="mesh" ): logger.info( "removing invalid member: %s" % member ) object.removeMembers([member]) def get_mesh_transforms(object_list): # TODO move to common """ Get all the mesh shapes transforms. Includes all descendants in hierarchy. Args: object_list (list): PyNode list of nodes. """ shapes_in_hierarchy = pm.listRelatives( object_list, allDescendents=True, path=True, f=True, type="mesh", ) shapes_transforms = pm.listRelatives( shapes_in_hierarchy, p=True, path=True, f=True ) return shapes_transforms def add_member(surfacing_object, transform): # TODO move to common """ Add transform to surfacing Object Args: surfacing_object (PyNode): surfacing object transform (PyNode): transform node """ pm.sets(surfacing_object, transform, fe=True) def add_mesh_transforms_to_surfacing_object( surfacing_object, object_list ): """ Add all mesh shape transforms -and descendants- from the list to a surfacing Object. Args: surfacing_object (PyNode): surfacing object object_list (list): object list """ pm.select() if is_surfacing_object(surfacing_object): for item in object_list: # Disconnect the objects from other Surf proj and obj for c in item.instObjGroups.listConnections(c=True, p=True): if is_surfacing_object(c[1].node()) or is_surfacing_project(c[1].node()): logger.info( "disconnecting from Surf project or obj: %s" % c[1].node() ) pm.disconnectAttr("%s"%c[0], "%s"%c[1]) for transform in get_mesh_transforms(item): pm.select(transform) add_member(surfacing_object, transform) def update_surfacing_partition(): """Recreate the partition node, and reconnects to all the surfacing objects objectSets.""" partitions = [ item for item in pm.ls(type="partition") if item.hasAttr("surfacing_partition") ] for each in partitions: logger.info( "disconnecting existing partition: %s" % each ) each.sets.disconnect() pm.delete(each) logger.info("deleted partition") surfacing_partition = pm.createNode( "partition", name="surfacing_partition" ) logger.info( "partition created: %s" % surfacing_partition ) surfacing_partition.setAttr( "surfacing_partition", "", force=True ) for project in get_surfacing_projects(): for object in get_surfacing_objects(project): pm.connectAttr( "%s.partition" % object, surfacing_partition.sets, na=True, ) logger.info( "partition connected: %s " % object ) def remove_invalid_characters(): """Remove not allowed characters from surfacing projects and names like '_'.""" project_root = get_surfacing_root() surfacing_projects = get_surfacing_projects() #invalid_character = '_' invalid_character = '*' for project in surfacing_projects: if invalid_character in project.name(): project.rename(project.name().replace(invalid_character, '')) logger.info( 'Invalid character removed from surfacing_project,' 'new name: %s' % project) for surfacing_object in get_surfacing_objects(project): if invalid_character in surfacing_object.name(): surfacing_object.rename( surfacing_object.name().replace(invalid_character, '')) logger.info( 'Invalid characters removed from surfacing_object,' 'new name: %s' % surfacing_object) def validate_surfacing(): """ Validate the scene. Removes invalidad characters and members, updates the partition, and mesh attributes. """ remove_invalid_characters() remove_surfacing_invalid_members() update_surfacing_partition() update_surfacing_attributes() def export_alembic(geo_list, file_path): """ Export alembic file from the object list. Args: geo_list (list): list of geometry to export file_path (str): export file path """ if geo_list and file_path: roots = " -root |" + " -root |".join( [str(x) for x in geo_list] ) cmd = ( r'-frameRange 0 0 -uvWrite -dataFormat ogawa ' r'-userAttrPrefix surfacing' + roots + ' -file ' + (file_path) ) logger.info("AbcExport: %s" % cmd) mc.AbcExport(j=cmd) logger.info( "Succesful Alembic export to: %s" % file_path ) def merge_surfacing_object_meshes(surfacing_object): """ Merge all the meshs assigned to a surfacing Object. Args: surfacing_object (PyNode): surfacing object Raises: BaseException. Could not merge member meshes. """ try: members = surfacing_object.members() logger.info("Merging members: %s" % members) geo_name = "%s_geo" % str(surfacing_object) if len(members) > 1: geo = pm.polyUnite(*members, n=geo_name) return geo[0] else: logger.info( "single object found, skipping merge: %s" % members[0] ) members[0].rename(geo_name) pm.parent(members[0], world=True) return members[0] except BaseException: logger.error( "Could not merge members of: %s" % surfacing_object ) return False def export_surfacing_project(project, subdiv_level=0, single_export=True, folder_path=False): """ Export surfacing Project to Alembic. Args: project (PyNode): surfacing project Kwargs: single_export (bool): is single export folder_path (str): Export folder path """ current_file = pm.sceneName() if single_export: save_unsaved_scene_() if not folder_path: folder_path = qtutils.get_folder_path() project_geo_list = [] if ldtutils.is_directory(folder_path) and is_surfacing_project(project): for each in get_surfacing_objects(project): merged_geo = merge_surfacing_object_meshes(each) if merged_geo: project_geo_list.append(merged_geo) if project_geo_list: if subdiv_level: for geo in project_geo_list: logger.info( "subdivision level: %s" % subdiv_level ) logger.info( "subdividing merged members: %s" % geo ) # -mth 0 -sdt 2 -ovb 1 -ofb 3 -ofc 0 -ost 0 -ocr 0 -dv 3 # -bnr 1 -c 1 -kb 1 -ksb 1 -khe 0 -kt 1 -kmb 1 -suv 1 # -peh 0 -sl 1 -dpe 1 -ps 0.1 -ro 1 -ch 1 pm.polySmooth( geo, mth=0, sdt=2, ovb=1, dv=subdiv_level ) export_file_path = os.path.join( folder_path, str(project) + ".abc" ) export_alembic(project_geo_list, export_file_path) export_surfacing_object_dir = os.path.join( folder_path, str(project) ) ldtutils.create_directoy(export_surfacing_object_dir) for geo in project_geo_list: export_root = " -root |" + geo export_surfacing_object_path = os.path.join( export_surfacing_object_dir + "/" + geo + ".abc" ) export_alembic( [geo], export_surfacing_object_path ) if single_export: pm.openFile(current_file, force=True) def export_all_surfacing_projects(folder_path=None, subdiv_level=0): """ Export all surfacing Projects. Kwargs: folder_path (str): folder to export files. """ save_unsaved_scene_() if not folder_path: folder_path = qtutils.get_folder_path() current_file = pm.sceneName() for project in get_surfacing_projects(): export_surfacing_project( project, subdiv_level, single_export=False, folder_path=folder_path ) pm.openFile(current_file, force=True) return True def save_unsaved_scene_(): # TODO move to common """Check the scene state, if modified, will ask the user to save it.""" if unsaved_scene(): if save_scene_dialog(): pm.saveFile(force=True) else: raise ValueError("Unsaved changes") def update_surfacing_attributes(): """ Create attributes on meshes of what surfacing object they are assigned to. Adds the attributes to all the shapes transforms assigned to surfacing objects. This will be used later for quick shader/material creation and assignment. """ for project in get_surfacing_projects(): project.setAttr(ATTR_SURFACING_PROJECT, project) logger.info( "Updating attributes for project: %s" % project ) for surfacing_object_set in get_surfacing_objects(project): logger.info( "\tUpdating attributes for object texture set: %s" % surfacing_object_set ) surfacing_object_set.setAttr( ATTR_SURFACING_OBJECT, surfacing_object_set ) members = surfacing_object_set.members() logger.info( "\t\tUpdating attr for meshes: %s" % members ) for member in members: member.setAttr( ATTR_SURFACING_PROJECT, project.name(), force=True, ) member.setAttr( ATTR_SURFACING_OBJECT, surfacing_object_set.name(), force=True, ) def set_wifreframe_color_black(): """Set the wireframe color to black in all mesh objects.""" transforms = pm.ls(type="transform") shape_transforms = get_mesh_transforms(transforms) for mesh in shape_transforms: mesh_shape = mesh.getShape() mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(0) mesh_shape.overrideColor.set(1) def set_wifreframe_color_none(): """Remove the wireframe color in all mesh objects.""" transforms = pm.ls(type="transform") shape_transforms = get_mesh_transforms(transforms) for mesh in shape_transforms: mesh_shape = mesh.getShape() mesh_shape.overrideEnabled.set(0) def set_wireframe_colors_per_project(): """ Set the wireframe color per surfacing project. For all meshes, sets it to black to start with, this implies that the mesh has not be assigned to any surfacing object yet will show black in the VP """ set_wifreframe_color_black() projects = get_surfacing_projects() for project in projects: random.seed(project) wire_color = random.randint(1, 31) for surfacingObject in get_surfacing_objects(project): for mesh in surfacingObject.members(): mesh_shape = mesh.getShape() try: mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(0) mesh_shape.overrideColor.set(wire_color) except: logger.error('Could not set override color for: %s, might ' 'belong to a display layer' % mesh ) def set_wireframe_colors_per_object(): """ Set the wireframe color per surfacing object. For all meshes, sets it to black to start with, this implies that the mesh has not be assigned to any surfacing object yet will show black in the VP """ set_wifreframe_color_black() projects = get_surfacing_projects() print projects for project in projects: for surfacingObject in get_surfacing_objects(project): for mesh in surfacingObject.members(): mesh_shape = mesh.getShape() try: mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(1) mesh_shape.overrideColorRGB.set( ldtutils.get_random_color(surfacingObject) ) except: logger.error('Could not set override color for: %s, might ' 'belong to a display layer' % mesh ) def set_materials_per_object(shader_type): """Create a material per surfacing project and assigns it""" delete_materials() projects = get_surfacing_projects() for project in projects: for obj in get_surfacing_objects(project): shader, shading_group = create_shader( type=shader_type) pm.select(obj) meshes = pm.ls(sl=True) pm.sets(shading_group, forceElement=meshes) pm.select(None) if shader_type == 'aiStandardSurface': shader.baseColor.set( ldtutils.get_random_color(obj) ) else: shader.color.set( ldtutils.get_random_color(obj) ) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), 'obj', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), obj.name(), force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), 'color', force=True) def set_materials_per_project(shader_type): """Create a material per surfacing project and assigns it""" delete_materials() projects = get_surfacing_projects() for project in projects: shader, shading_group = create_shader( type=shader_type) pm.select(project) meshes = pm.ls(sl=True) pm.sets(shading_group, forceElement=meshes) pm.select(None) if shader_type == 'aiStandardSurface': shader.baseColor.set( ldtutils.get_random_color(project) ) else: shader.color.set( ldtutils.get_random_color(project) ) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), 'project', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), project.name(), force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), 'color', force=True) def delete_materials(): """delete all material networks that have surfacing attributes""" all_shading_groups = pm.ls(type="shadingEngine") to_delete = [] for shading_group in all_shading_groups: if pm.hasAttr(shading_group, ATTR_MATERIAL): to_delete.append(shading_group) pm.delete(to_delete) def delete_materials_viewport(type=None): """ delete all material networks that have surfacing attributes. Kwargs: type (str): type of vp material to delete, usually 'color', or 'pattern' """ all_shading_groups = pm.ls(type="shadingEngine") to_delete = [] for shading_group in all_shading_groups: if pm.hasAttr(shading_group, ATTR_MATERIAL_VP): to_delete.append(shading_group) pm.delete(to_delete) def unsaved_scene(): """Check for unsaved changes.""" import maya.cmds as cmds return cmds.file(q=True, modified=True) def save_scene_dialog(): """ Ask the user to go ahead save or cancel the operation. Returns: bool. True is Ok clicked, false otherwise. """ msg = QtWidgets.QMessageBox() msg.setIcon(QtWidgets.QMessageBox.Information) msg.setText("Your scene has unsaved changes") msg.setInformativeText("") msg.setWindowTitle("Warning") msg.setDetailedText( "This tool will do undoable changes. It requires you to save your scene, and reopen it after its finished" ) msg.setStandardButtons( QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel ) retval = msg.exec_() if retval == QtWidgets.QMessageBox.Ok: return True else: return False def create_file_node(name=None): """ Create a file node, and its 2dPlacement Node. Kwargs: name (str): file node name Returns: PyNode. Image file node """ file_node = pm.shadingNode( 'file', name=name, asTexture=True, isColorManaged=True) placement_name = '%s_place2dfile_nodeture' % name placement_node = pm.shadingNode( 'place2dTexture', name=placement_name, asUtility=True) file_node.filterType.set(0) pm.connectAttr(placement_node.outUV, file_node.uvCoord) pm.connectAttr(placement_node.outUvFilterSize, file_node.uvFilterSize) pm.connectAttr(placement_node.coverage, file_node.coverage) pm.connectAttr(placement_node.mirrorU, file_node.mirrorU) pm.connectAttr(placement_node.mirrorV, file_node.mirrorV) pm.connectAttr(placement_node.noiseUV, file_node.noiseUV) pm.connectAttr(placement_node.offset, file_node.offset) pm.connectAttr(placement_node.repeatUV, file_node.repeatUV) pm.connectAttr(placement_node.rotateFrame, file_node.rotateFrame) pm.connectAttr(placement_node.rotateUV, file_node.rotateUV) pm.connectAttr(placement_node.stagger, file_node.stagger) pm.connectAttr(placement_node.translateFrame, file_node.translateFrame) pm.connectAttr(placement_node.wrapU, file_node.wrapU) pm.connectAttr(placement_node.wrapV, file_node.wrapV) return file_node def create_shader(type='PxrSurface'): """ Create shaders and shading groups. Kwargs: type (str): type of material shader to create, for ie 'blinn' tag (str): tag to set in ATTR_MATERIAL, usually the surfacing project or surfacing object Returns: tuple. PyNode shader, and PyNode shading_group """ shader, shading_group = pm.createSurfaceShader(type) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), '', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), '', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), '', force=True) return shader, shading_group def import_surfacing_textures(): """ TODO WE DONT REALLY NEED THIS DONT WE TODO THIS IS A WORKING HARDCODED IMPORT, IMPLEMENT CORRECTLY Import textures to surfacing objects or projects. Kwargs: parsed_files (list): list of lucidity parsed files with 'filepath' key key (str): surfacing attr to use for import, surfacing project or surfacing object shaders (list): a list of shaders, where the keys match the parsed files key to use for import """ import pymel.core as pm from ldtcommon import TEXTURE_FILE_PATTERN import ldtmaya import ldtutils import ldttextures textures_folder = '/run/media/ezequielm/misc/wrk/current/cabinPixar/textures' texture_list = ldtutils.get_files_in_folder( textures_folder, recursive=True, pattern='.tex') texture_finder = ldttextures.TextureFinder( TEXTURE_FILE_PATTERN, texture_list) texture_finder.get_channel_plug(texture_list[0]) for surfPrj in ldtmaya.get_surfacing_projects(): for surfObj in ldtmaya.get_surfacing_objects(surfPrj): # Find texture files with a matching surfacing_project, get udim paths texture_files = texture_finder.find_key_values( surfacing_project=surfPrj, merge_udims=True) if texture_files: print surfObj # create and assign the material to each surfacing_object pm.select(surfObj) meshes = pm.ls(sl=True) shader, shading_group = ldtmaya.create_shader() shader.rename('%s_%s' % (surfObj, 'material')) pm.sets(shading_group, forceElement=meshes) pm.select(None) for texture_file in texture_files: # get the shader plug input, to connect the texture to shader_plug = texture_finder.get_channel_plug(texture_file) if shader_plug: shader_plug = pm.PyNode( '%s.%s' % (shader.name(), shader_plug)) # create image nodes file_node = ldtmaya.create_file_node( '%s_%s' % (surfObj, shader_plug)) # paths come with 'udim', replac this with the prman <UDIM> file_node.fileTextureName.set( texture_file.replace('udim', '<UDIM>')) pm.setAttr('%s.%s' % (file_node, 'uvTilingMode'), 3) pm.setAttr('%s.%s' % (file_node, 'alphaIsLuminance'), 1) # TODO Query the shader plug, connect RGB or single channel # TODO if a normal or bump, create the inbetween node # if shader_plug == "normal" or shader_plug == "bump" print "plug %s -->%s" % (texture_file, shader_plug) if len(shader_plug.elements()) == 4: if "bump" in shader_plug.name(): bump_node = pm.shadingNode( 'bump2d', asTexture=True) bump_node.rename('%s_%s' % (surfObj, 'bump')) file_node.outAlpha.connect(bump_node.bumpValue) bump_node.outNormal.connect(shader_plug) else: file_node.outColor.connect(shader_plug) else: file_node.outAlpha.connect(shader_plug) ldtui代码:init_.py: """ .. module:: ldtui :synopsis: Main tools UI. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import sys import imp import os import logging from functools import partial from ldtui import qtutils import ldt logger = logging.getLogger(__name__) class LDTWindow(QMainWindow): '''Main Tools UI Window. Loads the plugInfo.plugin_object.plugin_layout QWidget from all loaded plugins as tabs''' def __init__(self, plugins): super(LDTWindow, self).__init__() self.setWindowTitle("Look Dev Tool Set") self.setGeometry(0, 0, 650, 600) layout = QtWidgets.QGridLayout() self.setLayout(layout) tabwidget = QtWidgets.QTabWidget() tabwidget.setTabBar(qtutils.HTabWidget(width=150, height=50)) tabwidget.setTabPosition(QtWidgets.QTabWidget.West) # Stylesheet fix for Katana # With default colors, the tab text is almost the # same as the tab background stylesheet = """ QTabBar::tab:unselected {background: #222222;} QTabWidget>QWidget>QWidget{background: #222222;} QTabBar::tab:selected {background: #303030;} QTabWidget>QWidget>QWidget{background: #303030;} """ tabwidget.setStyleSheet(stylesheet) layout.addWidget(tabwidget, 0, 0) plugins_ui = {} plugins_buttons = {} for pluginInfo in plugins.getAllPlugins(): tabwidget.addTab( pluginInfo.plugin_object.plugin_layout, pluginInfo.name) self.setCentralWidget(tabwidget) qtutils.py: """ .. module:: qtutils :synopsis: small qt utilies and custom widgets. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import logging logger = logging.getLogger(__name__) def get_folder_path(): """Gets a folder path from the user""" file_dialog = QtWidgets.QFileDialog() file_dialog.setFileMode(QtWidgets.QFileDialog.Directory) if file_dialog.exec_(): path = str(file_dialog.selectedFiles()[0]) return path else: return None class HTabWidget(QtWidgets.QTabBar): ''' QPaint event to draw the QTabWidget titles horizontally ''' def __init__(self, *args, **kwargs): self.tabSize = QtCore.QSize(kwargs.pop('width'), kwargs.pop('height')) super(HTabWidget, self).__init__(*args, **kwargs) def paintEvent(self, event): painter = QtWidgets.QStylePainter(self) option = QtWidgets.QStyleOptionTab() for index in range(self.count()): self.initStyleOption(option, index) tabRect = self.tabRect(index) tabRect.moveLeft(10) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, option) painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(index)) def tabSizeHint(self, index): return self.tabSize ldtutils代码: """ .. module:: ldtutils :synopsis: general non dcc specific utils. .. moduleauthor:: Ezequiel Mastrasso """ from ldtcommon import CONFIG_MATERIALS_JSON from ldtcommon import TEXTURE_CHANNEL_MATCHING_RATIO from ldtcommon import TEXTURE_MATCHING_RATIO from fuzzywuzzy import fuzz import subprocess import multiprocessing import sys import json import logging import os import random import lucidity logger = logging.getLogger(__name__) def create_commands(texture_mapping): default_command = "maya -batch -file someMayaFile.mb -command " commands = [] for key, value in texture_mapping: dir, filename = os.path.split(key) filename, ext = os.path.splitext(filename) filename = os.path.join(dir, filename + "WithTexture." + ext) commands.append("".format(default_command, "abc_file", key, "texture", value, "file -save ", filename)) return commands def launch_function(func, args): commands = create_commands(args) launch_multiprocess(launch_subprocess, commands) def launch_subprocess(command): try: subprocess_output = subprocess.check_output(command) logger.info("Subprocess launched\t{out}\nrunning the command\t{command}", out=subprocess_output, command=command) except subprocess.CalledProcessError as e: logger.exception( "Error while trying to launch subprocess: {}".format(e.output)) raise return command def launch_multiprocess(function, args): """ :param function: Function to be called inside the multiprocess -- Takes only 1 arg :param args: list/tuple of arguments for above function :return: """ multiprocessing.freeze_support() pool = multiprocessing.Pool(processes=int(multiprocessing.cpu_count()/2)) logger.debug("Pool created") try: pool_process = pool.map_async(function, (args)) except Exception as e: msg = "Error while trying to launch process in pool: {}".format(e) logger.exception(msg) raise finally: pool.close() pool.join() try: logger.info("output from the process: {pool_out}", out=pool_process.successful()) return 0 except AssertionError: logger.exception("Couldn't communicate with the process poll created; \ No output from processes fetched") return 1 def map_textures_to_alembic(texture_mapping): launch_subprocess(command) def load_json(file_path): """ Load a json an returns a dict. Args: file_path (str): Json file path to open. """ with open(file_path) as handle: dictdump = json.loads(handle.read()) return dictdump def save_json(file_path, data): """ Dump a dict into a json file. Args: file_path (str): Json file path to save. data (dict): Data to save into the json file. """ # TODO (eze) pass def get_random_color(seed): """ Return a random color using a seed. Used by all material creating, and viewport color functions that do not use textures, to have a common color accross dccs Args: seed (str): Returns: tuple, R,G,B colors. """ random.seed(seed + "_r") color_red = random.uniform(0, 1) random.seed(seed + "_g") color_green = random.uniform(0, 1) random.seed(seed + "_b") color_blue = random.uniform(0, 1) return color_red, color_green, color_blue def create_directoy(path): """ Create a folder. Args: path (str): Directory path to create. """ os.mkdir(path) logger.info("Directory created: %s" % path) def is_directory(path): """ Check if the given path exists, and is a directory. Args: path (str): Directory to check. """ if os.path.exists(path) and os.path.isdir(path): return True else: return False def get_files_in_folder(path, recursive=False, pattern=None): """ Search files in a folder. Args: path (str): Path to search. Kwards: recursive (bool): Search files recursively in folder. pattern (str): pattern to match, for ie '.exr'. Returns: array. File list """ logger.info("Searching for files in: %s" % path) logger.info("Searching options: Recursive %s, pattern: %s" % (recursive, pattern)) file_list = [] for path, subdirs, files in os.walk(path): for file in files: # skip .mayaswatchs stuff if ".maya" not in file: if pattern: if pattern in file: file_list.append(os.path.join(path, file)) logger.debug( "File with pattern found, added to the list: %s" % file) else: file_list.append(os.path.join(path, file)) logger.debug("File added to the list: %s" % file) if not recursive: break return file_list def string_matching_ratio(stringA, stringB): """ Compare two strings and returns a fuzzy string matching ratio. In general ratio, partial_ratio, token_sort_ratio and token_set_ratio did not give different results given that we are comparin a single. TODO: Try bitap algorithm for fuzzy matching, partial substring matching might be better for our cases. Different channels fuzzy ratio comparission ('baseColor','diffusecolor') = 67 ('base','diffusecolor') = 25 ('specular','specularColor') = 76 ('specular','specularcolor') = 76 ('specular_color', 'specular_bump') = 67 ('coat_color', 'coat_ior') = 78 ('secondary_specular_color', 'secondary_specular_ior') = 91 ('subsurface_weight', 'subsurface_Color') = 67 ('emission', 'emission_weight') = 70 Same channel diferent naming ratio comparission ('diffuse_weight','diffuseGain') = 64 Args: stringA (str): string to compare against. stringB (str): string to compare. Returns: int. Ratio, from 0 to 100 according to fuzzy matching. """ return fuzz.token_set_ratio(stringA, stringB) def get_config_materials(): """ Gets the CONFIG_MATERIALS_JSON as a dict Returns: dict. CONFIG_MATERIALS_JSON """ return load_json(CONFIG_MATERIALS_JSON)
最新发布
08-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值