python - Number/Version Paddings in Maya -


i trying write out code doing number/ version padding tried search thru internet, however, able find mel example in works doesn't make sense me (most not understand how works)

$padding = 3; $num = 5; string $pad = `python ("'%0"+$padding+"d' % "+$num)`; // results is: 005 

however when tried convert python style, got following result:

padding = '3' num = '5' pad = ("%0"+padding+"d' % "+num) result is: %03d' % 5 

or tried rearrange code around, results either wrong (totally wrong, can see) or maya errors typeerror: cannot concatenate 'str' , 'int' objects

any pointers?

padding = '3' num = '5' pad = ("%%0%si" % padding) % int(num) print pad # prints '005' 

how works:

you use %% escape %% % after string processing::

in [17]: step1 = ("%%0%si" % padding)  in [18]: step1 out[18]: '%03i'  in [19]: step2 = step1 % int(num)  in [20]: step2 out[20]: '005' 

how works: (second try ;))

when string processing "%" has special meaning

"%s" replaced string "%i" replaced integer "%%" means "%", kinda \n newline , \\ \ 

the %x can furether modified putting numbers between

"%10s" means string padded 10 spaces "%010i" means integer padded 10 zeros 

what want integer one, since numbers 0 part has come variable have 2 steps of string processing, use %%->% trick in first round of string processing th %03i'

here use parenthesis group stuff logically, there of course no parenthesis in real code :)

"(%%)0(%s)i" % num  %% => % "%s" % num => '3'  (%)0(3)i %03i 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -