## Multi-select Multiplication

### Multi-select Multiplication

I'm not sure if I'm posting in the right place, any moderators feel free to move me to the section you think is most suitable as I am new to these forums.

I am coding a video game with a bone structure system, and I use Sublime Text 2 (evaluation now, may buy soon.) to edit the xml files for the animations. I am trying to test my short algorithm for exaggeration of bone movements. (essentially A-P(A-C), A being the current relative angle, P being the change amount to multiply by, c being the center/avg of the max/min angles, but that's irrelevant.)

The XML of the angles looks like this:
Code: Select all
<keyframe angle="100" timestamp="1"/>
<keyframe angle="97" timestamp="4"/>
<keyframe angle="93" timestamp="7"/>
<keyframe angle="90" timestamp="10"/>

What I would like to be able to do is highlight the numbers in front of "angle", whether via find operation or by hand, and perform the math I would like to on them. (in this case the A=100, 97, 93, or 90 respectively, P being... let's say 0.8 for a little reduction of movement, and C being 95). I have lots of animations and I would prefer not to change them all by hand with a calculator. Is there a plugin that can allow me to do this with multi-selection editing?
Carocrazy132

Posts: 4
Joined: Sun Jul 29, 2012 7:18 pm

### Re: Multi-select Multiplication

Sounds simple enough to do with a plugin. If you're familiar with python, you can write it yourself (API). If not, I'm sure if you ask nicely, someone will get you on the right track.

To start,
Code: Select all
sel = self.view.sel() // an array of all the current selections
solution = sel[0] + sel[1] ... // your math here
sublime.set_clipboard(solution) // send the solution to the clipboard.
C0D312

Posts: 1056
Joined: Sun Jul 10, 2011 3:23 am

### Re: Multi-select Multiplication

I've actually never touched Python before so I'm hoping this is mostly correct as I don't have a full grasp of the syntax, just what I could get from a quick peek at the documentation. How does this look, and how do I run it?

Code: Select all
import sublime, sublime_plugin

class Multimath(sublime_plugin.TextCommand):
def run(self, edit):
#self.view.insert(edit, 0, "Hello, World!")
sel = self.view.sel()
Max=0
Min=0
for index in range(len(sel)):
if sel[index]>Max:
Max=sel[index]
if sel[index]<Min:
Min=sel[index]
Avg=Max-Min
solution=""
for index in range(len(sel)):
newNumber=sel[index]-0.8(sel[index]-Avg)
solution=solution+newNumber+"\n"

sublime.set_clipboard(solution)

edited
Last edited by Carocrazy132 on Sun Jul 29, 2012 8:20 pm, edited 1 time in total.
Carocrazy132

Posts: 4
Joined: Sun Jul 29, 2012 7:18 pm

### Re: Multi-select Multiplication

Code: Select all
{ "keys": ["ctrl+r"], "command": "example"}

"keys" can be whatever you want, "command" must correspond with the name of the class. However, Sublime Text converts the classname from camelCase to snake_case and removed the "Command" suffix. So ExampleCommand becomes example.

It also helps to open the console (control+`) to see any errors or print statements.

C0D312

Posts: 1056
Joined: Sun Jul 10, 2011 3:23 am

### Re: Multi-select Multiplication

Alright, I think I've about got it. Current code:

Code: Select all
import sublime, sublime_plugin

class mmathCommand(sublime_plugin.TextCommand):
def run(self, edit):
#self.view.insert(edit, 0, "Hello, World!")
sel = self.view.sel()
Max=sel[1]
Min=sel[1]
for index in range(len(sel)):
if sel[index]>Max:
Max=sel[index]
if sel[index]<Min:
Min=sel[index]
Avg=Max-Min
solution=""
for index in range(len(sel)):
newNumber=sel[index]-0.8(sel[index]-Avg)
solution=solution+newNumber+"\n"

sublime.set_clipboard(solution)

Getting this error, not sure I understand it considering the circumstances.

Code: Select all
Traceback (most recent call last):
File ".\sublime_plugin.py", line 362, in run_
File ".\MultiEdit.py", line 14, in run
Avg=Max-Min
TypeError: unsupported operand type(s) for -: 'Region' and 'int'
Carocrazy132

Posts: 4
Joined: Sun Jul 29, 2012 7:18 pm

### Re: Multi-select Multiplication

Oh, whoops forgot to mention something important. self.view.sel() returns an array of "regions." A region is essentially an arrays with 2 values: a start and an end point. To get the actually characters, you need to convert the regions to characters using self.view.substr(region). So
Code: Select all
if sel[index]>Max:
...
should be
Code: Select all
if self.view.substr(sel[index]) > Max:
...

Hope that makes sense.
C0D312

Posts: 1056
Joined: Sun Jul 10, 2011 3:23 am

### Re: Multi-select Multiplication

EDIT3:

Current Code:

Code: Select all
import sublime, sublime_plugin

class mmathCommand(sublime_plugin.TextCommand):
def run(self, edit):
#self.view.insert(edit, 0, "Hello, World!")
sel = self.view.sel()
Max=float(self.view.substr(sel[1]))
Min=float(self.view.substr(sel[1]))
for index in range(len(sel)):
I=float(self.view.substr(sel[index]))
if I > Max:
Max=I
if I < Min:
Min=I
Avg=Max-Min
solution = ""
for index in range(len(sel)):
I=float(self.view.substr(sel[index]))
newNumber=I-0.8(I-Avg)
solution=solution+newNumber+"\n"

sublime.set_clipboard(solution)

#-100
#-50
#0
#50
#100

Getting an error I don't know how to deal with:

Code: Select all
Traceback (most recent call last):
File ".\sublime_plugin.py", line 362, in run_
File ".\MultiEdit.py", line 19, in run
newNumber=I-0.8(I-Avg)
TypeError: 'float' object is not callable
Carocrazy132

Posts: 4
Joined: Sun Jul 29, 2012 7:18 pm

### Re: Multi-select Multiplication

Code: Select all
I-0.8(I-Avg)

what are you trying here? multiplication?
vitaLee

Posts: 144
Joined: Tue Dec 27, 2011 11:10 pm

### Re: Multi-select Multiplication

Python doesn't understand that notation. You'll have to use
Code: Select all
i-0.8*(i-avg)
C0D312

Posts: 1056
Joined: Sun Jul 10, 2011 3:23 am