Anim8or Community

Please login or register.

Login with username, password and session length
Advanced search  

News:

Ian Ross has just released a book on Anim8or. It's perect for a beginner and a good reference for experienced users. It contains detailed chapters on every aspect, with many examples. Get your own copy here: "Anim8or Tutorial Book"

Pages: 1 2 [3]

Author Topic: Animated Textures Using Controller Scripts  (Read 70199 times)

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Animated Textures Using Controller Scripts
« Reply #30 on: October 01, 2021, 12:10:47 pm »

I have tried repeatedly to post this but it kept failing to post. It could be due to the zip file attachment, even though it is only 2.5MB. So I will post this text, and try to post the zip file as a second post.

Raxx's script isn't as easy as it could be to use. So I have refactored it to make it as simple to use as I can. There is no time scaling in this version of the script.


I have attached a zip file containing an example project with two flames. Only absolute paths work, so for this example project to work on everyone's machine, it needs to be in a known location. I've coded the scripts to work if you un-zip the attached zip file to the directory:
C:\Simple Animated Textures
i.e.
The flame .png files should be located in the directory:
C:\Simple Animated Textures\Sprites\fire

To correspond to the path set with the script line:
$texPath = "C:/Simple Animated Textures/Sprites/fire/";

(Relative paths can appear to work when you scroll along the time line, but render movie will not be animated.)

To run the example project:

In Anim8or open "C:\Simple Animated Textures\Example 1 - Two flames.an8"

Switch to Scene view.
Select camera view (e.g. press 1 on the numeric key pad)

The flames will animate if you drag the time slider, or render the movie.
This is the video it renders:


This is a much simplified version of Raxx,s script which bypasses most of the complexity.

I use the frame variable to work out which texture file needs to be loaded - so no messing around with graphs or keys.

To use the script for youself you just have to:
1) Create a named texture. For example the left hand flame texture in the example is named "fire1". And the script you create for this texture in step 2e knows which texture to animate with the following line:
$t = project.LookupTexture("fire1");

Raxx's video:
&t=5s&ab_channel=Raxx
walks you through creating this texture at 2:01, and applying it to a mesh between 1:04 and 3:23

Raxx's video gives a second example of doing this between 4:09 and 5:18

2) for that texture, in Scene view:
a) add a Target (Build->add target). Give it a name like "fire3_sprite" to aid you knowing which texture it corresponds to.
b) double click on the target, and click on the "..." in the "Location" section.
c) make sure the "On" Expression radio button is selected, and press "Edit" next to it.
d) Open "C:\Simple Animated Textures\sprite_controller.txt" in a text editor, select all the text and copy it. Then replace the text in the Script Viewer that was displayed as a result of pressing "Edit", by pasting the text from "sprite_controller.txt"

e) Tweak the script to match your needs. E.g. the absolute path to the texture animation files. And the name of the texture that this script corresponds to. And the number of the first and last file in the animation sequence. To change which texture frame is displayed on a particular animation frame set the $firstFrameNum variable.

f) Press OK, on each of the three pop-up windows to close them down. Then save your project.

3) The texture should animate now. Presuming you have applied it to a mesh (I applied the texture to both Ambient and Diffuse in the example project).


I've posted a superman video to YouTube using it:


Other notes (Things that I may have over simplified):
1) One of the simplifications I have made is to remove the variables to disable the script. But you can disable the script by commenting out the entire script. You can do this by placing a "/*" open comment before the first line, and a matching "*/" after the last line to disable the script. (The speech marks are not to be included.)

2) To keep the time line clean, it makes sense to place the sprites under a parent object like Raxx does. In his video he shows how to create a container object to contain scripts at 15:54 :
Build->Add Target
Name it: sprites_scripts
Then double click on the individual sprites, like "fire1_sprite", and select "sprites_scripts" as the parent.

3) The simplified script loops over the texture files for the entire period of animation. However it would not be hard to modify the script to have a lead-in texture file to be displayed before frame == $firstFrameNum. e.g. this could be a png with all pixels 100% transparent. And you could have another file to be displayed when frame > ($firstFrameNum + $numFrames). E.g. a soot mark from an explosion.

This is a bit more advanced, and you will be changing code that appears after the line:
// /////////////////////////////////
// EVERYTHING PAST HERE, LEAVE ALONE
// /////////////////////////////////

Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Animated Textures Using Controller Scripts
« Reply #31 on: October 01, 2021, 12:24:46 pm »

I still cannot post the zip file! I also tried posting it as a 7z file. That failed too!
I also tried posting with notification turned off, etc.


Here is the contents of "sprite_controller.txt" which is what makes it work. This example uses the texture files from Raxx's original post:



texture $t;
string $texPrefix, $texType, $texPath, $texFileName;
int $i, $k, $texStartingFrame, $texEndingFrame, $texDigits;
int $texFrame, $numFrames;
int $firstFrameNum; // This is an animation frame number to correspond to $texStartingFrame image. E.g, two torches can burn out of sync by setting $firstFrameNum to different frames.
$firstFrameNum = 0;

// What's the name of the texture? This is NOT the file name, it's the Name field in the Texture Map panel in the Texture Selector dialog box of the material editor.
$t = project.LookupTexture("fire1");

// Texture File Path - Must be the exact location, does not look through subfolders
// Leave blank only if the files are located in the textures path in File->Configure (not in a subfolder!)
// Absolute Path Example: "D:/MyProject/Sprites/"
// Local Path (of .an8 file) Example: "./Sprites/"  Note that rendering movies doesn't work with relative paths - just use absolute paths.
$texPath = "C:/3D CAD/Anim8orScripts/Simple Animated Textures/Sprites/fire/";

// Texture Prefix - Enter the name (without numbers) of the image sequence's file name, right up until the numbering.
// For example, "filename_00001.png" has the prefix, "filename_"
$texPrefix = "fire1_";

// Number of digits in the texture name
// For example, if it's filename_00001, the value would be 5
$texDigits = 4;

// Texture File Type - "png", "bmp", "gif", or "jpg"
$texType = "png";

// What's the starting number?
// For example, if the texture is named "filename_00001.png", the value is 1
// For this and the ending number, configure any additional textures you added earlier as well
$texStartingFrame = 55;

// What's the ending number?
$texEndingFrame = 106;


// /////////////////////////////////
// EVERYTHING PAST HERE, LEAVE ALONE
// /////////////////////////////////

// Determine number of frames
$numFrames = $texEndingFrame - $texStartingFrame + 1;

// Note: frame is a built in variable, as is time
// Mod results in frames looping over the available texture files.
//  $firstFrameNum allows you to choose which frame the texture file sequence loop starts on.
//  Adding $numFrames allows animation even when frame < $firstFrameNum
$texFrame = (frame + $numFrames - $firstFrameNum) % $numFrames;

// Belts and Braces: clamp should be redundent, but do need to add  $texStartingFrame
$texFrame = clamp($texFrame + $texStartingFrame, $texStartingFrame, $texEndingFrame);

// Determine how many zeroes to pad
$texFileName = PrintToString("%d", $texFrame);
$k = $texDigits - $texFileName.length();
$texFileName = "";
while($k > 0)
{
   $k = $k - 1;
   $texFileName = PrintToString("%s%s", $texFileName, "0");
}

// At this point  $texFileName contains just the padding zeros of the file number (if any)
$texFileName = PrintToString("%s%s%s%d.%s", $texPath, $texPrefix, $texFileName, $texFrame, $texType);

if ($t.GetFileName != $texFileName)
$t.SetFileName($texFileName);

Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Animated Textures Using Controller Scripts
« Reply #32 on: October 01, 2021, 01:17:09 pm »


How the values in the script were derived:

In the directory:
C:\Simple Animated Textures\Sprites\fire
There are a number of texture files, each with a number suffix.
The first one is called: fire1_0055.png
The last one is called: fire1_0106.png

Previously you will have done the following:
In Object view, a mesh was created, double clicking on a mesh face will bring up the "Mesh Editor" dialogue. On the "Material" drop down box, select new and click the "..." button next to it to bring up the "Material Editor" dialogue. Press the "." button next to "Ambient" brings up the Texture dialogue box, and you can create a new texture by clicking the "Load Texture" button. Select a texture file from the sequence, e.g. "fire1_0055.png". At this point the "Name:" field will say "fire1_0055", change this to "fire1", this is the name of the texture that the script will use to identify the texture!

So back to the script file:

Identify the texture to animate by giving its name in quotes in the following line:
$t = project.LookupTexture("fire1");

Tell the script where to find the texture files. On our machine they are in the directory: C:\Simple Animated Textures\Sprites\fire
So put that full path in quotes on the following line (note that you have to swap the '\' characters to '/' otherwise you will get an error. You also add one at the end.)
$texPath = "C:/Simple Animated Textures/Sprites/fire/";

Our texture files all start with a name, and end with a number followed by the file extension. e.g. "fire1_0055.png"
Tell the script what the first part of the file name is with the following line:
$texPrefix = "fire1_";

Next tell the script how many digits there are in the file name. In our case "0055" is four digits.
$texDigits = 4;

Then tell the script what file extension our texture files have.
$texType = "png";

Next we tell the script the number of the first file in the texture sequence. In our case this is file "fire1_0055.png", so this is 55:
$texStartingFrame = 55;

Then we tell the script the number of the last file in the texture sequence. In our case this is file "fire1_0106.png", so this is 106:
$texEndingFrame = 106;

And that is all you have to change!

There is one more variable you can change if you want. This is at the top of the file, and normally is just set to 0:
$firstFrameNum = 0;
But if you want two flames burning, and not have both animations in step, you can define a second flame texture, and the script for the second flame will be identical except for the value of this variable. In the second texture script you could set:
$firstFrameNum = 20;
This defines which animation frame corresponds to the first texture file.
Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Animated Textures Using Controller Scripts
« Reply #33 on: October 01, 2021, 05:58:10 pm »

I suppose I can try uploading the two new files without putting them in a zip file.
Basically the 7z file Raxx uploaded is the directory structure I was using. The flame texture files can be found in that archive.

I have attached my example project:
"Example 1 - Two flames.a8"

And my simplified animated texture script template:
"sprite_controller.txt"
Logged

2020 Hindsight

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Animated Textures Using Controller Scripts
« Reply #34 on: October 07, 2021, 11:24:20 am »

Tiger carasel
Logged

ianross

  • Sr. Member
  • ****
  • Posts: 251
  • Imagination is the elixir of life.
    • View Profile
    • https://www.tradebit.com/filedetail.php/164976574-part-1-anim8or-tutor-course
Re: Animated Textures Using Controller Scripts
« Reply #35 on: August 05, 2022, 08:22:16 am »

Big thank you to Raxx and Hindsight.
Please check out my website page

https://arkhouse.jimdofree.com/free-e-book-anim8or-movie-manual/
Logged
Pages: 1 2 [3]