Notifications
Clear all

[Closed] Create objects with variable names?

Problem: I want to read in a 2D image and create many 3D spheres to replicate the image. Each sphere needs to be assigned a material of what the current pixel is in the image.

My code:

x = 0
y = 0

-- [I]FOR EACH ROW[/I]
for r=0 to h-1 do (
	pixels=getpixels b [0,r] w
	x = x + 10
	y = 0

-- [I]FOR EACH COLUMN[/I]
  for c=1 to w do (
	p=pixels[c]
        y = y + 10

-- [I]CREATE BLINN IN SLOT ONE, SET DIFFUSE TO CURRENT PIXEL COLOR[/I]
	meditMaterials[1].shaderType = 1
	meditMaterials[1].Diffuse = color ((p.r as integer)*256) ((p.g as integer)*256) ((p.b as integer)*256)

-- [I]CREATE SPHERE AT CURRENT LOCATION[/I]
	Sphere radius:5 pos:[x,y,0] segs:20

-- [I]MAKE THAT SPHERE HAVE THE MATERIAL OF SLOT 1[/I]
        [B]$Sphere[CURRENT].material = meditMaterials[1]?????[/B]







What do I put for that last line to assign material 1 to the recently created sphere?

Is it possible to create an object with a variable in its name?

EDIT: I changed the line before the to:

-- CREATE SPHERE AT CURRENT LOCATION WITH MATERIAL 1
	Sphere radius:5 pos:[x,y,0] segs:20 material: meditMaterials[1]

But it textures every sphere in the scene with the bottom right pixel’s RGB value…
Why is this?

9 Replies

you are doing almost everything right. why do you ask about a possibility of making the unique name?
do you really want to know how to make a unique name?

Well with the current problem I’m running into with them all being the color of the last pixel in the image, I am assuming that while it is running the max script, it isn’t assigning materials until after creating all of the spheres… hmm

EDIT: Picture example of 32×32 image with bottom right pixel color is white

you have to make your own rule of naming… probably the name based on pixel color in general can not be unique. but if you add x-y pixel coordinate to the name it makes the name unique.
the sample code is:


for y=0 to 9 do for x=0 to 9 do box name:("_"  + x as string + "." + y as string) pos:[x*10, y*10, 0]

I suppose the names are already “unique” because they are Sphere001, Sphere002, … , until Sphere#### (width*height of the image).

How can I make a for-loop to assign the materials or simply assign them as I create them?

I’m beating myself in the head over this one. :banghead:

What is Maxscripts naming convention for adding variables to the name?

I’ve got all of the spheres named “Pixel1” all the way to “Pixel1024”, yet I can’t seem to assign the materials correctly.

Even if I wanted to change the wirecolor I still have problems.

Is it something like:

(
i = 1
$Pixel<‘i’>.wirecolor = color 0 0 0
)

to set Pixel1 to black wirecolor?

Argh. :wip:

and i think this is your actual problem, not the names.
now you use 1 instance material for all objects,
so shortly you need new material for each object.

m = standard shaderType:1 color:p
Sphere radius:5 pos:[x,y,0] segs:20 material:m

Thanks, I’m really close now… but have run into another problem.

Test image (32×32 pixels)

3D Spheres (32×32 spheres)

Hmm… why are some colors/areas not assigned the correct material, while others are?

-- FOR EACH ROW
for r=0 to h-1 do (
pixels=getpixels b [0,r] w
x = x + 10
y = 0
	
-- FOR EACH COLUMN
for c=1 to w do (
p=pixels[c]
y = y + 10

m = standard shaderType:1
m.diffuse = (color((p.r as integer)*256)((p.g as integer)*256)((p.b as integer)*256))
Sphere radius:5 pos:[x,y,0] segs:20 material:m
))


Is this unpack correct?

format "fn unpack val = for p in val collect(r=p/256^2; g=p/256-r*256; b=mod p 256; color r g b)
"


Sorry for all the questions.

i wonder why you need this color conversion (multiplying with 256).
scaning bitmap with GetPixels returns array of Colors.
run next test –

c = (color 5 15 25)
c2 = (color (c.r*256) (c.g*256) (c.b*256))
-->> (color 1280 3840 6400)
 
img1 = bitmap 100 100 color:c
img2 = bitmap 100 100 color:c2
 
display img1
display img2 

now try what i post before (no color conversion) –

m = standard shaderType:1 color:[b]p[/b]

cheers!

:bounce: It worked, Thanks! I would have never guessed :bounce:
(I deleted the *256’s in the diffuse line)