Sublime Forum

Multi-select Multiplication

#1

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:

<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?

0 Likes

#2

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,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.

0 Likes

#3

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]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)

[/code]

edited

0 Likes

#4

To run it, add the following to your user keybindings: { "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.

more info: docs.sublimetext.info/en/latest/ … ugins.html

0 Likes

#5

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

[code]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)

[/code]

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

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'

0 Likes

#6

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 if sel[index]>Max: ... should be if self.view.substr(sel[index]) > Max: ...

Hope that makes sense.

0 Likes

#7

EDIT3:

Current Code:

[code]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[/code]

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

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

0 Likes

#8
I-0.8(I-Avg)

what are you trying here? multiplication?

0 Likes

#9

Python doesn’t understand that notation. You’ll have to use i-0.8*(i-avg)

0 Likes